我将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"
}
}
}
}我正在尝试上述逻辑,但并不是无法检索。有人能帮上忙吗。
发布于 2019-01-02 14:10:51
由于字段user_ids是text类型,因此默认情况下没有为其指定任何分析器,它将使用standard分析器,它不会将8938,8936,8937分解为术语8938、8936和8937,因此id不能匹配。
为了解决这个问题,我建议您将I数组存储到user_ids字段,而不是csv。因此,在索引您的json输入时,应该如下所示:
{
...
"user_ids": [
8938,
8936,
8937
]
...
}由于用户in是整数值,所以在映射中应该进行更改:
{
"user_ids": {
"type": "integer"
}
}现在查询如下:
{
"query": {
"bool": {
"filter": [
[
{
"terms": {
"userIds": [
8936
]
}
}
]
],
"must_not": [
[
{
"exists": {
"field": "deleted_at"
}
}
]
]
}
},
"size": 10000
}https://stackoverflow.com/questions/54005056
复制相似问题