Elasticsearch搜索语法

创建一个demo索引

number_of_shards:主分区切片数
number_of_replicas:分区副本数
refresh_interval:索引更新时间
在elasticsearch7.0以后的版本废弃了type,mappings映射可以不指定type。下面是指定了type(doc)

PUT /demo
{
	"settings": {
		"number_of_shards": 1,
		"number_of_replicas": 1,
		"refresh_interval": "30s"
	},
	"mappings": {
		"doc": {
			"properties": {
				"title": {
					"type": "keyword"
				},
				"content": {
					"type": "text",
					"analyzer": "ik_max_word"
				},
				"date": {
				    "type": "date",
                    "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
				},
				"age": {
				    "type": "integer",
				    "index": false
				},
				"is_vip": {
				    "type": "boolean"
				},
                "range_price": {
                    "type": "integer_range"
                },
                "range_date": {
                    "type": "date_range",
                    "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
                },
                "range_ip": {
                    "type": "ip_range"
                },
                "tags": {
                    "properties": {
                        "name": {"type": "text"},
                        "is_show": {"type": "boolean"},
                        "sort": {"type": "integer"}
                    }
                },
                "nodes": {
                    "type": "nested"
                },
                "ip": {
                    "type": "ip"
                },
                "location": {
                    "type": "geo_point"
                }
			}
		}
	}
}
批量添加一些数据
GET /demo/doc/_bulk
{"index":{}}
{"title":"变改给大家带来了什么","content": "迎来了新的变化和发展。","date": "2019-05-28 00:10:20","age": 21,"is_vip": false,"range_price": {"gte": 30,"lte": 80},"range_date": {"gte": "2019-05-01 00:10:20","lte": "2019-05-28 00:10:20"},"range_ip": "192.168.0.1/16","tags": {"name": "的卢马","is_show": true,"sort": 1},"nodes": {"name": "刘备","sex": "男","age": 21},"ip": "192.168.1.11","location": {"lat": 51.12,"lon": -81.34}}
{"index":{}}
{"title":"变改给大家带来了什么","content": "迎来了新的变化和发展。","date": "2018-05-28 00:10:20","age": 21,"is_vip": false,"range_price": {"gte": 40,"lte": 80},"range_date": {"gte": "2019-05-01 00:10:20","lte": "2019-05-28 00:10:20"},"range_ip": "192.168.0.2/16","tags": {"name": "的卢马","is_show": true,"sort": 1},"nodes": {"name": "赵云","sex": "男","age": 18},"ip": "192.168.1.12","location": {"lat": 61.12,"lon": -91.34}}
{"index":{}}
{"title":"变改给大家带来了什么","content": "迎来了新的变化和发展。","date": "2017-05-28 00:10:20","age": 21,"is_vip": false,"range_price": {"gte": 50,"lte": 80},"range_date": {"gte": "2019-05-01 00:10:20","lte": "2019-05-28 00:10:20"},"range_ip": "192.168.0.3/16","tags": {"name": "的卢马","is_show": true,"sort": 1},"nodes": {"name": "马超","sex": "男","age": 19},"ip": "192.168.1.13","location": {"lat": 71.12,"lon": -101.34}}
{"index":{}}
{"title":"变改给大家带来了什么","content": "迎来了新的变化和发展。","date": "2016-05-28 00:10:20","age": 21,"is_vip": false,"range_price": {"gte": 60,"lte": 80},"range_date": {"gte": "2019-05-01 00:10:20","lte": "2019-05-28 00:10:20"},"range_ip": "192.168.0.4/16","tags": {"name": "的卢马","is_show": true,"sort": 1},"nodes": {"name": "黄忠","sex": "男","age": 31},"ip": "192.168.1.14","location": {"lat": 81.12,"lon": -111.34}}


GET /demo/doc/_bulk
{"index":{}}
{"title":"科技发展给大家带来了什么","content": "科技的发展和进步","date": "2019-05-28 00:10:20","age": 21,"is_vip": false,"range_price": {"gte": 30,"lte": 80},"range_date": {"gte": "2019-05-01 00:10:20","lte": "2019-05-28 00:10:20"},"range_ip": "192.168.0.1/16","tags": {"name": "的卢马","is_show": true,"sort": 1},"nodes": {"name": "刘备","sex": "男","age": 21},"ip": "192.168.1.11","location": {"lat": 51.12,"lon": -81.34}}
{"index":{}}
{"title":"科技发展给大家带来了什么","content": "科技的发展和进步","date": "2018-05-28 00:10:20","age": 21,"is_vip": false,"range_price": {"gte": 40,"lte": 80},"range_date": {"gte": "2019-05-01 00:10:20","lte": "2019-05-28 00:10:20"},"range_ip": "192.168.0.2/16","tags": {"name": "的卢马","is_show": true,"sort": 1},"nodes": {"name": "赵云","sex": "男","age": 18},"ip": "192.168.1.12","location": {"lat": 61.12,"lon": -91.34}}
{"index":{}}
{"title":"科技发展给大家带来了什么","content": "科技的发展和进步","date": "2017-05-28 00:10:20","age": 21,"is_vip": false,"range_price": {"gte": 50,"lte": 80},"range_date": {"gte": "2019-05-01 00:10:20","lte": "2019-05-28 00:10:20"},"range_ip": "192.168.0.3/16","tags": {"name": "的卢马","is_show": true,"sort": 1},"nodes": {"name": "马超","sex": "男","age": 19},"ip": "192.168.1.13","location": {"lat": 71.12,"lon": -101.34}}
{"index":{}}
{"title":"科技发展给大家带来了什么","content": "科技的发展和进步","date": "2016-05-28 00:10:20","age": 21,"is_vip": false,"range_price": {"gte": 60,"lte": 80},"range_date": {"gte": "2019-05-01 00:10:20","lte": "2019-05-28 00:10:20"},"range_ip": "192.168.0.4/16","tags": {"name": "的卢马","is_show": true,"sort": 1},"nodes": {"name": "黄忠","sex": "男","age": 31},"ip": "192.168.1.14","location": {"lat": 81.12,"lon": -111.34}}
几种类型搜索方式

text搜索,match全文搜索

// highlight高亮显示搜索内容,默认标签为<em>{value}</em>
// from、size分页查询
// sort排序查询
GET /demo/doc/_search?_source
{
    "query" : {
        "match" : {
            "content" : "科技发展"
        }
    },
    "highlight" : {
        "pre_tags" : ["<font color='red'>"],
        "post_tags" : ["</font>"],
        "fields" : {
            "content" : {}
        }
    },
    "from": 0,
    "size": 20,
    "sort": [
    	{
        	"age": {
          		"order": "asc"
        	}
      	}
    ]
}

搜索可以覆盖mappings原先设置好的分词器

GET /demo/doc/_search?_source
{
    "query" : {
        "match" : {
            "content" : {
            	"query": "发展进步",
              	"analyzer": "ik_smart"
            }
        }
    }
}

match_phrase短语搜索
使用match搜索”发展进步“会搜索到包含发展与进步的结果集,使用match_phrase则会匹配到”发展进步“的结果集

GET /demo/doc/_search?_source
{
    "query" : {
        "match_phrase" : {
            "content" : {
            	"query": "发展进步",
              	"analyzer": "ik_smart"
            }
        }
    }
}

keyword搜索,关键字字段不会进行分词。也无法对keyword类型的字段分词搜索
可使用term、terms进行查询

// 匹配搜索
GET /demo/doc/_search?_source
{
    "query" : {
        "match" : {
            "title": "变改给大家带来了什么"
        }
    }
}
// 单个关键字搜索
GET /demo/doc/_search?_source
{
  "query": {
    "term": {
      "title": "变改给大家带来了什么"
    }
  }
}
// 多个关键字搜索
GET /demo/doc/_search?_source
{
  "query": {
    "terms": 
        {
          "title": ["变改给大家带来了什么", "改变"]
        }
  }
}

date搜索

// 范围搜索
GET /demo/doc/_search?_source
{
  	"query": {
    	"bool": {
      		"must": [
        		{"range": {"date": {"gte": "2019-01-01"}}},
        		{"range": {"date": {"lte": "2020-01-01"}}}
      		]
    	}
  	}
}
// 匹配搜索
GET /demo/doc/_search?_source
{
  	"query": {
    	"term": {
      		"date": "2019-05-28 00:10:20"
    	}
  	}
}

boolean搜索

GET /demo/doc/_search?_source
{
  	"query": {
    	"term": {
      		"is_vip": false
    	}
  	}
}

ip搜索

GET /demo/doc/_search?_source
{
  	"query": {
    	"term": {
      		"ip": "192.168.1.11"
    	}
  	}
}

object搜索

// match
GET /demo/doc/_search?_source
{
  	"query": {
    	"bool": {
      		"must": [
        		{"match": {"tags.name": "马"}}
      		]
    	}
  	}
}
// term,term不适用与text类型查找。可以与直觉类型如boolean、date、ip
GET /demo/doc/_search?_source
{
  "query": {
    "term": {
      "tags.is_show": true
    }
  }
}

nested搜索

GET /demo/doc/_search?_source
{
	"query": {
		"nested": {
			"path": "nodes",
			"query": {
				"bool": {
					"must": [{
						"match": {
							"nodes.name": "赵云"
						}
					}]
				}
			}
		}
	}
}

integer_range搜索

GET /demo/doc/_search?_source
{
	"query": {
    	"term": {
      		"range_price": {
        		"value": 20
      		}
    	}
  	}
}

date_range 搜索

GET /demo/doc/_search?_source
{
  	"query": {
   		"term": {
      		"range_date": {
        		"value": "2020-05-01 00:10:20"
      		}
    	}
  	}
}

ip_range搜索

GET /demo/doc/_search?_source
{
  	"query": {
    	"term": {
      		"range_ip": {
        		"value": "192.168.0.1"
      		}
    	}
  	}
}

geo_point搜索

GET /demo/doc/_search?_source
{
  	"query": {
    	"geo_bounding_box": {
      		"location": {
        		"top_left": {
          			"lat": 52,
          			"lon": -82
        		},
        		"bottom_right": {
          			"lat": 50,
          			"lon": -84
        		}
      		}
    	}
  	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值