elasticsearch should實現or功能,設置minimum_should_match

elasticsearch實現傳統數據庫中的or功能,須要使用bool下面的should關鍵字,對於A or B的狀況,應該至少返回A和B中的一個,可是以下語句,不只返回A和B中的至少一個,也返回了沒有A也沒有B的狀況:數據庫

{
   "query": {
     "bool": {
       "fileter":[
           {"range":{"date.keyword":{"gt":"20170101","lt":"20170201"}}}
       ]
       "should": [
           {"term": {"A.keyword": "0000000000"}},
           {"term": {"B.keyword": "0000000001"}}
      ]
    }
  }
}

參看elasticsearch官方文檔,對should的說明以下:json

shouldapp

The clause (query) should appear in the matching document. If the bool query is in a query context and has a must or filter clause then a document will match the bool query even if none of the should queries match. In this case these clauses are only used to influence the score. If the bool query is a filter context or has neither must or filter then at least one of the should queries must match a document for it to match the bool query. This behavior may be explicitly controlled by settings the minimum_should_match parameter.less

表達的意思是:若是一個query語句的bool下面,除了should語句,還包含了filter或者must語句,那麼should context下的查詢語句能夠一個都不知足,只是_score=0,因此上述查詢語句,有無should語句,查詢到的hits().total()是同樣的,只是score不一樣而已。elasticsearch

爲了達到傳統數據庫中or的功能,有以下兩種方法:ui

  1. 將should語句寫到must下面,而後讓must和filter並列
    {
      "query": {
        "bool": {
    	  "fileter":[
    	      {"range":{"date.keyword":{"gt":"20170101","lt":"20170201"}}}
    	  ],
    	  "must":[
    		  {
    			"bool":{
    			  "should": [
    				  {"term": {"A.keyword": "0000000000"}},
    				  {"term": {"B.keyword": "0000000001"}}
    			  ]
    			 }
    		  }
    	  ]
        }
      }
    }

     

     2. 採用官方文檔中的 minimum_should_match 參數this

Type Example Description

Integerspa

3.net

Indicates a fixed value regardless of the number of optional clauses.code

Negative integer

-2

Indicates that the total number of optional clauses, minus this number should be mandatory.

Percentage

75%

Indicates that this percent of the total number of optional clauses are necessary. The number computed from the percentage is rounded down and used as the minimum.

Negative percentage

-25%

Indicates that this percent of the total number of optional clauses can be missing. The number computed from the percentage is rounded down, before being subtracted from the total to determine the minimum.

Combination

3<90%

A positive integer, followed by the less-than symbol, followed by any of the previously mentioned specifiers is a conditional specification. It indicates that if the number of optional clauses is equal to (or less than) the integer, they are all required, but if it’s greater than the integer, the specification applies. In this example: if there are 1 to 3 clauses they are all required, but for 4 or more clauses only 90% are required.

Multiple combinations

2<-25% 9<-3

Multiple conditional specifications can be separated by spaces, each one only being valid for numbers greater than the one before it. In this example: if there are 1 or 2 clauses both are required, if there are 3-9 clauses all but 25% are required, and if there are more than 9 clauses, all but three are required.

minimum_should_match表明了最小匹配精度,若是設置minimum_should_match=1,那麼should語句中至少須要有一個條件知足,查詢語句以下:

{
  "query": {
    "bool": {
	  "fileter":[
	      {"range":{"date.keyword":{"gt":"20170101","lt":"20170201"}}}
	  ]
      "should": [
		  {"term": {"A.keyword": "0000000000"}},
		  {"term": {"B.keyword": "0000000001"}}
      ],
	  "minimum_should_match":1
    }
  }
}

第一種方法和第二種方法返回的結果是一致的。

另外,minimum_should_match的參數不少:

http://blog.csdn.net/xiao_jun_0820/article/details/51095521  講的很清楚。

相關文章
相關標籤/搜索