需要

有一批文献,作者ID(user_id)是多值,传入一批 user_id,每个 user_id 取一篇文献进去。

如上图,想要取 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