Elastic Search Search in Deepth
来源:互联网 发布:青岛齐鲁商品交易软件 编辑:程序博客网 时间:2024/05/01 15:41
Find Exact Values
term用于查询指定字段是否完全匹配给定的值,可以使用constant_score执行non-scoring mode
GET /my_store/products/_search{ "query" : { "constant_score" : { "filter" : { "term" : { "price" : 20 } } } }}
使用term查询text时会遇到的问题
GET /my_store/products/_search{ "query" : { "constant_score" : { "filter" : { "term" : { "productID" : "XHDK-A-1293-#fJ3" } } } }}
不会得到任何返回是因为,text会被分析器分割成小的token
GET /my_store/_analyze?field=productID&text=XHDK-A-1293-%23fJ3{ "tokens" : [ { "token" : "xhdk", "start_offset" : 0, "end_offset" : 4, "type" : "<ALPHANUM>", "position" : 1 }, { "token" : "a", "start_offset" : 5, "end_offset" : 6, "type" : "<ALPHANUM>", "position" : 2 }, { "token" : "1293", "start_offset" : 7, "end_offset" : 11, "type" : "<NUM>", "position" : 3 }, { "token" : "fj3", "start_offset" : 13, "end_offset" : 16, "type" : "<ALPHANUM>", "position" : 4 } ]}
所以使用XHDK-A-1293-#fJ3查不到任何结果是因为它并不存在于倒排索引中。
可以通过把字段设置为not_analyzed来防止分词
DELETE /my_store PUT /my_store { "mappings" : { "products" : { "properties" : { "productID" : { "type" : "string", "index" : "not_analyzed" } } } }}
Combining Filters
Bool Filter
可以使用bool query做如下的查询
SELECT productFROM productsWHERE (price = 20 OR productID = "XHDK-A-1293-#fJ3") AND (price != 30)
bool query由以下四个部分组成
{ "bool" : { "must" : [], // MUST MATCH 等同于 AND "should" : [], // 等同于 OR "must_not" : [], // 等同于 NOT "filter": [] // MUST MATCH,但是不打分 }}
上面的SQL查询可以表示为,注意两个term queries只是被分别放在了should和must_not中而并没有放在must中是因为所有的查询都被包裹在constant_sorce中,所有其余的查询都使用filter模式
GET /my_store/products/_search{ "query" : { "constant_score" : { "filter" : { "bool" : { "should" : [ { "term" : {"price" : 20}}, { "term" : {"productID" : "XHDK-A-1293-#fJ3"}} ], "must_not" : { "term" : {"price" : 30} } } } } }}
Nesting boolean queries
SELECT documentFROM productsWHERE productID = "KDKE-B-9947-#kL5" OR ( productID = "JODL-X-1937-#pV7" AND price = 30 )
可以转换成
GET /my_store/products/_search{ "query" : { "constant_score" : { "filter" : { "bool" : { "should" : [ { "term" : {"productID" : "KDKE-B-9947-#kL5"}}, { "bool" : { "must" : [ { "term" : {"productID" : "JODL-X-1937-#pV7"}}, { "term" : {"price" : 30}} ] }} ] } } } }}
0 0
- Elastic Search Search in Deepth
- elastic search
- Elastic Search Data In, Data Out
- Elastic Search使用
- Elastic Search vs. mongodb
- elastic search教程
- Elastic Search Filter Script
- Elastic search点点滴滴
- elastic search记录
- Elastic Search RESTful API
- elastic search 配置文件
- Elastic Search Mapping
- Elastic Search 查询基础
- elastic search 基本介绍
- Elastic Search 学习概要
- ELASTIC SEARCH学习笔记
- elastic search学习
- Elastic Search学习总结
- Date
- 计算几何基础_1
- 201605计划安排
- 线程共享--互斥锁
- 谁在关心toString的性能?
- Elastic Search Search in Deepth
- IOS 将打印字典和数组的内容由字符编码变为中文
- HTTP之Content-Length
- Windows下cucumber的安装
- JAVA POI读写excel
- android studio更改兼容android低版本
- ajax请求绕过同源策略的实现
- 【HDU】 2444 The Accomodation of Students
- STL容器学习总结