编程语言
首页 > 编程语言> > ElasticSearch匹配查询多个术语PHP

ElasticSearch匹配查询多个术语PHP

作者:互联网

我试图构造必须查询多个术语,数组看起来像这样:

$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中创建多个字词查询有任何想法吗?

提前致谢.

解决方法:

您需要添加其他数组以包含所有术语查询

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

标签:elasticsearch,php,querydsl
来源: https://codeday.me/bug/20191118/2028920.html