我将ID以逗号分隔的形式保存在数据库中,并将其索引到ElasticSearch。现在,我需要检索user_id是否与值匹配。
例如,它将这样保存在user_ids列的索引中(elasticsearch中的数据库类型是varchar(500),它是文本)
8938,8936,8937
$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"
}
}
}
}
我正在尝试上述逻辑,但无法检索。有人可以帮忙吗?
答案 0 :(得分:1)
由于字段user_ids
的类型为text
,因此默认情况下未为其指定任何分析器,它将使用standard
分析器,该分析器不会将8938,8936,8937
分解为{ {1}},8938
和8936
,因此ID无法匹配。
要解决此问题,建议您将ID数组存储到8937
字段中,而不要存储到csv中。因此,在为您建立索引时,json输入应如下所示:
user_ids
由于用户ID是整数值,因此应在映射中进行以下更改:
{
...
"user_ids": [
8938,
8936,
8937
]
...
}
现在查询如下:
{
"user_ids": {
"type": "integer"
}
}