如何查询我尚未为其定义映射的字段

how to make a query on a field I have not defined a mapping for

我有一个要添加到 b运行ds 的字段 current_country,它尚未在我的 elasticsearch 映射中定义。

我想对此进行过滤查询,因为它没有定义我想它没有被分析并且术语查询应该有效。

这是我正在做的查询

{
    "index": "products",
    "type": "brand",
    "body": {
        "from": 0,
        "size": 100,
        "sort": [
            {
                "n_name": "asc"
            }
        ],
        "query": {
            "filtered": {
                "query": {
                    "function_score": {
                        "filter": {
                            "bool": {
                                "must": [
                                    {
                                        "term": {
                                            "current_country": "DK"
                                        }
                                    }
                                ]
                            }
                        }
                    }
                }
            }
        }
    }
}

其中 returns 索引中没有文档。

我运行下面的查询来检查当前国家/地区是否存在

{
    "index": "products",
    "type": "brand",
    "body": {
        "from": 0,
        "size": 100,
        "sort": [
            {
                "n_name": "asc"
            }
        ],
        "query": {
            "filtered": {
                "query": {
                    "function_score": {
                        "filter": {
                            "bool": {
                                "must": [
                                    {
                                        "exists": {
                                            "field": "current_country"
                                        }
                                    }
                                ]
                            }
                        }
                    }
                }
            }
        }
    }
}

其中returns一共693个文件。

这是索引中的示例文档,当我 运行 上面的查询时返回。

{
    "_index": "products",
    "_type": "brand",
    "_id": "195da951241478LuxoLivingbrand",
    "_score": null,
    "_source": {
        "categories": [
            "Bordlamper og designer bordlamper der giver liv og lys"
        ],
        "image": "http://www.fotoagent.dk/single_picture/11385/138/mega/and_tradition_flowerpot_bordlampe_lilla.jpg",
        "top_price": 1695,
        "low_price": 1695,
        "n_name": "&Tradition",
        "name": "&Tradition",
        "current_country": "DK",
        "current_currency": "DKK"
    }
}

如何查询 current_country(最好是过滤查询)。

如果您没有为字段定义任何映射,elasticsearch 会尝试将该字段检测为 string/date/numeric。如果它检测到该字段为字符串,那么它将使用默认分析器(标准分析器)来分析您的输入。由于 standard analyzer 使用小写标记过滤器,您的输入字符串被索引为 "dk"。由于术语过滤器不分析输入,"DK" 不会匹配 "dk".

可以通过多种方式解决。

  1. (hack) 您可以将输入过滤器项小写。这不适用于短语。

  2. (更好)为您的输入定义一个映射。您可以动态更改映射/ add new mapping easily