ES中提供了一种强大的检索数据方式,这种检索方式称之为Query DSL(Domain Specified Language)

Query DSL是利用Rest API传递JSON格式的请求体(RequestBody)数据与ES进行交互,这种方式的丰富查询语法让ES检索变得更强大,更简洁

文档: https://www.elastic.co/guide/en/elasticsearch/reference/7.17/query-dsl.html

GET /es_db/_doc/_search {json请求体数据}
OR
GET /es_db/_search {json请求体数据}

demo

get /es_test/_search
{"query": {"match_all": {}},"from": 0,"size": 10000,"sort": [{"_id": "desc"}]}

DSL

  • 查询所有match_all

    • 返回指定条数size
    • 分页查询form
    • 深分页查询Scroll
    • 指定字段排序sort
    • 返回指定字段_source
  • 默认OR匹配所有match
  • 短语查询match_phrase — slop
  • 多字段查询multi_match
  • query_string
    • simple_query_string
  • 关键词查询Term
  • 前缀查询prefix
  • 通配符查询wildcard
  • 范围查询range
    • 日期range
  • 多id查询ids
  • 模糊查询fuzzy
  • 高亮highlight
    • 自定义高亮html标签
    • 多字段高亮

查询所有match_all

1.查询所有match_all

使用match_all,默认只会返回10条数据。
原因:_search查询默认采用的是分页查询,每页记录数size的默认值为10。如果想显示更多数据,指定size

2.返回指定条数size (默认10,最大(from+size)<=10000)

size 关键字: 指定查询结果中返回指定条数。默认返回值10条

get /es_test/_search
{"query": {"match_all": {}},"from": 2,"size": 10000,"sort": [{"_id": "desc"}]
}
response
{"error" : {"root_cause" : [{"type" : "illegal_argument_exception","reason" : "Result window is too large, from + size must be less than or equal to: [10000] but was [10002]. See the scroll api for a more efficient way to request large data sets. This limit can be set by changing the [index.max_result_window] index level setting."}],"type" : "search_phase_execution_exception","reason" : "all shards failed","phase" : "query","grouped" : true,"failed_shards" : [{"shard" : 0,"index" : "es_test","node" : "rEYg9XpfS_uCtGpHpeoSCw","reason" : {"type" : "illegal_argument_exception","reason" : "Result window is too large, from + size must be less than or equal to: [10000] but was [10002]. See the scroll api for a more efficient way to request large data sets. This limit can be set by changing the [index.max_result_window] index level setting."}}],"caused_by" : {"type" : "illegal_argument_exception","reason" : "Result window is too large, from + size must be less than or equal to: [10000] but was [10002]. See the scroll api for a more efficient way to request large data sets. This limit can be set by changing the [index.max_result_window] index level setting.","caused_by" : {"type" : "illegal_argument_exception","reason" : "Result window is too large, from + size must be less than or equal to: [10000] but was [10002]. See the scroll api for a more efficient way to request large data sets. This limit can be set by changing the [index.max_result_window] index level setting."}}},"status" : 400
}

异常原因:
1、查询结果的窗口太大,from + size的结果必须小于或等于10000,而当前查询结果的窗口为20000。
2、可以采用scroll api更高效的请求大量数据集。
3、查询结果的窗口的限制可以通过参数index.max_result_window进行设置。

注意:参数index.max_result_window主要用来限制单次查询满足查询条件的结果窗口的大小,窗口大小由from + size共同决定。不能简单理解成查询返回给调用方的数据量。这样做主要是为了限制内存的消耗。

比如:from为1000000,size为10,逻辑意义是从满足条件的数据中取1000000到(1000000 + 10)的记录。这时ES一定要先将(1000000 + 10)的记录(即result_window)加载到内存中,再进行分页取值的操作。尽管最后我们只取了10条数据返回给客户端,但ES进程执行查询操作的过程中确需要将(1000000 + 10)的记录都加载到内存中,可想而知对内存的消耗有多大。这也是ES中不推荐采用(from + size)方式进行深度分页的原因。

同理,from为0,size为1000000时,ES进程执行查询操作的过程中确需要将1000000 条记录都加载到内存中再返回给调用方,也会对ES内存造成很大压力。

PUT /es_test/_settings
{ "index.max_result_window" :"20000"
}
#修改现有所有的索引,但新增的索引,还是默认的10000
PUT /_all/_settings
{ "index.max_result_window" :"20000"
}#查看所有索引中的index.max_result_window值
GET /_all/_settings/index.max_result_window
{"es_test" : {"settings" : {"index" : {"max_result_window" : "20000"}}},"test" : {"settings" : {"index" : {"max_result_window" : "10000"}}}
}

3.分页查询form

from 关键字: 用来指定起始返回位置,和size关键字连用可实现分页效果

get /es_test/_search
{"from": 2,"size": 10000
}

4.深分页查询Scroll

改动index.max_result_window参数值的大小,只能解决一时的问题,当索引的数据量持续增长时,在查询全量数据时还是会出现问题。而且会增加ES服务器内存大结果集消耗完的风险。最佳实践还是根据异常提示中的采用scroll api更高效的请求大量数据集。

查询命令中新增scroll=1m,说明采用游标查询,保持游标查询窗口一分钟
这里由于测试数据量不够,所以size值设置为2
实际使用中为了减少游标查询的次数,可以将值适当增大,比如设置为1000

查询结果:
除了返回前2条记录,还返回了一个游标ID值_scroll_id

多次根据scroll_id游标查询,直到没有数据返回则结束查询。采用游标查询索引全量数据,更安全高效,限制了单次对内存的消耗。

默认为500 高并发下会报错:
elasticsearch7.x max_open_scroll_context Trying to create too many scroll contexts. Must be less than or equal to 500

GET /es_test/_search?scroll=1m
{"query": { "match_all": {}},"size":  2
}response{"_scroll_id" : "FGluY2x1ZGVfY29udGV4dF91dWlkDXF1ZXJ5QW5kRmV0Y2gBFm13d0dUNVRzUS1HeUlaU3lSTVhLLUEAAAAAAAJyuxZyRVlnOVhwZlNfdUN0R3BIcGVvU0N3","took" : 0,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 8,"relation" : "eq"},"max_score" : 1.0,"hits" : [{"_index" : "es_test","_type" : "_doc","_id" : "2","_score" : 1.0,"_source" : {"name" : "李四","sex" : 1,"age" : 28,"address" : "广州荔湾大厦","remark" : "java assistant"}},{"_index" : "es_test","_type" : "_doc","_id" : "3","_score" : 1.0,"_source" : {"name" : "王五","sex" : 0,"age" : 26,"address" : "广州白云山公园","remark" : "php developer"}}]}
}GET /_search/scroll
{"scroll": "1m", "scroll_id" : "FGluY2x1ZGVfY29udGV4dF91dWlkDXF1ZXJ5QW5kRmV0Y2gBFm13d0dUNVRzUS1HeUlaU3lSTVhLLUEAAAAAAAJyuxZyRVlnOVhwZlNfdUN0R3BIcGVvU0N3"
}

5.指定字段排序sort

注意:会让得分失效

#排序,分页

GET /es_test/_search
{"query": {"match_all": {}},"sort": [{"age": "desc"}],"from": 10,"size": 5
}

6.返回指定字段_source

_source 关键字: 是一个数组,在数组中用来指定展示那些字段

GET /es_test/_search
{"query": {"match_all": {}},"_source": ["name","address"]
}

默认OR匹配所有match

match 在匹配时会对所查找的关键词进行分词,然后按分词匹配查找

match支持以下参数:

  • query : 指定匹配的值
  • operator : 匹配条件类型
    • and : 条件分词后都要匹配
    • or : 条件分词后有一个匹配即可(默认)
  • minmum_should_match : 最低匹配度,即条件在倒排索引中最低的匹配度
#模糊匹配 match 分词后or的效果
GET /es_test/_search
{"query": {"match": {"address": "广州白云山公园"}}
}
# 分词后 and的效果
GET /es_test/_search
{"query": {"match": {"address": {"query": "广州白云山公园","operator": "AND"}}}
}

在match中的应用: 当operator参数设置为or时,minnum_should_match参数用来控制匹配的分词的最少数量。

# 最少匹配广州,公园两个词
GET /es_test/_search
{"query": {"match": {"address": {"query": "广州公园","minimum_should_match": 2}}}
}

短语查询match_phrase

match_phrase查询分析文本并根据分析的文本创建一个短语查询。match_phrase会将检索关键词分词

match_phrase的分词结果必须在被检索字段的分词中都包含,而且顺序必须相同,而且默认必须都是连续的

GET /es_test/_search
{"query": {"match_phrase": {"address": "广州白云山"}}
}
GET /es_test/_search
{"query": {"match_phrase": {"address": "广州白云"}}
}

思考:为什么查询广州白云山有数据,广州白云没有数据?

分析原因:

先查看广州白云山公园分词结果,可以知道广州和白云不是相邻的词条,中间会隔一个白云山,而match_phrase匹配的是相邻的词条,所以查询广州白云山有结果,但查询广州白云没有结果。

POST _analyze
{"analyzer":"ik_max_word","text":"广州白云山"
}
response
{"tokens" : [{"token" : "广州","start_offset" : 0,"end_offset" : 2,"type" : "CN_WORD","position" : 0},{"token" : "白云山","start_offset" : 2,"end_offset" : 5,"type" : "CN_WORD","position" : 1},{"token" : "白云","start_offset" : 2,"end_offset" : 4,"type" : "CN_WORD","position" : 2},{"token" : "云山","start_offset" : 3,"end_offset" : 5,"type" : "CN_WORD","position" : 3}]
}

如何解决词条间隔的问题?可以借助slop参数,slop参数告诉match_phrase查询词条能够相隔多远时仍然将文档视为匹配

#广州云山分词后相隔为2,可以匹配到结果
GET /es_test/_search
{"query": {"match_phrase": {"address": {"query": "广州云山","slop": 2} }}
}

多字段查询multi_match

可以根据字段类型,决定是否使用分词查询,得分最高的在前面

注意:字段类型分词,将查询条件分词之后进行查询,如果该字段不分词就会将查询条件作为整体进行查询

GET /es_test/_search
{"query": {"multi_match": {"query": "长沙万里","fields": ["address","name"]}}
}

最佳匹配

GET /es_test/_search
{"query": {"multi_match": {"type": "best_fields", //most_fields"query": "长沙万里","fields": ["address","name"]}}
}

query_string (AND | OR | NOT)

允许我们在单个查询字符串中指定AND | OR | NOT条件,同时也和 multi_match query一样,支持多字段搜索。

和match类似,但是match需要指定字段名,query_string是在所有字段中搜索,范围更广泛。

注意: 查询字段分词就将查询条件分词查询,查询字段不分词将查询条件不分词查询

未指定字段查询

GET /es_test/_search
{"query": {"query_string": {"query": "张三 OR 橘子洲"}}
}

指定单个字段查询

#Query String
GET /es_test/_search
{"query": {"query_string": {"default_field": "address","query": "白云山 OR 橘子洲"}}
}

指定多个字段查询

GET /es_test/_search
{"query": {"query_string": {"fields": ["name","address"],"query": "(张三 OR (广州 AND 王五)) NOT 天河"}}
}

simple_query_string

类似Query String,但是会忽略错误的语法,同时只支持部分查询语法,不支持AND OR NOT,会当作字符串处理。支持部分逻辑:

  • + 替代AND
  • | 替代OR
  • - 替代NOT

simple_query_string 默认的operator是OR

GET /es_test/_search
{"query": {"simple_query_string": {"fields": ["name","address"],"query": "广州公园","default_operator": "AND"}}
}GET /es_test/_search
{"query": {"simple_query_string": {"fields": ["name","address"],"query": "广州 + 公园"}}
}

关键词查询Term

Term用来使用关键词查询(精确匹配),还可以用来查询没有被进行分词的数据类型。Term是表达语意的最小单位,搜索和利用统计语言模型进行自然语言处理都需要处理Term。

match在匹配时会对所查找的关键词进行分词,然后按分词匹配查找,而term会直接对关键词进行查找。

一般模糊查找的时候,多用match,而精确查找时可以使用term。

  • ES中默认使用分词器为标准分词器(StandardAnalyzer),标准分词器对于英文单词分词,对于中文单字分词。

  • 在ES的Mapping Type 中 keyword , date ,integer, long , double , boolean or ip 这些类型不分词,只有text类型分词。

# 思考: 查询广州白云是否有数据,为什么?
GET /es_test/_search
{"query":{"term": {"address": {"value": "广州白云"}}}
}# 采用term精确查询, 查询字段映射类型为keyword
GET /es_test/_search
{"query":{"term": {"address.keyword": {"value": "广州白云山公园"}}}
}

对于英文,可以考虑建立索引时忽略大小写

在ES中,Term查询,对输入不做分词。会将输入作为一个整体,在倒排索引中查找准确的词项,并且使用相关度算分公式为每个包含该词项的文档进行相关度算分。

PUT /es_good/_bulk
{"index":{"_id":1}}
{"productId":"xxx123","productName":"iPhone"}
{"index":{"_id":2}}
{"productId":"xxx111","productName":"iPad"}# 思考: 查询iPhone可以查到数据吗?
GET /es_good/_search
{"query":{"term": {"productName": {"value": "iPhone"}}}
}GET /es_good/_analyze
{"analyzer":"standard","text":"iPhone"
}
response
{"tokens" : [{"token" : "iphone","start_offset" : 0,"end_offset" : 6,"type" : "<ALPHANUM>","position" : 0}]
}#1. 使用keyword搜索
get /es_good/_search
{"query": {"term": {"productName.keyword": {"value": "iPhone"}}}
}#2. 对于英文,可以考虑建立索引时忽略大小写
PUT /es_good
{"settings": {"analysis": {"normalizer": {"es_normalizer": {"filter": ["lowercase","asciifolding"],"type": "custom"}}}},"mappings": {"properties": {"productId": {"type": "text"},"productName": {"type": "keyword","normalizer": "es_normalizer","index": "true"}}}
}

Constant Score 避免算分

可以通过 Constant Score 将查询转换成一个 Filtering,避免算分,并利用缓存,提高性能。

将 Query 转成 Filter,忽略TF-IDF计算,避免相关性算分的开销

Filter可以有效利用缓存

GET /es_test/_search
{"query": {"constant_score": {"filter": {"term": {"address.keyword": "广州白云山公园"}}}}
}

ES中的结构化搜索

结构化搜索(Structured search)是指对结构化数据的搜索。

结构化数据:

  • 日期,布尔类型和数字都是结构化的
  • 文本也可以是结构化的。
    如彩色笔可以有离散的颜色集合:红(red) 、绿(green、蓝(blue)
    一个博客可能被标记了标签,例如,分布式(distributed)和搜索(search)
    电商网站上的商品都有UPC(通用产品码Universal Product Code)或其他的唯一标识,它们都需要遵从严格规定的、结构化的格式。

应用场景:对bool,日期,数字,结构化的文本可以利用term做精确匹配

GET /es_test/_search
{"query": {"term": {"age": {"value": 28}}}
}

term处理多值字段,term查询是包含,不是等于

POST /es_employee/_bulk
{"index":{"_id":1}}
{"name":"小明","interest":["跑步","篮球"]}
{"index":{"_id":2}}
{"name":"小红","interest":["跳舞","画画"]}
{"index":{"_id":3}}
{"name":"小丽","interest":["跳舞","唱歌","跑步"]}POST /es_employee/_search
{"query": {"term": {"interest.keyword": {"value": "跑步"}}}
}

前缀查询prefix

它会对分词后的term进行前缀搜索。

它不会分析要搜索字符串,传入的前缀就是想要查找的前缀

默认状态下,前缀查询不做相关度分数计算,它只是将所有匹配的文档返回,然后赋予所有相关分数值为1。它的行为更像是一个过滤器而不是查询。两者实际的区别就是过滤器是可以被缓存的,而前缀查询不行

prefix的原理:需要遍历所有倒排索引,并比较每个term是否已所指定的前缀开头

过程: 所有倒排索引 广州都查出来

GET /es_test/_search
{"query": {"prefix": {"address": {"value": "广州"}}}
}

通配符查询wildcard

通配符查询:工作原理和prefix相同,只不过它不是只比较开头,它能支持更为复杂的匹配模式。

GET /es_test/_search
{"query": {"wildcard": {"address": {"value": "*白*"}}}
}

范围查询range

  • range:范围关键字
  • gte 大于等于
  • lte 小于等于
  • gt 大于
  • lt 小于
  • now 当前时间
POST /es_db/_search
{"query": {"range": {"age": {"gte": 25,"lte": 28}}}
}

日期range

DELETE /es_good
POST /es_good/_bulk
{"index":{"_id":1}}
{"price":100,"date":"2021-01-01","productId":"XHDK-1293"}
{"index":{"_id":2}}
{"price":200,"date":"2022-01-01","productId":"KDKE-5421"}GET /es_good/_mappingGET /es_good/_search
{"query": {"range": {"date": {"gte": "now-2y"}}}
}

多id查询ids

ids 关键字: 值为数组类型,用来根据一组id获取多个对应的文档

GET /es_test/_search
{"query": {"ids": {"values": [1,2]}}
}

或者批量查询

GET /es_test/_mget
{"ids": [1,2]
}

模糊查询fuzzy

在实际的搜索中,我们有时候会打错字,从而导致搜索不到。在Elasticsearch中,我们可以使用fuzziness属性来进行模糊查询,从而达到搜索有错别字的情形。

fuzzy 查询会用到两个很重要的参数,fuzziness,prefix_length

  1. fuzziness:表示输入的关键字通过几次操作可以转变成为ES库里面的对应field的字段
  • 操作是指:新增一个字符,删除一个字符,修改一个字符,每次操作可以记做编辑距离为1,
  • 如中文集团到中威集团编辑距离就是1,只需要修改一个字符;
  • 该参数默认值为0,即不开启模糊查询。
  • 如果fuzziness值在这里设置成2,会把编辑距离为2的东东集团也查出来。
  1. prefix_length:表示限制输入关键字和ES对应查询field的内容开头的第n个字符必须完全匹配,不允许错别字匹配
  • 如这里等于1,则表示开头的字必须匹配,不匹配则不返回
  • 默认值也是0
  • 加大prefix_length的值可以提高效率和准确率。

注意: fuzzy 模糊查询 最大模糊错误 必须在0-2之间
搜索关键词长度为 2,不允许存在模糊
搜索关键词长度为3-5,允许1次模糊
搜索关键词长度大于5,允许最大2次模糊

GET /es_test/_search
{"query": {"fuzzy": {"address": {"value": "白运山","fuzziness": 1    }}}
}GET /es_test/_search
{"query": {"match": {"address": {"query": "广洲","fuzziness": 1}}}
}

高亮highlight

highlight 关键字: 可以让符合条件的文档中的关键词高亮。

highlight相关属性:

  • pre_tags 前缀标签
  • post_tags 后缀标签
  • tags_schema 设置为styled可以使用内置高亮样式
  • require_field_match 多字段高亮需要设置为false
#指定ik分词器
PUT /es_goods
{"settings" : {"index" : {"analysis.analyzer.default.type": "ik_max_word"}}
}PUT /es_goods/_doc/1
{"proId" : "2","name" : "牛仔男外套","desc" : "牛仔外套男装春季衣服男春装夹克修身休闲男生潮牌工装潮流头号青年春秋棒球服男 7705浅蓝常规 XL","timestamp" : 1576313264451,"createTime" : "2019-12-13 12:56:56"
}PUT /es_goods/_doc/2
{"proId" : "6","name" : "HLA海澜之家牛仔裤男","desc" : "HLA海澜之家牛仔裤男2019时尚有型舒适HKNAD3E109A 牛仔蓝(A9)175/82A(32)","timestamp" : 1576314265571,"createTime" : "2019-12-18 15:56:56"
}
GET /es_goods/_search
{"query": {"term": {"name": {"value": "牛仔"}}},"highlight": {"fields": {"*":{}}}
}

response—highlight

{"took" : 53,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 2,"relation" : "eq"},"max_score" : 0.22396864,"hits" : [{"_index" : "es_goods","_type" : "_doc","_id" : "1","_score" : 0.22396864,"_source" : {"proId" : "2","name" : "牛仔男外套","desc" : "牛仔外套男装春季衣服男春装夹克修身休闲男生潮牌工装潮流头号青年春秋棒球服男 7705浅蓝常规 XL","timestamp" : 1576313264451,"createTime" : "2019-12-13 12:56:56"},"highlight" : {"name" : ["<em>牛仔</em>男外套"]}},{"_index" : "es_goods","_type" : "_doc","_id" : "2","_score" : 0.15373456,"_source" : {"proId" : "6","name" : "HLA海澜之家牛仔裤男","desc" : "HLA海澜之家牛仔裤男2019时尚有型舒适HKNAD3E109A 牛仔蓝(A9)175/82A(32)","timestamp" : 1576314265571,"createTime" : "2019-12-18 15:56:56"},"highlight" : {"name" : ["HLA海澜之家<em>牛仔</em>裤男"]}}]}
}

自定义高亮html标签

可以在highlight中使用pre_tags和post_tags

GET /es_goods/_search
{"query": {"term": {"name": {"value": "牛仔"}}},"highlight": {"post_tags": ["</span>"], "pre_tags": ["<span style='color:red'>"],"fields": {"*":{}}}
}

多字段高亮

GET /products/_search
{"query": {"term": {"name": {"value": "牛仔"}}},"highlight": {"pre_tags": ["<font color='red'>"],"post_tags": ["<font/>"],"require_field_match": "false","fields": {"name": {},"desc": {}}}
}

实战商品搜索

创建索引库

PUT product_db
{"mappings": {"properties": {"id": {"type": "long"},"name": {"type": "text","analyzer": "ik_max_word"},"keywords": {"type": "text","analyzer": "ik_max_word"},"subTitle": {"type": "text","analyzer": "ik_max_word"},"salecount":{"type": "long"},"putawayDate":{"type": "date"},"price": {"type": "keyword"},"promotionPrice": {"type": "keyword"},"originalPrice": {"type": "keyword"},"pic": {"type": "keyword"},"sale": {"type": "long"},"hasStock": {"type": "boolean"},"brandId": {"type": "long"},"brandName": {"type": "keyword"},"brandImg": {"type": "keyword"},"categoryId": {"type": "long"},"categoryName": {"type": "keyword"},"attrs": {"type": "nested","properties": {"attrId": {"type": "long"},"attrName": {"type": "keyword"},"attrValue": {"type": "keyword"}}}}}
}

添加数据

PUT /product_db/_doc/1
{"id": "26","name": "小米 11 手机","keywords": "小米手机","subTitle": "AI智慧全面屏 6GB +64GB 亮黑色 全网通版 移动联通电信4G手机 双卡双待 双卡双待","price": "3999","promotionPrice": "2999","originalPrice": "5999","pic": "http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20180615/xiaomi.jpg","sale": 999,"hasStock": true,"salecount":999,"putawayDate":"2021-04-01","brandId": 6,"brandName": "小米","brandImg": "http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20190129/1e34aef2a409119018a4c6258e39ecfb_222_222.png","categoryId": 19,"categoryName": "手机通讯","attrs": [{"attrId": 1,"attrName": "cpu","attrValue": "2核"},{"attrId": 2,"attrName": "颜色","attrValue": "黑色"}]
}PUT /product_db/_doc/2
{"id": "27","name": "小米 10 手机","keywords": "小米手机","subTitle": "AI智慧全面屏 4GB +64GB 亮白色 全网通版 移动联通电信4G手机 双卡双待 双卡双待","price": "2999","promotionPrice": "1999","originalPrice": "3999","pic": "http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20180615/xiaomi.jpg","sale": 999,"hasStock": false,"salecount":99,"putawayDate":"2021-04-02","brandId": 6,"brandName": "小米","brandImg": "http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20190129/1e34aef2a409119018a4c6258e39ecfb_222_222.png","categoryId": 19,"categoryName": "手机通讯","attrs": [{"attrId": 1,"attrName": "cpu","attrValue": "4核"},{"attrId": 2,"attrName": "颜色","attrValue": "白色"}]
}
PUT /product_db/_doc/3
{"id": "28","name": "小米  手机","keywords": "小米手机","subTitle": "AI智慧全面屏 4GB +64GB 亮蓝色 全网通版 移动联通电信4G手机 双卡双待 双卡双待","price": "2999","promotionPrice": "1999","originalPrice": "3999","pic": "http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20180615/xiaomi.jpg","sale": 999,"hasStock": true,"salecount":199,"putawayDate":"2021-04-03","brandId": 6,"brandName": "小米","brandImg": "http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20190129/1e34aef2a409119018a4c6258e39ecfb_222_222.png","categoryId": 19,"categoryName": "手机通讯","attrs": [{"attrId": 1,"attrName": "cpu","attrValue": "2核"},{"attrId": 2,"attrName": "颜色","attrValue": "蓝色"}]
}
PUT /product_db/_doc/4
{"id": "29","name": "Apple iPhone 8 Plus 64GB 金色特别版 移动联通电信4G手机","keywords": "苹果手机","subTitle": "苹果手机 Apple产品年中狂欢节,好物尽享,美在智慧!速来 >> 勾选[保障服务][原厂保2年],获得AppleCare+全方位服务计划,原厂延保售后无忧。","price": "5999","promotionPrice": "4999","originalPrice": "7999","pic": "http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20180615/5acc5248N6a5f81cd.jpg","sale": 999,"hasStock": true,"salecount":1199,"putawayDate":"2021-04-04","brandId": 51,"brandName": "苹果","brandImg": "http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20180607/timg.jpg","categoryId": 19,"categoryName": "手机通讯","attrs": [{"attrId": 1,"attrName": "cpu","attrValue": "4核"},{"attrId": 2,"attrName": "颜色","attrValue": "金色"}]
}
PUT /product_db/_doc/5
{"id": "30","name": "HLA海澜之家简约动物印花短袖T恤","keywords": "海澜之家衣服","subTitle": "HLA海澜之家短袖T恤","price": "199","promotionPrice": "99","originalPrice": "299","pic": "http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20180615/5ad83a4fN6ff67ecd.jpg!cc_350x449.jpg","sale": 999,"hasStock": true,"salecount":19,"putawayDate":"2021-04-05","brandId": 50,"brandName": "海澜之家","brandImg": "http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20190129/99d3279f1029d32b929343b09d3c72de_222_222.jpg","categoryId": 8,"categoryName": "T恤","attrs": [{"attrId": 1,"attrName": "尺寸","attrValue": "M"},{"attrId": 2,"attrName": "颜色","attrValue": "黑色"}]
}
PUT /product_db/_doc/6
{"id": "31","name": "HLA海澜之家蓝灰花纹圆领针织布短袖T恤","keywords": "海澜之家衣服","subTitle": "HLA海澜之家短袖T恤","price": "299","promotionPrice": "199","originalPrice": "299","pic": "http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20180615/5ac98b64N70acd82f.jpg!cc_350x449.jpg","sale": 999,"hasStock": true,"salecount":399,"putawayDate":"2021-04-06","brandId": 50,"brandName": "海澜之家","brandImg": "http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20190129/99d3279f1029d32b929343b09d3c72de_222_222.jpg","categoryId": 8,"categoryName": "T恤","attrs": [{"attrId": 1,"attrName": "尺寸","attrValue": "X"},{"attrId": 2,"attrName": "颜色","attrValue": "蓝灰"}]
}
PUT /product_db/_doc/7
{"id": "32","name": "HLA海澜之家短袖T恤男基础款","keywords": "海澜之家衣服","subTitle": "HLA海澜之家短袖T恤","price": "269","promotionPrice": "169","originalPrice": "399","pic": "http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20180615/5a51eb88Na4797877.jpg","sale": 999,"hasStock": true,"salecount":399,"putawayDate":"2021-04-07","brandId": 50,"brandName": "海澜之家","brandImg": "http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20190129/99d3279f1029d32b929343b09d3c72de_222_222.jpg","categoryId": 8,"categoryName": "T恤","attrs": [{"attrId": 1,"attrName": "尺寸","attrValue": "L"},{"attrId": 2,"attrName": "颜色","attrValue": "蓝色"}]
}
PUT /product_db/_doc/8
{"id": "33","name": "小米(MI)小米电视4A ","keywords": "小米电视机家用电器","subTitle": "小米(MI)小米电视4A 55英寸 L55M5-AZ/L55M5-AD 2GB+8GB HDR 4K超高清 人工智能网络液晶平板电视","price": "2269","promotionPrice": "2169","originalPrice": "2399","pic": "http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20180615/5b02804dN66004d73.jpg","sale": 999,"hasStock": true,"salecount":132,"putawayDate":"2021-04-09","brandId": 6,"brandName": "小米","brandImg": "http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20190129/1e34aef2a409119018a4c6258e39ecfb_222_222.png","categoryId": 35,"categoryName": "手机数码","attrs": [{"attrId": 1,"attrName": "屏幕尺寸","attrValue": "52"},{"attrId": 2,"attrName": "机身颜色","attrValue": "黑色"}]
}
PUT /product_db/_doc/9
{"id": "34","name": "小米(MI)小米电视4A 65英寸","keywords": "小米电视机家用电器","subTitle": "小米(MI)小米电视4A 65英寸 L55M5-AZ/L55M5-AD 2GB+8GB HDR 4K超高清 人工智能网络液晶平板电视","price": "3269","promotionPrice": "3169","originalPrice": "3399","pic": "http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20180615/5b028530N51eee7d4.jpg","sale": 999,"hasStock": true,"salecount":999,"putawayDate":"2021-04-10","brandId": 6,"brandName": "小米","brandImg": "http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20190129/1e34aef2a409119018a4c6258e39ecfb_222_222.png","categoryId": 35,"categoryName": "手机数码","attrs": [{"attrId": 1,"attrName": "屏幕尺寸","attrValue": "65"},{"attrId": 2,"attrName": "机身颜色","attrValue": "金色"}]
}
PUT /product_db/_doc/10
{"id": "35","name": "耐克NIKE 男子 休闲鞋 ROSHE RUN 运动鞋 511881-010黑色41码","keywords": "耐克运动鞋 鞋子","subTitle": "耐克NIKE 男子 休闲鞋 ROSHE RUN 运动鞋 511881-010黑色41码","price": "569","promotionPrice": "369","originalPrice": "899","pic": "http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20180615/5b235bb9Nf606460b.jpg","sale": 999,"hasStock": true,"salecount":399,"putawayDate":"2021-04-11","brandId": 58,"brandName": "NIKE","brandImg": "http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20180615/timg (51).jpg","categoryId": 29,"categoryName": "男鞋","attrs": [{"attrId": 1,"attrName": "尺码","attrValue": "42"},{"attrId": 2,"attrName": "颜色","attrValue": "黑色"}]
}
PUT /product_db/_doc/11
{"id": "36","name": "耐克NIKE 男子 气垫 休闲鞋 AIR MAX 90 ESSENTIAL 运动鞋 AJ1285-101白色41码","keywords": "耐克运动鞋 鞋子","subTitle": "AIR MAX 90 ESSENTIAL 运动鞋 AJ1285-101白色","price": "769","promotionPrice": "469","originalPrice": "999","pic": "http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20180615/5b19403eN9f0b3cb8.jpg","sale": 999,"hasStock": true,"salecount":499,"putawayDate":"2021-04-13","brandId": 58,"brandName": "NIKE","brandImg": "http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20180615/timg (51).jpg","categoryId": 29,"categoryName": "男鞋","attrs": [{"attrId": 1,"attrName": "尺码","attrValue": "44"},{"attrId": 2,"attrName": "颜色","attrValue": "白色"}]
}
PUT /product_db/_doc/12
{"id": "37","name": "(华为)HUAWEI MateBook X Pro 2019款 13.9英寸3K触控全面屏 轻薄笔记本","keywords": "轻薄笔记本华为 笔记本电脑","subTitle": "轻薄华为笔记本 电脑","price": "4769","promotionPrice": "4469","originalPrice": "4999","pic": "http://tuling-mall.oss-cn-shenzhen.aliyuncs.com/tulingmall/images/20200317/800_800_1555752016264mp.png","sale": 999,"hasStock": true,"salecount":699,"putawayDate":"2021-04-14","brandId": 3,"brandName": "华为","brandImg": "http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20190129/17f2dd9756d9d333bee8e60ce8c03e4c_222_222.jpg","categoryId": 19,"categoryName": "手机通讯","attrs": [{"attrId": 1,"attrName": "容量","attrValue": "16G"},{"attrId": 2,"attrName": "网络","attrValue": "4G"}]
}
PUT /product_db/_doc/13
{"id": "38","name": "华为nova6se 手机 绮境森林 全网通(8G+128G)","keywords": "轻薄笔记本华为 手机","subTitle": "华为nova6se 手机","price": "6769","promotionPrice": "6469","originalPrice": "6999","pic": "http://tuling-mall.oss-cn-shenzhen.aliyuncs.com/tulingmall/images/20200311/78_78_42B09549789695A42D621CF87DC53B5EBE9385772DC61FB9mp.png","sale": 999,"hasStock": true,"salecount":899,"putawayDate":"2021-04-15","brandId": 3,"brandName": "华为","brandImg": "http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20190129/17f2dd9756d9d333bee8e60ce8c03e4c_222_222.jpg","categoryId": 19,"categoryName": "手机通讯","attrs": [{"attrId": 1,"attrName": "容量","attrValue": "64G"},{"attrId": 2,"attrName": "网络","attrValue": "5G"}]
}
PUT /product_db/_doc/14
{"id": "39","name": "iPhone7/6s/8钢化膜苹果8Plus全屏复盖抗蓝光防窥防偷看手机膜","keywords": "手机膜","subTitle": "iPhone7/6s/8钢化膜苹果8Plus全屏复盖抗蓝光防窥防偷看手机膜","price": "29","promotionPrice": "39","originalPrice": "49","pic": "http://tuling-mall.oss-cn-shenzhen.aliyuncs.com/tulingmall/images/20200311/6df99dab78bb2014.jpg","sale": 999,"hasStock": true,"salecount":799,"putawayDate":"2021-04-16","brandId": 51,"brandName": "苹果","brandImg": "http://tuling-mall.oss-cn-shenzhen.aliyuncs.com/tulingmall/images/20200311/2b84746650fc122d67749a876c453619.png","categoryId": 30,"categoryName": "手机配件","attrs": [{"attrId": 1,"attrName": "手机膜-材料","attrValue": "钢化"},{"attrId": 2,"attrName": "手机膜-颜色","attrValue": "白色"}]
}
PUT /product_db/_doc/15
{"id": "40","name": "七匹狼短袖T恤男纯棉舒适春夏修身运动休闲短袖三条装 圆领3条装","keywords": "七匹狼服装 衣服","subTitle": "七匹狼短袖T恤男纯棉舒适春夏修身运动休闲短袖三条装 圆领3条装","price": "129","promotionPrice": "139","originalPrice": "149","pic": "http://tuling-mall.oss-cn-shenzhen.aliyuncs.com/tulingmall/images/20200311/19e846e727dff337.jpg","sale": 999,"hasStock": true,"salecount":199,"putawayDate":"2021-04-20","brandId": 49,"brandName": "七匹狼","brandImg": "http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20190129/18d8bc3eb13533fab466d702a0d3fd1f40345bcd.jpg","categoryId": 8,"categoryName": "T恤","attrs": [{"attrId": 1,"attrName": "尺寸","attrValue": "M"},{"attrId": 2,"attrName": "颜色","attrValue": "白色"},{"attrId": 3,"attrName": "适用季节","attrValue": "春季"}]
}
PUT /product_db/_doc/16
{"id": "41","name": "华为P40 Pro手机","keywords": "华为手机","subTitle": "华为P40 Pro手机","price": "2129","promotionPrice": "2139","originalPrice": "2149","pic": "http://tuling-mall.oss-cn-shenzhen.aliyuncs.com/tulingmall/images/20200327/1_DE7F785A7E0C276D3A1F40A5C6D82B07D2AED60CE1F73795mp.png","sale": 999,"hasStock": true,"salecount":199,"putawayDate":"2021-05-03","brandId": 3,"brandName": "华为","brandImg": "http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20190129/17f2dd9756d9d333bee8e60ce8c03e4c_222_222.jpg","categoryId": 19,"categoryName": "手机通讯","attrs": [{"attrId": 1,"attrName": "容量","attrValue": "128G"},{"attrId": 2,"attrName": "网络","attrValue": "5G"}]
}
PUT /product_db/_doc/17
{"id": "42","name": "朵唯智能手机 4G全网通 老人学生双卡双待手机","keywords": "朵唯手机","subTitle": "朵唯手机后置双摄,国产虎贲芯片!优化散热结构!浅薄机身!朵唯4月特惠!","price": "3129","promotionPrice": "3139","originalPrice": "3249","pic": "http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20180615/xiaomi.jpg","sale": 999,"hasStock": true,"salecount":1199,"putawayDate":"2021-06-01","brandId": 59,"brandName": "朵唯","brandImg": "http://tuling-mall.oss-cn-shenzhen.aliyuncs.com/tulingmall/images/20200311/2b84746650fc122d67749a876c453619.png","categoryId": 19,"categoryName": "手机通讯","attrs": [{"attrId": 1,"attrName": "容量","attrValue": "32G"},{"attrId": 2,"attrName": "网络","attrValue": "4G"}]
}

检索

POST /product_db/_doc/_search
{"from": 0,"size": 8,"query": {"bool": {"must": [{"match": {"name": {"query": "华为手机"}}}]}},"aggregations": {"brand_agg": {"terms": {"field": "brandId","size": 50},"aggregations": {"brand_name_agg": {"terms": {"field": "brandName"}},"brand_img_agg": {"terms": {"field": "brandImg"}}}},"category_agg": {"terms": {"field": "categoryId","size": 50,"min_doc_count": 1},"aggregations": {"category_name_agg": {"terms": {"field": "categoryName"}}}},"attr_agg": {"nested": {"path": "attrs"},"aggregations": {"attr_id_agg": {"terms": {"field": "attrs.attrId"},"aggregations": {"attr_name_agg": {"terms": {"field": "attrs.attrName"}},"attr_value_agg": {"terms": {"field": "attrs.attrValue"}}}}}}},"highlight": {"pre_tags": ["<b style='color:red'>"],"post_tags": ["</b>"],"fields": {"name": {}}}
}

response

{"took" : 5,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 9,"relation" : "eq"},"max_score" : 3.0658708,"hits" : [{"_index" : "product_db","_type" : "_doc","_id" : "16","_score" : 3.0658708,"_source" : {"id" : "41","name" : "华为P40 Pro手机","keywords" : "华为手机","subTitle" : "华为P40 Pro手机","price" : "2129","promotionPrice" : "2139","originalPrice" : "2149","pic" : "http://tuling-mall.oss-cn-shenzhen.aliyuncs.com/tulingmall/images/20200327/1_DE7F785A7E0C276D3A1F40A5C6D82B07D2AED60CE1F73795mp.png","sale" : 999,"hasStock" : true,"salecount" : 199,"putawayDate" : "2021-05-03","brandId" : 3,"brandName" : "华为","brandImg" : "http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20190129/17f2dd9756d9d333bee8e60ce8c03e4c_222_222.jpg","categoryId" : 19,"categoryName" : "手机通讯","attrs" : [{"attrId" : 1,"attrName" : "容量","attrValue" : "128G"},{"attrId" : 2,"attrName" : "网络","attrValue" : "5G"}]},"highlight" : {"name" : ["<b style='color:red'>华为</b>P40 Pro<b style='color:red'>手机</b>"]}},{"_index" : "product_db","_type" : "_doc","_id" : "13","_score" : 2.1864593,"_source" : {"id" : "38","name" : "华为nova6se 手机 绮境森林 全网通(8G+128G)","keywords" : "轻薄笔记本华为 手机","subTitle" : "华为nova6se 手机","price" : "6769","promotionPrice" : "6469","originalPrice" : "6999","pic" : "http://tuling-mall.oss-cn-shenzhen.aliyuncs.com/tulingmall/images/20200311/78_78_42B09549789695A42D621CF87DC53B5EBE9385772DC61FB9mp.png","sale" : 999,"hasStock" : true,"salecount" : 899,"putawayDate" : "2021-04-15","brandId" : 3,"brandName" : "华为","brandImg" : "http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20190129/17f2dd9756d9d333bee8e60ce8c03e4c_222_222.jpg","categoryId" : 19,"categoryName" : "手机通讯","attrs" : [{"attrId" : 1,"attrName" : "容量","attrValue" : "64G"},{"attrId" : 2,"attrName" : "网络","attrValue" : "5G"}]},"highlight" : {"name" : ["<b style='color:red'>华为</b>nova6se <b style='color:red'>手机</b> 绮境森林 全网通(8G+128G)"]}},{"_index" : "product_db","_type" : "_doc","_id" : "12","_score" : 1.3113759,"_source" : {"id" : "37","name" : "(华为)HUAWEI MateBook X Pro 2019款 13.9英寸3K触控全面屏 轻薄笔记本","keywords" : "轻薄笔记本华为 笔记本电脑","subTitle" : "轻薄华为笔记本 电脑","price" : "4769","promotionPrice" : "4469","originalPrice" : "4999","pic" : "http://tuling-mall.oss-cn-shenzhen.aliyuncs.com/tulingmall/images/20200317/800_800_1555752016264mp.png","sale" : 999,"hasStock" : true,"salecount" : 699,"putawayDate" : "2021-04-14","brandId" : 3,"brandName" : "华为","brandImg" : "http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20190129/17f2dd9756d9d333bee8e60ce8c03e4c_222_222.jpg","categoryId" : 19,"categoryName" : "手机通讯","attrs" : [{"attrId" : 1,"attrName" : "容量","attrValue" : "16G"},{"attrId" : 2,"attrName" : "网络","attrValue" : "4G"}]},"highlight" : {"name" : ["(<b style='color:red'>华为</b>)HUAWEI MateBook X Pro 2019款 13.9英寸3K触控全面屏 轻薄笔记本"]}},{"_index" : "product_db","_type" : "_doc","_id" : "3","_score" : 1.148024,"_source" : {"id" : "28","name" : "小米  手机","keywords" : "小米手机","subTitle" : "AI智慧全面屏 4GB +64GB 亮蓝色 全网通版 移动联通电信4G手机 双卡双待 双卡双待","price" : "2999","promotionPrice" : "1999","originalPrice" : "3999","pic" : "http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20180615/xiaomi.jpg","sale" : 999,"hasStock" : true,"salecount" : 199,"putawayDate" : "2021-04-03","brandId" : 6,"brandName" : "小米","brandImg" : "http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20190129/1e34aef2a409119018a4c6258e39ecfb_222_222.png","categoryId" : 19,"categoryName" : "手机通讯","attrs" : [{"attrId" : 1,"attrName" : "cpu","attrValue" : "2核"},{"attrId" : 2,"attrName" : "颜色","attrValue" : "蓝色"}]},"highlight" : {"name" : ["小米  <b style='color:red'>手机</b>"]}},{"_index" : "product_db","_type" : "_doc","_id" : "1","_score" : 1.0955135,"_source" : {"id" : "26","name" : "小米 11 手机","keywords" : "小米手机","subTitle" : "AI智慧全面屏 6GB +64GB 亮黑色 全网通版 移动联通电信4G手机 双卡双待 双卡双待","price" : "3999","promotionPrice" : "2999","originalPrice" : "5999","pic" : "http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20180615/xiaomi.jpg","sale" : 999,"hasStock" : true,"salecount" : 999,"putawayDate" : "2021-04-01","brandId" : 6,"brandName" : "小米","brandImg" : "http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20190129/1e34aef2a409119018a4c6258e39ecfb_222_222.png","categoryId" : 19,"categoryName" : "手机通讯","attrs" : [{"attrId" : 1,"attrName" : "cpu","attrValue" : "2核"},{"attrId" : 2,"attrName" : "颜色","attrValue" : "黑色"}]},"highlight" : {"name" : ["小米 11 <b style='color:red'>手机</b>"]}},{"_index" : "product_db","_type" : "_doc","_id" : "2","_score" : 1.0955135,"_source" : {"id" : "27","name" : "小米 10 手机","keywords" : "小米手机","subTitle" : "AI智慧全面屏 4GB +64GB 亮白色 全网通版 移动联通电信4G手机 双卡双待 双卡双待","price" : "2999","promotionPrice" : "1999","originalPrice" : "3999","pic" : "http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20180615/xiaomi.jpg","sale" : 999,"hasStock" : false,"salecount" : 99,"putawayDate" : "2021-04-02","brandId" : 6,"brandName" : "小米","brandImg" : "http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20190129/1e34aef2a409119018a4c6258e39ecfb_222_222.png","categoryId" : 19,"categoryName" : "手机通讯","attrs" : [{"attrId" : 1,"attrName" : "cpu","attrValue" : "4核"},{"attrId" : 2,"attrName" : "颜色","attrValue" : "白色"}]},"highlight" : {"name" : ["小米 10 <b style='color:red'>手机</b>"]}},{"_index" : "product_db","_type" : "_doc","_id" : "17","_score" : 0.9146368,"_source" : {"id" : "42","name" : "朵唯智能手机 4G全网通 老人学生双卡双待手机","keywords" : "朵唯手机","subTitle" : "朵唯手机后置双摄,国产虎贲芯片!优化散热结构!浅薄机身!朵唯4月特惠!","price" : "3129","promotionPrice" : "3139","originalPrice" : "3249","pic" : "http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20180615/xiaomi.jpg","sale" : 999,"hasStock" : true,"salecount" : 1199,"putawayDate" : "2021-06-01","brandId" : 59,"brandName" : "朵唯","brandImg" : "http://tuling-mall.oss-cn-shenzhen.aliyuncs.com/tulingmall/images/20200311/2b84746650fc122d67749a876c453619.png","categoryId" : 19,"categoryName" : "手机通讯","attrs" : [{"attrId" : 1,"attrName" : "容量","attrValue" : "32G"},{"attrId" : 2,"attrName" : "网络","attrValue" : "4G"}]},"highlight" : {"name" : ["朵唯智能<b style='color:red'>手机</b> 4G全网通 老人学生双卡双待<b style='color:red'>手机</b>"]}},{"_index" : "product_db","_type" : "_doc","_id" : "4","_score" : 0.63257253,"_source" : {"id" : "29","name" : "Apple iPhone 8 Plus 64GB 金色特别版 移动联通电信4G手机","keywords" : "苹果手机","subTitle" : "苹果手机 Apple产品年中狂欢节,好物尽享,美在智慧!速来 >> 勾选[保障服务][原厂保2年],获得AppleCare+全方位服务计划,原厂延保售后无忧。","price" : "5999","promotionPrice" : "4999","originalPrice" : "7999","pic" : "http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20180615/5acc5248N6a5f81cd.jpg","sale" : 999,"hasStock" : true,"salecount" : 1199,"putawayDate" : "2021-04-04","brandId" : 51,"brandName" : "苹果","brandImg" : "http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20180607/timg.jpg","categoryId" : 19,"categoryName" : "手机通讯","attrs" : [{"attrId" : 1,"attrName" : "cpu","attrValue" : "4核"},{"attrId" : 2,"attrName" : "颜色","attrValue" : "金色"}]},"highlight" : {"name" : ["Apple iPhone 8 Plus 64GB 金色特别版 移动联通电信4G<b style='color:red'>手机</b>"]}}]},"aggregations" : {"attr_agg" : {"doc_count" : 18,"attr_id_agg" : {"doc_count_error_upper_bound" : 0,"sum_other_doc_count" : 0,"buckets" : [{"key" : 1,"doc_count" : 9,"attr_name_agg" : {"doc_count_error_upper_bound" : 0,"sum_other_doc_count" : 0,"buckets" : [{"key" : "cpu","doc_count" : 4},{"key" : "容量","doc_count" : 4},{"key" : "手机膜-材料","doc_count" : 1}]},"attr_value_agg" : {"doc_count_error_upper_bound" : 0,"sum_other_doc_count" : 0,"buckets" : [{"key" : "2核","doc_count" : 2},{"key" : "4核","doc_count" : 2},{"key" : "128G","doc_count" : 1},{"key" : "16G","doc_count" : 1},{"key" : "32G","doc_count" : 1},{"key" : "64G","doc_count" : 1},{"key" : "钢化","doc_count" : 1}]}},{"key" : 2,"doc_count" : 9,"attr_name_agg" : {"doc_count_error_upper_bound" : 0,"sum_other_doc_count" : 0,"buckets" : [{"key" : "网络","doc_count" : 4},{"key" : "颜色","doc_count" : 4},{"key" : "手机膜-颜色","doc_count" : 1}]},"attr_value_agg" : {"doc_count_error_upper_bound" : 0,"sum_other_doc_count" : 0,"buckets" : [{"key" : "4G","doc_count" : 2},{"key" : "5G","doc_count" : 2},{"key" : "白色","doc_count" : 2},{"key" : "蓝色","doc_count" : 1},{"key" : "金色","doc_count" : 1},{"key" : "黑色","doc_count" : 1}]}}]}},"category_agg" : {"doc_count_error_upper_bound" : 0,"sum_other_doc_count" : 0,"buckets" : [{"key" : 19,"doc_count" : 8,"category_name_agg" : {"doc_count_error_upper_bound" : 0,"sum_other_doc_count" : 0,"buckets" : [{"key" : "手机通讯","doc_count" : 8}]}},{"key" : 30,"doc_count" : 1,"category_name_agg" : {"doc_count_error_upper_bound" : 0,"sum_other_doc_count" : 0,"buckets" : [{"key" : "手机配件","doc_count" : 1}]}}]},"brand_agg" : {"doc_count_error_upper_bound" : 0,"sum_other_doc_count" : 0,"buckets" : [{"key" : 3,"doc_count" : 3,"brand_img_agg" : {"doc_count_error_upper_bound" : 0,"sum_other_doc_count" : 0,"buckets" : [{"key" : "http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20190129/17f2dd9756d9d333bee8e60ce8c03e4c_222_222.jpg","doc_count" : 3}]},"brand_name_agg" : {"doc_count_error_upper_bound" : 0,"sum_other_doc_count" : 0,"buckets" : [{"key" : "华为","doc_count" : 3}]}},{"key" : 6,"doc_count" : 3,"brand_img_agg" : {"doc_count_error_upper_bound" : 0,"sum_other_doc_count" : 0,"buckets" : [{"key" : "http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20190129/1e34aef2a409119018a4c6258e39ecfb_222_222.png","doc_count" : 3}]},"brand_name_agg" : {"doc_count_error_upper_bound" : 0,"sum_other_doc_count" : 0,"buckets" : [{"key" : "小米","doc_count" : 3}]}},{"key" : 51,"doc_count" : 2,"brand_img_agg" : {"doc_count_error_upper_bound" : 0,"sum_other_doc_count" : 0,"buckets" : [{"key" : "http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20180607/timg.jpg","doc_count" : 1},{"key" : "http://tuling-mall.oss-cn-shenzhen.aliyuncs.com/tulingmall/images/20200311/2b84746650fc122d67749a876c453619.png","doc_count" : 1}]},"brand_name_agg" : {"doc_count_error_upper_bound" : 0,"sum_other_doc_count" : 0,"buckets" : [{"key" : "苹果","doc_count" : 2}]}},{"key" : 59,"doc_count" : 1,"brand_img_agg" : {"doc_count_error_upper_bound" : 0,"sum_other_doc_count" : 0,"buckets" : [{"key" : "http://tuling-mall.oss-cn-shenzhen.aliyuncs.com/tulingmall/images/20200311/2b84746650fc122d67749a876c453619.png","doc_count" : 1}]},"brand_name_agg" : {"doc_count_error_upper_bound" : 0,"sum_other_doc_count" : 0,"buckets" : [{"key" : "朵唯","doc_count" : 1}]}}]}}
}

聚合

POST /product_db/_search
{"query": {"bool": {"must": [{"multi_match": {"query": "华为手机","fields": ["name","keywords","subTitle"],"operator": "or"}}],"filter": [{"term": {"hasStock": "true"}},{"range": {"price": {"gte": 2000,"lte": 3000}}},{"nested": {"path": "attrs","query": {"bool": {"must": [{"term": {"attrs.attrId": {"value": "1"}}}]}}}}]}},"aggs": {"brand_id_aggs": {"terms": {"field": "brandId","size": 50},"aggs": {"brand_name_aggs": {"terms": {"field": "brandName"}},"brandImg_aggs" : {"terms": {"field": "brandImg"}}}},"attrs_aggs": {"nested": {"path": "attrs"},"aggs": {"attrId_aggs": {"terms": {"field": "attrs.attrId","size": 50},"aggs": {"attrName_aggs": {"terms": {"field": "attrs.attrName"}},"attrValue_aggs": {"terms": {"field": "attrs.attrValue"}}}}}}}, "sort": [{"salecount": {"order": "desc"}}],"highlight": {"fields": {"*": {}}}
}

response

{"took" : 5,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 3,"relation" : "eq"},"max_score" : null,"hits" : [{"_index" : "product_db","_type" : "_doc","_id" : "14","_score" : null,"_source" : {"id" : "39","name" : "iPhone7/6s/8钢化膜苹果8Plus全屏复盖抗蓝光防窥防偷看手机膜","keywords" : "手机膜","subTitle" : "iPhone7/6s/8钢化膜苹果8Plus全屏复盖抗蓝光防窥防偷看手机膜","price" : "29","promotionPrice" : "39","originalPrice" : "49","pic" : "http://tuling-mall.oss-cn-shenzhen.aliyuncs.com/tulingmall/images/20200311/6df99dab78bb2014.jpg","sale" : 999,"hasStock" : true,"salecount" : 799,"putawayDate" : "2021-04-16","brandId" : 51,"brandName" : "苹果","brandImg" : "http://tuling-mall.oss-cn-shenzhen.aliyuncs.com/tulingmall/images/20200311/2b84746650fc122d67749a876c453619.png","categoryId" : 30,"categoryName" : "手机配件","attrs" : [{"attrId" : 1,"attrName" : "手机膜-材料","attrValue" : "钢化"},{"attrId" : 2,"attrName" : "手机膜-颜色","attrValue" : "白色"}]},"highlight" : {"keywords" : ["<em>手机</em>膜"],"subTitle" : ["iPhone7/6s/8钢化膜苹果8Plus全屏复盖抗蓝光防窥防偷看<em>手机</em>膜"],"price" : ["<em>29</em>"],"name" : ["iPhone7/6s/8钢化膜苹果8Plus全屏复盖抗蓝光防窥防偷看<em>手机</em>膜"]},"sort" : [799]},{"_index" : "product_db","_type" : "_doc","_id" : "3","_score" : null,"_source" : {"id" : "28","name" : "小米  手机","keywords" : "小米手机","subTitle" : "AI智慧全面屏 4GB +64GB 亮蓝色 全网通版 移动联通电信4G手机 双卡双待 双卡双待","price" : "2999","promotionPrice" : "1999","originalPrice" : "3999","pic" : "http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20180615/xiaomi.jpg","sale" : 999,"hasStock" : true,"salecount" : 199,"putawayDate" : "2021-04-03","brandId" : 6,"brandName" : "小米","brandImg" : "http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20190129/1e34aef2a409119018a4c6258e39ecfb_222_222.png","categoryId" : 19,"categoryName" : "手机通讯","attrs" : [{"attrId" : 1,"attrName" : "cpu","attrValue" : "2核"},{"attrId" : 2,"attrName" : "颜色","attrValue" : "蓝色"}]},"highlight" : {"keywords" : ["小米<em>手机</em>"],"subTitle" : ["AI智慧全面屏 4GB +64GB 亮蓝色 全网通版 移动联通电信4G<em>手机</em> 双卡双待 双卡双待"],"price" : ["<em>2999</em>"],"name" : ["小米  <em>手机</em>"]},"sort" : [199]},{"_index" : "product_db","_type" : "_doc","_id" : "16","_score" : null,"_source" : {"id" : "41","name" : "华为P40 Pro手机","keywords" : "华为手机","subTitle" : "华为P40 Pro手机","price" : "2129","promotionPrice" : "2139","originalPrice" : "2149","pic" : "http://tuling-mall.oss-cn-shenzhen.aliyuncs.com/tulingmall/images/20200327/1_DE7F785A7E0C276D3A1F40A5C6D82B07D2AED60CE1F73795mp.png","sale" : 999,"hasStock" : true,"salecount" : 199,"putawayDate" : "2021-05-03","brandId" : 3,"brandName" : "华为","brandImg" : "http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20190129/17f2dd9756d9d333bee8e60ce8c03e4c_222_222.jpg","categoryId" : 19,"categoryName" : "手机通讯","attrs" : [{"attrId" : 1,"attrName" : "容量","attrValue" : "128G"},{"attrId" : 2,"attrName" : "网络","attrValue" : "5G"}]},"highlight" : {"keywords" : ["<em>华为</em><em>手机</em>"],"subTitle" : ["<em>华为</em>P40 Pro<em>手机</em>"],"price" : ["<em>2129</em>"],"name" : ["<em>华为</em>P40 Pro<em>手机</em>"]},"sort" : [199]}]},"aggregations" : {"attrs_aggs" : {"doc_count" : 6,"attrId_aggs" : {"doc_count_error_upper_bound" : 0,"sum_other_doc_count" : 0,"buckets" : [{"key" : 1,"doc_count" : 3,"attrValue_aggs" : {"doc_count_error_upper_bound" : 0,"sum_other_doc_count" : 0,"buckets" : [{"key" : "128G","doc_count" : 1},{"key" : "2核","doc_count" : 1},{"key" : "钢化","doc_count" : 1}]},"attrName_aggs" : {"doc_count_error_upper_bound" : 0,"sum_other_doc_count" : 0,"buckets" : [{"key" : "cpu","doc_count" : 1},{"key" : "容量","doc_count" : 1},{"key" : "手机膜-材料","doc_count" : 1}]}},{"key" : 2,"doc_count" : 3,"attrValue_aggs" : {"doc_count_error_upper_bound" : 0,"sum_other_doc_count" : 0,"buckets" : [{"key" : "5G","doc_count" : 1},{"key" : "白色","doc_count" : 1},{"key" : "蓝色","doc_count" : 1}]},"attrName_aggs" : {"doc_count_error_upper_bound" : 0,"sum_other_doc_count" : 0,"buckets" : [{"key" : "手机膜-颜色","doc_count" : 1},{"key" : "网络","doc_count" : 1},{"key" : "颜色","doc_count" : 1}]}}]}},"brand_id_aggs" : {"doc_count_error_upper_bound" : 0,"sum_other_doc_count" : 0,"buckets" : [{"key" : 3,"doc_count" : 1,"brand_name_aggs" : {"doc_count_error_upper_bound" : 0,"sum_other_doc_count" : 0,"buckets" : [{"key" : "华为","doc_count" : 1}]},"brandImg_aggs" : {"doc_count_error_upper_bound" : 0,"sum_other_doc_count" : 0,"buckets" : [{"key" : "http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20190129/17f2dd9756d9d333bee8e60ce8c03e4c_222_222.jpg","doc_count" : 1}]}},{"key" : 6,"doc_count" : 1,"brand_name_aggs" : {"doc_count_error_upper_bound" : 0,"sum_other_doc_count" : 0,"buckets" : [{"key" : "小米","doc_count" : 1}]},"brandImg_aggs" : {"doc_count_error_upper_bound" : 0,"sum_other_doc_count" : 0,"buckets" : [{"key" : "http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20190129/1e34aef2a409119018a4c6258e39ecfb_222_222.png","doc_count" : 1}]}},{"key" : 51,"doc_count" : 1,"brand_name_aggs" : {"doc_count_error_upper_bound" : 0,"sum_other_doc_count" : 0,"buckets" : [{"key" : "苹果","doc_count" : 1}]},"brandImg_aggs" : {"doc_count_error_upper_bound" : 0,"sum_other_doc_count" : 0,"buckets" : [{"key" : "http://tuling-mall.oss-cn-shenzhen.aliyuncs.com/tulingmall/images/20200311/2b84746650fc122d67749a876c453619.png","doc_count" : 1}]}}]}}
}

ElasticSearch(四):DSL Query相关推荐

  1. 分布式搜索引擎ElasticSearch(四) -- 插件使用

    2019独角兽企业重金招聘Python工程师标准>>> 分布式搜索引擎ElasticSearch(四) -- 插件使用 博客分类: 搜索引擎,爬虫 首先 非常感谢国内大神 - Med ...

  2. Elasticsearch的DSL搜索

    Elasticsearch的DSL搜索 一.数据准备 1.创建对应的索引库 2.给索引库创建对应的映射 POST 192.168.1.117:9200/sell/_mapping {"pro ...

  3. 4 ElasticSearch RestFulAPI(DSL)

    ElasticSearch RestFulAPI(DSL) 1 全局操作 1.1 查看集群健康情况 API:GET /_cat/health?v ?v表示显示头信息 集群的健康状态有红.黄.绿三个状态 ...

  4. ElasticSearch(四) API 约定

    title: ElasticSearch(四) API 约定 tags: ElasticSearch author: Clown95 API 约定 现在我们对Elasticsearch有些了解,现在我 ...

  5. 【Elasticsearch】Elasticsearch filter和query的不同

    1.概述 吃透 | Elasticsearch filter和query的不同

  6. Elasticsearch学习---Term query和Match query

    前言 在Elasticsearch中Term query和Match query都可以用来对文档中的数据进行检索,但是在检索结果上会稍有不同,本文通过案例对两者的差异进行说明. 数据准备 建立一个索引 ...

  7. 【弄nèng - Elasticsearch】DSL入门篇(四)—— 布尔查询Bool Query

    文章目录 1. 介绍 2. 测试 项目推荐 1. 介绍 Bool Query包括四种子句,must, should, must_not, filter. 格式: {"bool":{ ...

  8. ElasticSearch高级 (Query DSL查询 bulk批量操作 导入数据 各种查询 实战技巧-优化比重 全量与增量数据同步)

    ElasticSearch高级 01-Query DSL(Domain Specific Language) 1 查询上下文 2 相关度评分:_score 3 元数据:_source 4 Query ...

  9. Elasticsearch高级查询Query DSL

    一.高级查询Query DSL简介 1.Query DSL(简介 Elasticsearch中提供了一种强大的检索数据方式,这种检索方式称之为Query DSL(Domain Specified La ...

最新文章

  1. swift开发记录 - MARK,TODO,FIXME
  2. 成功解决ValueError: attempted relative import beyond top-level package
  3. python 多继承的实现
  4. LinuxMint(Ubuntu)安装文泉驿家族黑体字
  5. python实现语音播放_用Python实现语音播报
  6. K均值聚类关于初始聚类中心的探讨
  7. Linux minilogd占用内存过高及开机启动项修改
  8. PHP如何判断一个数组是一维数组或者是二维数组?用什么函数?
  9. 教你用Python抓取百度翻译
  10. python中第三方模块_如何在python脚本中包含第三方模块?
  11. 这个工具,可视化分析10W数据后,找到了数据岗位月薪20K的秘诀
  12. 仿制波形驱动机器人- SAW
  13. VMware中安装Linux系统详细步骤
  14. Golang + Qt5 桌面开发终极解决方案
  15. vmware 无法安装 (出现原因及解决办法)
  16. 数据库自定义聚合函数(求和、标准差、平均值、几何平均值、几何标准差、偏度系数、峰度系数)
  17. Fabric.js 文档
  18. mysql批量添加空行_MySQL加入空行
  19. ios html录制视频,iPhone怎么录屏?玩转iOS14自带屏幕录制功能全攻略
  20. 近地天体撞击地球原理的设想

热门文章

  1. Stata:缺失值的填充和补漏
  2. 感觉-勇气-缘分 爱的永恒
  3. win10怎么取消开机磁盘检测
  4. 市场调研报告-全球与中国吸热气体发生器市场现状及未来发展趋势
  5. Python项目代码结构详解
  6. win10系统用什么测试软件,Win10系统如何测试软件兼容性?
  7. C++嵌入汇编语言计算有符号数组的平均值
  8. 3756. 筛选链表
  9. 读开源QScada框架HmiFuncDesigner-master的笔记
  10. android 渠道号怎么写,android无渠道号推广的细分统计