1.前言
es文档看后感到十分空,本篇文章练习如何使用RESTful风格的api请求查询Es中的数据。下面以提一个需求,写出对应的查询方式来验证自己是否掌握
索引名: goods
数据格式如下:
2.实例
1.查询spuId为44的商品信息
①使用URI的方式
格式: /索引/_search?q=字段名:字段值
GET /goods/_search?q=spuId:44
②使用请求体的方式
term是指某个字段的值必须是 44, 但是不能用来匹配字段值类型为text类型的。
GET /goods/_search{"query": {"term": {"spuId": {"value": "44"}}}}

2.精准匹配term不可用于text类型的字段查询

①搜索mainTitle字段为:Apple iPhone 11 手机 黑色 64GB 的商品
GET /goods/_search{"query": {"term": {"mainTitle": {"value": "Apple iPhone 11 手机 黑色 64GB","boost": 1.0}}}}

②这种需要使用match的方式进行搜索(最短的写法)
GET /goods/_search{"query": {"match": {"mainTitle": "Apple iPhone 11 手机 黑色 256GB"}}}
match查询完全写法
GET /goods/_search{"query": {"match": {"字段名": {"query":"用户的搜索输入内容","analyzer":"","auto_generate_synonyms_phrase_query":"",.................}}}}
3.查询spuId为44和shopId为92并且sales字段为11的

①解决思路:and and 在es中即使 bool查询
POST /goods/_search{"query": {"bool" : {"must": [{"term" : { "spuId" : "44" }},{"term" : { "shopId" : "92" }},{"term" : { "sales": "11" }}]}}}
三者取交集 就是查询结果。。
也可以用filter
POST /goods/_search{"query": {"bool" : {"filter": [{"term" : { "spuId" : "44" }},{"term" : { "shopId" : "92" }},{"term" : { "sales": "11" }}]}}}
4.组合查询中的should如何用
GET /goods/_search{"query": {"bool" : {"must": [{"term" : { "spuId" : "44" }},{"term" : { "shopId" : "92" }},{"term" : { "sales": "11" }}],"should":[{"term":{"sales":"7"}}],"minimum_should_match": 1}}}

GET /goods/_search{"query": {"bool" : {"must": [{"term" : { "spuId" : "44" }},{"term" : { "shopId" : "92" }},{"term" : { "sales": "11" }}],"should":[{"term":{"sales":"7"}}]}}}

should:指所匹配的文档或者说数据记录应该存在于查询结果中
当又must或filter存在时,优先选择must和filter中的。
当时当配置了minimum_should_match 相当于should 变成 了must 。
5.如何知道查出来的数据时哪个匹配上的
GET /goods/_search{"query": {"bool" : {"should" : [{"match" : { "mainTitle": {"query" : "手机", "_name" : "first"} }},{"match" : { "mainTitle" : {"query" : "化妆品", "_name" : "last"} }}]}}}

6.查询spuId为44并且mysqlId为179的数据赋值低一些的分数
GET /goods/_search{"query": {"boosting" : {"positive" : {"term" : {"spuId" : "44"}},"negative" : {"term" : {"mysqlId" : "179"}},"negative_boost" : 0.1}}}
7.使用filter进行查询并返回指定分数
filter查询返回的分数为0 ,如何赋值一个分数
GET /goods/_search{"query": {"constant_score" : {"filter" : {"term" : { "spuId" : "44"}},"boost" : 1.2}}}

8.DisjunctionMax 计算得分的方式
多个查询,A:10,B: 8,C :7
那么 最后的得分是 10 + (7+8)*tie_breaker = 最终得分

默认tie_breaker为0
9.如果查询结果分数想做处理
function score ,具体就是在你查询结果的分数到底怎么计算的问题


