查询多种文档

时间:2019-06-16 07:33:23

标签: elasticsearch

我要搜索的是在一定范围的文档中提取文档,而不是整个文档。我知道文件编号。例如,我想通过不同的过程在我知道的文档ID中查询与查询字段-'pLabel'匹配的某些句子。我的审判如下,但我收到的文件堆与我的期望有所不同。 例如,在诸如eid1,eid2 ... etc组的文档中,我想查询从组(eid1,eid2,eid3,...)中筛选出匹配的文档。查询如下所示。

如何修复查询语句以获得正确的搜索结果?

{
  "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "default_field": "pLabel" ,
            "query": "search words here"
          }
        }
      ] ,
      "must_not": [] ,
      "should": [
        {
          "term": {
            "eid": "eid1"
          }
        } ,
        {
          "term": {
            "eid": "eid2"
          }
        }
      ]
    }
  } ,
  "size": 0 ,
  "_source": [
    "eid"
  ] ,
  "aggs": {
    "eids": {
      "terms": {
        "field": "eid" ,
        "size": 1000
      }
    }
  }
}

1 个答案:

答案 0 :(得分:0)

您需要将Doc ID的should子句移动到must子句中。

现在查询可以返回与query_string子句匹配的任何文档,它只会选择与Doc ID匹配的文档。

此外,您应该使用terms查询

{
  "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "default_field": "pLabel",
            "query": "search words here"
          }
        },
        {
          "terms": {
            "user": ["eid1", "eid2"]
          }
        }
      ]
    }
  },
  "size": 0,
  "_source": [
    "eid"
  ],
  "aggs": {
    "eids": {
      "terms": {
        "field": "eid",
        "size": 1000
      }
    }
  }
}
相关问题