ElasticSearch过滤所需的多个字段

时间:2019-02-04 09:49:57

标签: elasticsearch elasticsearch-6 elasticsearch-php

我有这个查询(通过PHP客户端),用于ElasticSearch 6.2:

[
    "query"=> [
        "bool" => [
            "filter" => [
                "term" => [ "row.name" => $name ],
                "term" => [ "row.origin" => $origin ]
            ]
        ]
    ],
    "size" => "10"
]

如果我仅对row.namerow.origin使用一个搜索,它就可以工作,但是对两者而言,它都可以像OR一样工作并返回所有结果。如何过滤以仅返回与row.namerow.origin

完全匹配的文档

1 个答案:

答案 0 :(得分:1)

您采取了正确的方法,但我猜您错过了括号。

代替:

[
    "query"=> [
        "bool" => [
            "filter" => [
                "term" => [ "row.name" => $name ],
                "term" => [ "row.origin" => $origin ]
            ]
        ]
    ],
    "size" => "10"
]

您应该具有:

[
    "query"=> [
        "bool" => [
            "filter" => [
                ["term" => [ "row.name" => $name ]],
                ["term" => [ "row.origin" => $origin ]]
            ]
        ]
    ],
    "size" => "10"
]

在您的情况下,您使用两个相同的(term)键创建了一个地图:

[
  "term" => [ "row.name" => $name ],
  "term" => [ "row.origin" => $origin ]
]

第二个term取代了第一个{实际上,您发送了:

[
  "term" => [ "row.origin" => $origin ]
]

要发送多个term过滤器,您需要将其视为列表:

[
  ["term" => [ "row.name" => $name ]],
  ["term" => [ "row.origin" => $origin ]]
]