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