ElasticSearch 匹配查询多个词PHP

ElasticSearch match query multiple terms PHP

我正在尝试构建 must 查询多个术语,数组如下所示:

$params = [
'body' => [
    'query' => [
        "bool" => [
            "must" => [
                "terms" => [
                    "categories" => [
                        "Seating",
                    ],
                ],
                "terms" => [
                    "attributes.Color" => [
                        "Black",
                    ],
                ]
            ],
            "filter" => [
                "range" => [
                    "price" => [
                        "gte" => 39,
                        "lte" => 2999,
                    ],
                ],
            ],
        ],
    ],
    'from' => 0,
    'size' => 3,
],
];

在JSON中表示为:

{
"query": {
    "bool": {
        "must": {
            "terms": {
                "attributes.Color": ["Black"]
            }
        },
        "filter": {
            "range": {
                "price": {
                    "gte": "39",
                    "lte": "2999"
                }
            }
        }
    }
},
"from": 0,
"size": 3
}

问题是,JSON 对象在 PHP 中表示为数组,因此如果我为一个数组设置键,它会被重写。您知道如何在 PHP 中创建多个术语查询吗?

提前致谢。

您需要添加一个额外的数组来包含所有 terms 查询

$params = [
'body' => [
    'query' => [
        "bool" => [
            "must" => [
              [
                "terms" => [
                    "categories" => [
                        "Seating",
                    ],
                ]
              ],
              [
                "terms" => [
                    "attributes.Color" => [
                        "Black",
                    ],
                ]
              ]
            ],
            "filter" => [
                "range" => [
                    "price" => [
                        "gte" => 39,
                        "lte" => 2999,
                    ],
                ],
            ],
        ],
    ],
    'from' => 0,
    'size' => 3,
],
];