elasticSearch:在一个字段上具有多个值的布尔查询

时间:2020-06-24 00:46:05

标签: elasticsearch

这有效:

 GET /bitbucket$$pull-request-activity/_search
 {
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "prid": "12343"
          }
        },
        {
          "match": {
            "repoSlug": "com.xxx.vserver"
          }
        }
      ]
    }
  }
}

但是我想在一个呼叫中捕获多个prid。 但是,这不起作用:

 GET /bitbucket$$pull-request-activity/_search


{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "prid": "[12343, 11234, 13421]"
          }
        },
        {
          "match": {
            "repoSlug": "com.xxx.vserver"
          }
        }
      ]
    }
  }
}

有什么提示吗?

1 个答案:

答案 0 :(得分:1)

bool查询中使用必须时,这表示逻辑AND ,因此请确保您所使用的所有文档prid字段的匹配项也应与"repoSlug": "com.xxx.vserver"匹配。

如果没有文档与"repoSlug": "com.xxx.vserver"匹配,则不会返回任何结果。

而且,如果只有2个文档匹配,则搜索结果中将仅返回其中2个文档,而不是所有文档。

添加带有映射,示例文档和搜索查询的工作示例

索引样本数据:

{
  "id":"1",
  "message":"hello"
}
{
  "id":"2",
  "message":"hello"
}
{
  "id":"3",
  "message":"hello-bye"
}

搜索查询:

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "id": "[1, 2, 3]"
          }
        },
        {
          "match": {
            "message": "hello"
          }
        }
      ]
    }
  }
}

搜索结果:

"hits": [
      {
        "_index": "foo14",
        "_type": "_doc",
        "_id": "1",
        "_score": 1.5924306,
        "_source": {
          "id": "1",
          "message": "hello"
        }
      },
      {
        "_index": "foo14",
        "_type": "_doc",
        "_id": "3",
        "_score": 1.4903541,
        "_source": {
          "id": "3",
          "message": "hello-bye"
        }
      },
      {
        "_index": "foo14",
        "_type": "_doc",
        "_id": "2",
        "_score": 1.081605,
        "_source": {
          "id": "2",
          "message": "hello"
        }
      }
    ]