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

爷,独闯天下 提交于 2019-12-12 00:29:19

问题


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

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;
}

My mapping is defined as follows.

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

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

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?


回答1:


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

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

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



来源:https://stackoverflow.com/questions/32044307/why-cant-i-query-correctly-against-a-hashmap-field-in-elasticsearch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!