Php ElasticSearch或查询逗号分隔的值
我在数据库中以逗号分隔的形式保存id,并将其索引到ElasticSearch。现在我需要检索用户id是否与该值匹配 例如,在为列user_id编制索引时这样保存(数据库类型为varchar(500),在elasticsearch中为text) 893889368937Php ElasticSearch或查询逗号分隔的值,php,elasticsearch,elasticsearch-6,Php,elasticsearch,Elasticsearch 6,我在数据库中以逗号分隔的形式保存id,并将其索引到ElasticSearch。现在我需要检索用户id是否与该值匹配 例如,在为列user_id编制索引时这样保存(数据库类型为varchar(500),在elasticsearch中为text) 893889368937 $userId = 8936; // For example expecting to return that row $whereCondition = []; $whereCondition[] = [
$userId = 8936; // For example expecting to return that row
$whereCondition = [];
$whereCondition[] = [
"query_string" => [
"query"=> $userId,
"default_field" => "user_ids",
"default_operator" => "OR"
]
];
$searchParams = [
'query' => [
'bool' => [
'must' => [
$whereCondition
],
'must_not' => [
['exists' => ['field' => 'deleted_at']]
]
]
],
"size" => 10000
];
User::search($searchParams);
Json查询
{
"query": {
"bool": {
"must": [
[{
"query_string": {
"query": 8936,
"default_field": "user_ids",
"default_operator": "OR"
}
}]
],
"must_not": [
[{
"exists": {
"field": "deleted_at"
}
}]
]
}
},
"size": 10000
}
映射细节
{
"user_details_index": {
"aliases": {},
"mappings": {
"test_type": {
"properties": {
"created_at": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss"
},
"deleted_at": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss"
},
"updated_at": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss"
},
"user_ids": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
},
"settings": {
"index": {
"creation_date": "1546404165500",
"number_of_shards": "5",
"number_of_replicas": "1",
"uuid": "krpph26NTv2ykt6xE05klQ",
"version": {
"created": "6020299"
},
"provided_name": "user_details_index"
}
}
}
}
我正在尝试上述逻辑,但并非无法检索。有人能帮上忙吗。由于字段
用户id
是文本类型
默认情况下没有为其指定任何分析器,它将使用标准分析器,该分析器不会将893889368937
分解为8938
、8936
和8937
,因此id无法匹配
为了解决这个问题,我建议您将ID数组存储到user\u id
字段,而不是csv。因此,在索引json输入时,应如下所示:
{
...
"user_ids": [
8938,
8936,
8937
]
...
}
由于用户ID是整数值,应在映射中进行以下更改:
{
"user_ids": {
"type": "integer"
}
}
现在的查询如下所示:
{
"query": {
"bool": {
"filter": [
[
{
"terms": {
"userIds": [
8936
]
}
}
]
],
"must_not": [
[
{
"exists": {
"field": "deleted_at"
}
}
]
]
}
},
"size": 10000
}
请共享索引的映射。另外,请以elasticsearch的_SearchAPI接受的json格式共享查询。这将使这里的人更容易理解查询。@NishantSaini更新了问题,请同时添加索引映射。要获取映射,请使用get/\u mappings
@NishantSaini未应用该列的映射属性默认设置为什么使用逗号分隔的值,在这样做之前是否考虑过嵌套字段?在尝试像输入字符串的“number\u format\u exception”类型那样执行操作时获取数字格式异常:\“[8936]\“”有时记录它只有一个值,而有时它有多个值。它不应该作为字符串传递,而应该是数组。谢谢,我将在select中检查MySQL如何准备数组(因为我直接从MySQL行索引记录),这将解决此问题希望您使用PHP;从数据库中获取csv格式的ID后,使用$userIdsArray=explode(',',$useridsCsv)