在查询中使用大写字母时,elasticsearch 将不起作用

elasticsearch won't work when using uppercase letters in query

我正在使用 laravel 4.2。我的数据库是 mongodb。我有一个名为 products 的 table,在我的数据库中有一个名为 brand 的字段,其值为 ABC(大写)。

使用此查询时 ['term' => ['brand' => 'ABC']] 结果集为空。

但是当我尝试使用这个 ['term' => ['brand' => 'abc']] 时,它实际上正在工作并返回所有带有 brand = 'ABC' 的产品。

我的问题是为什么elasticsearch找不到大写字母?

这是因为您的 brand 字段已被分析,因此 ABC 被标记化并索引为 abc,因此为什么要搜索 term abc returns 命中而 ABC 没有。

为了改变这种行为,您可以为 brand 字段创建一个 not_analyzed 的子字段,这样您也可以搜索大写形式。

curl -XPUT localhost:9200/my_index/my_type/_mapping -d '{
    "properties": {
        "brand": {
            "type": "string",
            "fields": {
                "raw": {
                    "type": "string",
                    "index": "not_analyzed"
                }
            }
        }
    }
}'

如果您 运行 上面的命令并重新索引您的数据,您将能够搜索

['term' => ['brand.raw' => 'ABC']]

请注意,您还可以在 brand 字段中使用 match 查询(见下文),它们将 return 匹配。

['match' => ['brand' => 'abc']]
['match' => ['brand' => 'ABC']]