本文介绍了为什么我不能正确查询 Elasticsearch 中的 HashMap 字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 Elasticsearch v1.5.2.我有一个如下所示的 JSON 文档.

I am using Elasticsearch v1.5.2. I have a JSON document that looks like the following.

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

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

My Java POJO object that backs this JSON looks like the following. Note that I am using Spring-Boot v1.3.0.M2 with spring-boot-starter-data-elasticsearch dependency.

@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 成功查询文档(使用 Sense),但不能通过 metadata.codeId.我对 metadata.codeTitle 的查询如下所示.

I can query the document (using Sense) successfully by metadata.codeTitle but not metadata.codeId. My query for metadata.codeTitle looks like the following.

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

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

My query for metadata.codeId looks like the following.

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

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

Any ideas on what I am doing wrong?

推荐答案

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

It is because your codeId field is analyzed and the value is lowercased at indexing time. So you have two solutions:

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

You can query like this (i.e. all lowercase)

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

或者您可以将您的 codeId 字段声明为 not_analyzed 并保持查询原样.

Or you can declare your codeId field as not_analyzed and keep your query as it is.

就您而言,情况 1 看起来更容易实现.

In your case, it looks like case 1 will be easier to implement.

这篇关于为什么我不能正确查询 Elasticsearch 中的 HashMap 字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 04:44