gpt4 book ai didi

elasticsearch - Elasticsearch 中的匹配数组

转载 作者:行者123 更新时间:2023-11-29 02:53:57 29 4
gpt4 key购买 nike

我有如下文件:

{
"_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['weapons'] 是:

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

我得到一个错误:

illegal_state_exception: Can't get text on a START_ARRAY at 1:36

任何建议/帮助将不胜感激,我可以如何匹配数组。我引用了 nested datatypes

更新#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'] ]]
]
]
]
]

];

关于elasticsearch - Elasticsearch 中的匹配数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40735185/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com