简体   繁体   English

ElasticSearch匹配查询多个术语PHP

[英]ElasticSearch match query multiple terms PHP

I am trying to construct must query on multiple terms, the array looks like this: 我试图构造必须查询多个术语,数组看起来像这样:

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

Which is represented in JSON like this: 其中以JSON表示,如下所示:

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

The problem is, JSON objects are represented as arrays in PHP so if I setup key for one array, it is rewritten. 问题是,JSON对象在PHP中表示为数组,因此如果我为一个数组设置了键,则会重写它。 Do you have any idea on how to create multiple terms query in PHP? 您对如何在PHP中创建多个术语查询有任何想法吗?

Thanks in advance. 提前致谢。

You need to add an additional array to enclose all your terms queries 您需要添加其他数组以包含所有terms查询

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM