为什么我不能正确查询 Elasticsearch 中的 HashMap 字段?

Why can't I query correctly against a HashMap field in Elasticsearch?

我正在使用 Elasticsearch v1.5.2。我有一个 JSON 文档,如下所示。

{
    "id": "RRRZe32",
    "metadata": {
        "published": "2010-07-29T18:11:43.000Z",
        "codeId": "AChdUxnsuRyoCo7roK6gqZSg",
        "codeTitle": "something"
    }
}

我的 Java 支持此 JSON 的 POJO 对象如下所示。请注意,我正在使用具有 spring-boot-starter-data-elasticsearch 依赖项的 Spring-Boot v1.3.0.M2。

@Document(indexName="ws", type="vid")
public class Video {
 @Id 
 private String id;


 @Field(type=FieldType.Object, index=FieldIndex.not_analyzed)
 private Map<String, Object> metadata;
}

我的映射定义如下。

{
    "ws": {
        "mappings": {
            "vid": {
                "properties": {
                    "id": {
                        "type": "string"
                    },
                    "metadata": {
                        "properties": {
                            "codeId": {
                                "type": "string"
                            },
                            "codeTitle": {
                                "type": "string"
                            }
                        }
                    }
                }
            }
        }
    }
}

我可以通过 metadata.codeTitle 但不能 metadata.codeId 成功查询文档(使用 Sense)。我对 metadata.codeTitle 的查询如下所示。

{
    "query": {
        "bool": {
            "must": [
                {
                    "term": {
                        "metadata.codeTitle": {
                            "value": "something"
                        }
                    }
                }
            ]
        }
    }
}

我对 metadata.codeId 的查询如下所示。

{
    "query": {
        "bool": {
            "must": [
                {
                    "term": {
                        "metadata.codeId": {
                            "value": "AChdUxnsuRyoCo7roK6gqZSg"
                        }
                    }
                }
            ]
        }
    }
}

对我做错了什么有什么想法吗?

这是因为您的 codeId 字段是 analyzed 并且该值在索引时小写。所以你有两个解决方案:

您可以这样查询(即全部小写)

{
    "query": {
        "bool": {
            "must": [
                {
                    "term": {
                        "metadata.codeId": {
                            "value": "achduxnsuryoco7rok6gqzsg"
                        }
                    }
                }
            ]
        }
    }
}

或者您可以将 codeId 字段声明为 not_analyzed 并保持查询不变。

在您的情况下,情况 1 看起来更容易实施。