Elasticsearch 检索
1、 检索所有文档
GET bus/product/_search
2、 term检索
term是代表完全匹配,也就是精确查询,搜索前不会再对搜索词进行分词,所以我们的搜索词必须是文档分词集合中的一个,如果没有安装分词插件,汉字分词按每个汉字来分。
查询不到内容:
GET bus/product/_search
{
"query": {
"term": {
"producer": "公交"
}
}
}
producer中所有带“公”的文档都会被查询出来
GET bus/product/_search
{
"query": {
"term": {
"producer": "公"
}
}
}
3、 match检索
match查询会先对搜索词进行分词,分词完毕后再逐个对分词结果进行匹配,因此相比于term的精确搜索,match是分词匹配搜索
描述中带有机场酒店四个字的各种组合的文档都会被返回
GET bus/product/_search
{
"query": {
"match": {
"desc": "机场酒店"
}
}
}
4、 分页
GET bus/_search
{
"from": 0,
"size": 3,
"query": {
"match": {
"desc": "机场酒店"
}
}
}
GET bus/_search
{
"from": 0,
"size": 5,
"query": {
"match_all": {}
}
}
5、 过滤字段source filtering,类似select a,b from table中a,b。通过_source将缺省返回所有字段进行调整。
5.1 选择字段,只返回name和desc字段
GET bus/_search
{
"from": 0, "size": 1,
"_source": ["name","desc"] ,
"query": {
"match": {
"desc": "机场"
}
}
}
####
{
"took" : 294,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 13,
"max_score" : 1.9804206,
"hits" : [
{
"_index" : "bus",
"_type" : "product",
"_id" : "8",
"_score" : 1.9804206,
"_source" : {
"name" : "机场大巴A2线",
"desc" : "机机场场"
}
}
]
}
}
5.2 禁止_source返回内容
GET /bus/_search { "from": 0, "size": 1, "_source": false, "query": { "match_all": {} } } ##### { "took" : 2, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 27, "max_score" : 1.0, "hits" : [ { "_index" : "bus", "_type" : "product", "_id" : "20", "_score" : 1.0 } ] } }
5.3 支持通配
GET /bus/_search { "from": 0, "size": 1, "_source": "name*", "query": { "match_all": {} } } #### { "took" : 0, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 27, "max_score" : 1.0, "hits" : [ { "_index" : "bus", "_type" : "product", "_id" : "20", "_score" : 1.0, "_source" : { "name" : "空港豪华大巴A2线" } } ] } }
6、 检索结果显示版本
GET bus/_search
{
"version": true,
"from": 0,
"size": 3,
"query": {
"match": {
"desc": "机场酒店"
}
}
}
7、 按评分检索过滤
GET bus/_search
{
"version": true,
"min_score":"2.3", #大于2.3
"from": 0,
"size": 3,
"query": {
"match": {
"desc": "机场酒店"
}
}
}
8、 高亮关键字
GET bus/_search
{
"version": true,
"from": 0,
"size": 3,
"query": {
"match": {
"desc": "机场酒店"
}
}
, "highlight": {
"fields": {
"desc": {}
}
}
}
9、 短语匹配match_phrase
与match query类似,但用于匹配精确短语,分词后所有词项都要出现在该字段中,字段中的词项顺序要一致。
GET bus/_search
{
"query": {
"match_phrase": {
"name": "公交车122"
}
}
}
{
"took" : 4,
"timed_out" : false,
"_shards" : {
"total" : 3,
"successful" : 3,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 3.4102418,
"hits" : [
{
"_index" : "bus",
"_type" : "product",
"_id" : "3",
"_score" : 3.4102418,
"_source" : {
"name" : "公交车122路",
"desc" : "从前兴路枢纽到东站",
"price" : 2,
"producer" : "公交集团",
"tags" : [
"单层",
"空调"
]
}
}
]
}
}
对比match
GET bus/_search
{
"query": {
"match": {
"name": "公交车122"
}
}
}
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 3,
"successful" : 3,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 3,
"max_score" : 5.3417225,
"hits" : [
{
"_index" : "bus",
"_type" : "product",
"_id" : "2",
"_score" : 5.3417225,
"_source" : {
"name" : "公交车5路",
"desc" : "从巫家坝到梁家河",
"price" : 1,
"producer" : "公交集团",
"tags" : [
"双层",
"普通",
"热门"
]
}
},
{
"_index" : "bus",
"_type" : "product",
"_id" : "3",
"_score" : 3.4102418,
"_source" : {
"name" : "公交车122路",
"desc" : "从前兴路枢纽到东站",
"price" : 2,
"producer" : "公交集团",
"tags" : [
"单层",
"空调"
]
}
},
{
"_index" : "bus",
"_type" : "product",
"_id" : "1",
"_score" : 2.1597636,
"_source" : {
"name" : "公交车5路",
"desc" : "从巫家坝到梁家河",
"price" : 1,
"producer" : "公交集团",
"tags" : [
"双层",
"普通",
"热门"
]
}
}
]
}
}
10、短语前缀查询match_phrase_prefix
match_phrase_prefix与match_phrase相同,只是它允许在文本中的最后一个词的前缀匹配
GET bus/_search
{
"query": {
"match_phrase_prefix": {
"name": "公交车1"
}
}
}
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 3,
"successful" : 3,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 6.8204837,
"hits" : [
{
"_index" : "bus",
"_type" : "product",
"_id" : "3",
"_score" : 6.8204837,
"_source" : {
"name" : "公交车122路",
"desc" : "从前兴路枢纽到东站",
"price" : 2,
"producer" : "公交集团",
"tags" : [
"单层",
"空调"
]
}
}
]
}
}
对比:
GET bus/_search
{
"query": {
"match_phrase": {
"name": "公交车1"
}
}
}
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 3,
"successful" : 3,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 0,
"max_score" : null,
"hits" : [ ]
}
}
11、 多字段查询multi_match
GET bus/_search { "query": { "multi_match": { "query": "空港", "fields": ["desc","name"] } } } { "took" : 1, "timed_out" : false, "_shards" : { "total" : 3, "successful" : 3, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 3, "max_score" : 3.6836727, "hits" : [ { "_index" : "bus", "_type" : "product", "_id" : "16", "_score" : 3.6836727, "_source" : { "name" : "机场大巴A2线", "desc" : "空港", "price" : 21, "producer" : "大巴", "tags" : [ "单层", "空调", "大巴" ] } }, { "_index" : "bus", "_type" : "product", "_id" : "18", "_score" : 3.5525968, "_source" : { "name" : "空港大巴A2线", "desc" : "机场", "price" : 21, "producer" : "大巴", "tags" : [ "单层", "空调", "大巴" ] } }, { "_index" : "bus", "_type" : "product", "_id" : "19", "_score" : 3.1757839, "_source" : { "name" : "空港大巴A2线", "desc" : "空港快线", "price" : 21, "producer" : "大巴", "tags" : [ "单层", "空调", "大巴" ] } } ] } }
12、范围检索,range查询用于匹配数值型、日期型或字符串型字段在某一范围内的文档。
GET bus/_search { "from": 0, "size": 10, "query": { "range": { "updateDate": { "gte": "2018-12-01", "lte": "2018-12-31",
"format": "yyyy-MM-dd" } } } } { "took" : 1, "timed_out" : false, "_shards" : { "total" : 3, "successful" : 3, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 2, "max_score" : 1.0, "hits" : [ { "_index" : "bus", "_type" : "product", "_id" : "13", "_score" : 1.0, "_source" : { "name" : "机场大巴A2线", "desc" : "机机场场", "price" : 21, "producer" : "机场大巴", "tags" : [ "单层", "空调", "大巴" ], "updateDate" : "2018-12-20" } }, { "_index" : "bus", "_type" : "product", "_id" : "12", "_score" : 1.0, "_source" : { "name" : "机场大巴A2线", "desc" : "机机场场", "price" : 21, "producer" : "机场大巴", "tags" : [ "单层", "空调", "大巴" ], "updateDate" : "2018-12-01" } } ] } }
可以不加索引,表示搜索所有索引。
GET /_search { "from": 0, "size": 10, "query": { "range": { "updateDate": { "gte": "2018-12-01", "lte": "2018-12-31",
"format": "yyyy-MM-dd" } } } } { "took" : 14, "timed_out" : false, "_shards" : { "total" : 22, "successful" : 20, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 4, "max_score" : 1.0, "hits" : [ { "_index" : "bus", "_type" : "product", "_id" : "13", "_score" : 1.0, "_source" : { "name" : "机场大巴A2线", "desc" : "机机场场", "price" : 21, "producer" : "机场大巴", "tags" : [ "单层", "空调", "大巴" ], "updateDate" : "2018-12-20" } }, { "_index" : "home", "_type" : "product", "_id" : "3", "_score" : 1.0, "_source" : { "title" : "My first post3", "memo" : "a test23", "updateDate" : "2018-12-13" } }, { "_index" : "bus", "_type" : "product", "_id" : "12", "_score" : 1.0, "_source" : { "name" : "机场大巴A2线", "desc" : "机机场场", "price" : 21, "producer" : "机场大巴", "tags" : [ "单层", "空调", "大巴" ], "updateDate" : "2018-12-01" } }, { "_index" : "home", "_type" : "product", "_id" : "1", "_score" : 1.0, "_source" : { "title" : "My first post2", "memo" : "a test2", "updateDate" : "2018-12-12" } } ] } }
13、检索字段或者值是否存在,exists查询
只能返回field字段存在,并且不为null的数据。
GET /_search { "query": { "exists": {"field": "desc1"} } } { "took" : 1, "timed_out" : false, "_shards" : { "total" : 22, "successful" : 20, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 0, "max_score" : null, "hits" : [ ] } }
13.1 返回索引中没有某个字段的记录
GET bus/_search { "from": 0,"size": 1, "query": { "bool": { "must_not": [ { "exists": {"field": "updateDate"} } ] } } } 返回记录中缺少updateDate字段或者值为null的 { "took" : 1, "timed_out" : false, "_shards" : { "total" : 3, "successful" : 3, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 7, "max_score" : 1.0, "hits" : [ { "_index" : "bus", "_type" : "product", "_id" : "2", "_score" : 1.0, "_source" : { "name" : "公交车5路", "desc" : "从巫家坝到梁家河", "price" : 1, "producer" : "公交集团", "tags" : [ "双层", "普通", "热门" ] } } ] } }
13.2 返回存在字段,并且不能为null
GET bus/_search { "from": 0,"size": 1, "query": { "exists": {"field": "updateDate"} } } 或 GET bus/_search { "from": 0,"size": 1, "query": { "bool": { "must": [ {"exists": {"field": "updateDate"}} ] } } } 返回记录中有updateDate: { "took" : 3, "timed_out" : false, "_shards" : { "total" : 3, "successful" : 3, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 20, "max_score" : 1.0, "hits" : [ { "_index" : "bus", "_type" : "product", "_id" : "20", "_score" : 1.0, "_source" : { "name" : "空港豪华大巴A2线", "desc" : "空港快线", "price" : 35, "producer" : "大巴", "tags" : [ "单层", "空调", "大巴", "豪华" ], "updateDate" : "2018-07-20" } } ] } }
14、prefix query前缀查询,Matches documents that have fields containing terms with a specified prefix (not analyzed)
GET _search { "from": 0, "size": 2, "query": { "prefix": { "name": "公" #根据分词 } } } { "took" : 4, "timed_out" : false, "_shards" : { "total" : 22, "successful" : 20, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 3, "max_score" : 1.0, "hits" : [ { "_index" : "bus", "_type" : "product", "_id" : "2", "_score" : 1.0, "_source" : { "name" : "公交车5路", "desc" : "从巫家坝到梁家河", "price" : 1, "producer" : "公交集团", "tags" : [ "双层", "普通", "热门" ], "updateDate" : "2018-03-01" } }, { "_index" : "bus", "_type" : "product", "_id" : "1", "_score" : 1.0, "_source" : { "name" : "公交车5路", "desc" : "从巫家坝到梁家河", "price" : 1, "producer" : "公交集团", "tags" : [ "双层", "普通", "热门" ], "updateDate" : "2018-02-01" } } ] } }
15、wildcard查询(通配符查询)
支持*和?通配查询.
GET /_search { "query": { "wildcard" : { "user" : "ki*y" } } }
16、regexp查询(正则表达式查询)
https://www.elastic.co/guide/en/elasticsearch/reference//6.5/query-dsl-regexp-query.html
17、模糊查询fuzzy query
https://www.elastic.co/guide/en/elasticsearch/reference/6.5/query-dsl-fuzzy-query.html
18、ids查询
GET /_search { "query": { "ids": { "type": "product", "values": [1,2] } } }
19、排序
19.1 常规排序
GET bus/_search { "from": 0, "size": 50, "query": { "match_all": {} }, "sort": [ { "updateDate": { "order": "desc" } }, { "price": { "order": "desc" } } ] }
19.2 字段多值情况下排序,在字段内容是数组的情况下,排序可以指定按数组的min、max、avg、sum、median等聚合方式来排序
先把id为13的文档字段updateDate更新成数组 POST /bus/product/13/_update { "doc": {"updateDate":["2018-12-20","2018-01-01"]} } 如果是多字段,按最小字段排序 GET /bus/_search { "from": 0, "size": 100, "query": { "match_all": {} }, "sort": [ { "updateDate": { "order": "desc", "mode": "min" } } ] } 返回略:排序按数组中"2018-01-01"来排序
19.3 无值字段排序,在某些记录没有排序的字段或者字段为null
GET /_search { "sort" : [ { "price" : {"missing" : "_last"} } ], "query" : { "term" : { "product" : "chocolate" } } } 缺省情况下是_last排序,可以选择_first排序
19.4 经纬度排序
下例中:pin.location
是一个经纬度字段(geo_point)
GET /_search { "sort" : [ { "_geo_distance" : { "pin.location" : [-70, 40], "order" : "asc", "unit" : "km", "mode" : "min", "distance_type" : "arc", "ignore_unmapped": true } } ], "query" : { "term" : { "user" : "kimchy" } } }
distance_type:计算距离,arc(缺省)或者plane(快,但是不准确)
20、格式化字段返回值,一般用于时间和数字。
GET /bus/_search { "from": 0, "size": 1, "query": { "match_all": {} }, "docvalue_fields": [ { "field":"updateDate", "format":"yyyy-MM-dd hh:mm:ss" }, { "field":"price", "format":"0.00" } ] }
{ "took" : 0, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 27, "max_score" : 1.0, "hits" : [ { "_index" : "bus", "_type" : "product", "_id" : "20", "_score" : 1.0, "_source" : { "name" : "空港豪华大巴A2线", "desc" : "空港快线", "price" : 35, "producer" : "大巴", "tags" : [ "单层", "空调", "大巴", "豪华" ], "updateDate" : "2018-07-20" }, "fields" : { "updateDate" : [ "2018-07-20 12:00:00" ], "price" : [ "35.00" ] } } ] } }
21、多条件检索记录filter
返回名字中包含空港以及价格小于等于21的记录 GET /bus/_search { "from": 0,"size": 1, "query": { "bool": { "filter": [ {"match":{"name":"空港"}}, {"range":{"price":{"lte":21}}} ] } } } 返回: { "took" : 1, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 1, "max_score" : 0.0, "hits" : [ { "_index" : "shirts", "_type" : "_doc", "_id" : "1", "_score" : 0.0, "_source" : { "brand" : "gucci", "color" : "red", "model" : "slim" } } ] } }
22、返回检索计数
GET /bus/_count { "query": { "match": { "name": "空港" } } } { "count" : 9, "_shards" : { "total" : 3, "successful" : 3, "skipped" : 0, "failed" : 0 } }
23、解释api, explain api ,用于分析检索语句是否命中
GET /bus/product/26/_explain { "query": { "match": { "name":{ "operator": "and", "query": "空港" } } } }