2017-03-16 82 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