elasticseach或查询逗号分隔的值

uelo1irk  于 2021-06-15  发布在  ElasticSearch
关注(0)|答案(1)|浏览(468)

我在数据库中以逗号分隔的形式保存id,并将其索引到elasticsearch。现在我需要检索用户id是否与值匹配。
例如,在用户id列的索引中这样保存(数据库类型是varchar(500),在elasticsearch中是text)
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
}

Map详细信息

{
    "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"
            }
        }
    }
}

我正在尝试上述逻辑,但不是无法检索。有人能帮忙吗。

gstyhher

gstyhher1#

从野外开始 user_ids 属于类型 text 默认情况下,没有为它指定任何分析器 standard 不会坏的分析仪 8938,8936,8937 按条款 8938 , 8936 以及 8937 因此id不匹配。
为了解决这个问题,我建议您将id数组存储到 user_ids 字段而不是csv。因此,在索引json输入时,应如下所示:

{
   ...

   "user_ids": [
      8938,
      8936,
      8937
   ]

   ...
}

由于用户ID是整数值,应在Map中进行以下更改:

{
   "user_ids": {
      "type": "integer"
   }
}

现在查询如下:

{
  "query": {
    "bool": {
      "filter": [
        [
          {
            "terms": {
              "userIds": [
                8936
              ]
            }
          }
        ]
      ],
      "must_not": [
        [
          {
            "exists": {
              "field": "deleted_at"
            }
          }
        ]
      ]
    }
  },
  "size": 10000
}

相关问题