2017-03-16 103 views
0

我使用5.2版本GROUP BY在elasticsearch

想寫一個GROUP BY查詢彈性搜索我要查詢的數據,並限制下來到那些有特定的「標籤」。在下面的情況下。我想在標題或內容字段中選擇包含單詞「FIY」的項目,然後將其縮小以僅搜索具有標籤「FIY」和「Competition」的文檔。

查詢部分很好但我正在努力限制它給定的標籤。

到目前爲止,我得到了,但我得到的錯誤。

「原因」: 「[BOOL]查詢不支持[條款]」,

GET advice-articles/_search 
{ 
    "query": { 
    "bool": { 
    "must": [ 
    { 
     "multi_match": { 
     "query": "FIY", 
     "fields": ["title", "content"] 
     } 
    } 
    ], "filter": { 
    "bool": { 
     "terms": { 
     "tags.tagName": [ 
      "competition" 
     ] 
     } 
    } 
    } 
} 
} 
} 

的示例索引是

"_index": "advice-articles", 
    "_type": "article", 
    "_id": "1460", 
    "_score": 4.3167734, 
    "_source": { 
     "id": "1460", 
     "title": "Your top FIY tips", 
     "content": "Fix It Yourself in April 2012.", 
     "tags": [ 
     { 
      "tagName": "Fix it yourself" 
     }, 
     { 
      "tagName": "customer tips" 
     }, 
     { 
      "tagName": "competition" 
     } 
     ] 

我有對應關係如下

{ 
"advice-articles": { 
"mappings": { 
    "article": { 
    "properties": { 
     "content": { 
     "type": "text", 
     "fields": { 
      "keyword": { 
      "type": "keyword", 
      "ignore_above": 256 
      } 
     } 
     }, 
     "tags": { 
     "type": "nested", 
     "properties": { 
      "tagName": { 
      "type": "text", 
      "fields": { 
       "raw": { 
       "type": "keyword" 
       } 
      } 
      } 
     } 
     } 
    } 
    } 
} 
} 

}

+0

我認爲當你寫「布爾」時必須遵循「必須」,「應該」,「過濾器」或「must_not」。請參閱https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html – Adonis

回答

1

bool query使用一個或多個布爾子句,與類型化的發生每個子句建造。發生類型有: mustmust_notfiltershould

GET _search 
{ 
    "query": { 
    "bool": { 
     "must": [ 
     { 
      "multi_match": { 
      "query": "FIY", 
      "fields": [ 
       "title", 
       "content" 
      ] 
      } 
     }, 
     { 
      "nested": { 
      "path": "tags", 
      "query": { 
       "terms": { 
       "tags.tagName": [ 
        "competition" 
       ] 
       } 
      } 
      } 
     } 
     ] 
    } 
    } 
} 

這裏是你如何使用必備條款爲您的查詢需求。

+0

完整的示例,看看(上面)如果我使用在其上面不返回任何結果,使用應該返回的結果不是正確的文件 – simon1230756

+0

@ simon1230756錯過了標籤的嵌套部分,更新了查詢。請立即檢查。 – Rahul

+0

一切都很好,剛剛注意到我自己然後發佈了你的答案 – simon1230756

1

Inside 過濾器你不需要把布爾。

POST newindex/test/1460333 
{ 
    "title": "Your top FIY tips", 
    "content": "Fix It Yourself in April 2012.", 
    "tags": [ 
    { 
     "tagName": "Fix it yourself" 
    }, 
    { 
     "tagName": "customer tips" 
    }, 
    { 
     "tagName": "shoud not return" 
    } 
    ] 
} 

POST newindex/test/1460 
{ 
    "title": "Your top FIY tips", 
    "content": "Fix It Yourself in April 2012.", 
    "tags": [ 
    { 
     "tagName": "Fix it yourself" 
    }, 
    { 
     "tagName": "customer tips" 
    }, 
    { 
     "tagName": "competition" 
    } 
    ] 
} 

查詢:

GET newindex/_search 
{ 
    "query": { 
    "bool": { 
     "must": [ 
     { 
      "multi_match": { 
      "query": "FIY", 
      "fields": [ 
       "title", 
       "content" 
      ] 
      } 
     } 
     ], 
     "filter": { 
     "terms": { 
      "tags.tagName": [ 
      "competition" 
      ] 
     } 
     } 
    } 
    } 
} 

結果:

{ 
    "took": 4, 
    "timed_out": false, 
    "_shards": { 
    "total": 5, 
    "successful": 5, 
    "failed": 0 
    }, 
    "hits": { 
    "total": 1, 
    "max_score": 0.2876821, 
    "hits": [ 
     { 
     "_index": "newindex", 
     "_type": "test", 
     "_id": "1460", 
     "_score": 0.2876821, 
     "_source": { 
      "title": "Your top FIY tips", 
      "content": "Fix It Yourself in April 2012.", 
      "tags": [ 
      { 
       "tagName": "Fix it yourself" 
      }, 
      { 
       "tagName": "customer tips" 
      }, 
      { 
       "tagName": "competition" 
      } 
      ] 
     } 
     } 
    ] 
    } 
} 
+0

如果我使用上述它不返回任何結果,使用應返回結果不是但不是正確的文件 – simon1230756

+1

@ simon1230756有一個完整的例子,其中使用elasticsearch 5.2 – natnael88

+0

感謝,仍然沒有爲我工作,但注意到我的映射設置爲嵌套,現在都好:-) – simon1230756