2016-01-16 61 views
1

我想寫一個2部分的Elasticsearch布爾查詢。 我需要兩個條件「must」和兩個「should」。問題是我只想得到「應該」的分數。我嘗試了「過濾器」,但沒有成功。Elasticsearch布爾查詢與篩選器

更具體地講,我做了查詢是在這裏:

{ 
    "query": { 
    "bool": { 
     "must": [ 
     { 
      "match": { 
      "attr1": "on" 
      } 
     }, 
     { 
      "match": { 
      "category": "7eb84c804a8c824fd608e05f78d42f10" 
      } 
     } 
     ], 
     "should": [ 
     { 
      "term": { 
      "attr2": "on" 
      } 
     }, 
     { 
      "term": { 
      "attr3": "on" 
      } 
     } 
     ], 
     "minimum_should_match": "10%" 
    } 
    } 
} 

UPDATE 這裏是失敗的查詢,我想找到的錯誤!:

{ 
    "query": { 
    "bool": { 
     "filter": [ 
     { 
      "term": { 
      "attr1": "on" 
      } 
     }, 
     { 
      "term": { 
      "category": "7eb84c804a8c824fd608e05f78d42f10" 
      } 
     } 
     ], 
     "should": [ 
     { 
      "term": { 
      "attr2": "on" 
      } 
     }, 
     { 
      "term": { 
      "attr3": "on" 
      } 
     } 
     ], 
     "minimum_should_match": "10%" 
    } 
    } 
} 

如何可以按照以下順序重新編寫該查詢: 1)獲取attr1和類別完全相同的所有行 2)然後用should查詢這些結果

有什麼想法嗎?

+0

你有什麼使用過濾器? –

+0

嵌套:QueryParsingException [[dev1] [bool]查詢不支持[filter]]; –

+0

我根據您的答案更新我的問題並提供詳細信息! –

回答

3

看起來像你不在Elasticsearch 2.x上。

對於Elasticsearch 1.x中使用FilteredQuery:https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-filtered-query.html

{ 
    "filtered": { 
    "query": { 
     "bool": { 
     "should": [ 
      { 
      "term": { 
       "attr2": "on" 
      } 
      }, 
      { 
      "term": { 
       "attr3": "on" 
      } 
      } 
     ] 
     } 
    }, 
    "filter": { 
     "bool": { 
     "must": [ 
      { 
      "match": { 
       "attr1": "on" 
      } 
      }, 
      { 
      "match": { 
       "category": "7eb84c804a8c824fd608e05f78d42f10" 
      } 
      } 
     ] 
     } 
    } 
    } 
} 
+0

我使用Elasticsearch v2.0.0! –

+0

它的工作原理!謝謝! –

+0

你爲什麼在Elasticsearch v2.0.0快照而不是最新版本上工作?我會盡快切換並使用帶有過濾器子句的原始bool-query。 – Ronald