【问题标题】:ElasticSearch match query multiple terms PHPElasticSearch 匹配查询多个词 PHP
【发布时间】:2016-06-13 13:05:30
【问题描述】:

我正在尝试构造必须查询多个词的数组,如下所示:

$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" => [ "categories" => [ "Seating" ], "attributes.Color" => [ "Black" ] ],

标签: php elasticsearch querydsl


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-10
    • 1970-01-01
    • 2020-04-21
    • 1970-01-01
    • 2017-09-09
    • 1970-01-01
    • 1970-01-01
    • 2015-03-13
    相关资源
    最近更新 更多