首页 > 解决方案 > elasticsearch 如何在单个文档中查询(搜索)?

问题描述

假设索引的名称是索引 & 文档 1 的 id 是“1”

如何在单个文档中查询?

像这样的东西..

GET index/_search
{
   "query": {
      "id": "1",
      "terms": ["is this text in document 1?"]
   }
}

或者

GET index/_doc/1/_search
{
    ...
}

据我发现,

GET test/_doc/_search
{
    "query": {
        "terms" : {
            "_id" : ["1"]
        }
    }
}

这将获得“1”的文档 ID,但无法执行任何进一步的查询。

我想在单个文档中查询的​​原因是因为我的应用程序正在使用实时新闻视图,并且一旦从服务器检索到新闻,我想在弹性搜索中搜索它以查找关键工作突出显示和垃圾邮件过滤。

标签: elasticsearch

解决方案


您必须使用布尔查询来编写查询

最好的方法是在过滤器下指定 id 查询,因为它不会影响评分。接下来,您可以根据需要在 must、must_not 和 should 下指定查询:

GET index/_search
{
  "from": 0,
  "size": 10,
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "field": "value"
          }
        }
      ],
      "must_not": [],
      "should": [],
      "filter": [
        {
          "terms": {"_id": ["1"]}
        }
      ]
    }
  }
}

推荐阅读