Elasticsearch 7.x 中 msearch 使用案例

需求

有一批文献,作者ID(user_id)是多值,传入一批 user_id,每个 user_id 取一篇文献出来。
excel.png
如上图,想要取 user_id 包含 222、444 的文献各一篇。

数据准备

POST my_index/_bulk
{"index":{"_id":1}}
{"author_id":[111,222]}
{"index":{"_id":2}}
{"author_id":[222,333]}
{"index":{"_id":3}}
{"author_id":[444,555]}

查询

terms

  • 3 条全中,不符合需求
GET my_index/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "terms": {
          "author_id": [ "222", "444" ]
        }
      }
    }
  }
}

bool + should + term

  • 3 条全中,不符合需求
GET my_index/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "term": {
            "author_id": "222"
          }
        },
        {
          "term": {
            "author_id": "444"
          }
        }
      ],
      "minimum_should_match": 1
    }
  }
}

_msearch

  • 222、444 各中一条,符合要求
GET my_index/_msearch
{"index":"my_index"}
{"query":{"constant_score":{"filter":{"term":{"author_id":"222"}}}},"size":1,"terminate_after":1}
{"index":"my_index"}
{"query":{"constant_score":{"filter":{"term":{"author_id":"444"}}}},"size":1,"terminate_after":1}
本文出自 qbit snap

你可能感兴趣的:(elasticsearch,query)