在弹性搜索中匹配阵列



我的文档如下:

{
    "_index": "abc_local",
    "_type": "users",
    "_id": "1",
    "_version": 5,
    "found": true,
    "_source": {
        "firstname": "simer",
        "lastname": "kaur",
        "gender": "1",
        "Address": "Punjab House Fed. Housing Society, Amritsar, Punjab, India",
        "email": "rav@yopmail.com",
        "occupation": "Php Developer",
        "work": "Development",
        "fav_hunting_land": 2,
        "zipcode": "",
        "marital_status": "1",
        "phone": "1234567899",
        "school": "sdfergdfh",
        "species": [{
            "id": 1
        }, {
            "id": 2
        }, {
            "id": 3
        }, {
            "id": 4
        }, {
            "id": 5
        }, {
            "id": 6
        }],
        "activities": [{
            "id": 1
        }],
        "fav_weapon": 6,
        "weapons": [{
            "id": 1
        }, {
            "id": 2
        }, {
            "id": 3
        }, {
            "id": 6
        }],
        "properties": [{
            "id": 4
        }]
    }
}

我需要根据武器进行匹配,我正在尝试以下操作:

$params = [
            'index' => Constants::INDEX,
            'type' => Constants::DOC_TYPE_USERS,
            'body' => [
                "query"=> [
                    "bool"=> [
                        "must"=>   [ "match"=> [ "weapons.id"=>$params['weapons'] ]],
                        "should"=> [
                                    [ "match"=> [ "firstname"=> $params['search_text'] ]],
                                    [ "match"=> [ "lastname"=> $params['search_text']   ]]
                                        ]
                                    ]
                            ]
                        ]
                ];

当我在PHP中使用弹性时。这里$ params ['武器']是:

array (size=2)
  0 => string '1' (length=1)
  1 => string '2' (length=1)

我有一个错误:

illegal_state_exception:无法在1:36上获得start_array上的文本

任何建议/帮助将不胜感激,我可以如何匹配数组。我从嵌套的数据型

中获取了参考

更新#1 :我要发送到我的功能的参数: {"from":0,"size":null,"city":null,"state":"0","weapons":["1","2"],"activities":[],"species":[],"properties":[],"search_text":"lastname"}

更新#2 :我的查询的正文以JSON格式:

{
    "index": "abc_local",
    "type": "users",
    "body": {
        "query": {
            "bool": {
                "must": {
                    "match": {
                        "weapons.id": ["1", "2"]
                    }
                },
                "should": [{
                    "match": {
                        "firstname": "simer"
                    }
                }, {
                    "match": {
                        "lastname": "simer"
                    }
                }]
            }
        }
    }
}

您可以简单地将第一个match查询替换为 terms一个,因为 match不适用于值。

 $params = [
        'index' => Constants::INDEX,
        'type' => Constants::DOC_TYPE_USERS,
        'body' => [
            "query"=> [
                "bool"=> [
                    "must"=>   [ "terms"=> [ "weapons.id"=>$params['weapons'] ]],
                                    ^
                                    |
                               change this
                    "should"=> [
                                [ "match"=> [ "firstname"=> $params['search_text'] ]],
                                [ "match"=> [ "lastname"=> $params['search_text']   ]]
                                    ]
                                ]
                        ]
                    ]
            ];

如果您想检查来自数组的任何值是否从索引到字段,则必须"术语"而不是匹配。

{
    "index": "abc_local",
    "type": "users",
    "body": {
        "query": {
            "bool": {
                "must": {
                    "terms": {
                        "weapons.id": ["1", "2"]
                    }
                },
                "should": [{
                    "match": {
                        "firstname": "simer"
                    }
                }, {
                    "match": {
                        "lastname": "simer"
                    }
                }]
            }
        }
    }
}

请参阅Elasticsearch文档中的"术语级查询"。

最新更新