有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

ElasticSearch中的java嵌套搜索不起作用?

我想把所有国家名为“印度”的州都叫来。 为什么我不能用弹性搜索搜索嵌套数据

异常:嵌套:QueryParsingException[[userinfo][nested]路径[country]下的嵌套对象不是嵌套类型];}]


我正在执行的查询:

userinfo是索引名,state是类型

        POST /userinfo/state/_search?v
        {
            "query": {
                "nested": {

                   "path": "country",
                   "query": {
                       "match": {
                          "country.name": "India"
                       }
                   }
                }
            }
        }

        ------------------------------
        My Json is for state type : 

        {
                    "_index": "userinfo",
                    "_type": "state",
                    "_id": "35",
                    "_score": 1,
                    "_source": {
                       "id": "35",
                       "name": "County Down",
                       "abbreviation": "DWN",
                       "isDeleted": 0,
                       "country": {
                          "id": "3",
                          "name": "United Kingdom",
                          "abbreviation": "UK",
                          "isDeleted": 0
                       }
                    }
                 }

        -------------------------------------

 </br></br>
        State.java

        package com.elastic.entity;

        import org.springframework.data.annotation.Id;
        import org.springframework.data.elasticsearch.annotations.Document;
        import org.springframework.data.elasticsearch.annotations.Field;
        import org.springframework.data.elasticsearch.annotations.FieldType;

        @Document(indexName = "userinfo", type = "state")
        public class State {


            @Id
            private String id;

            private String name;
            private String abbreviation;
            private int isDeleted;

            @Field(type = FieldType.Nested)
            private Country country;



            public State()
            {
                super();
            }

            public State(String id, String name, String abbreviation, int isDeleted, Country country) {
                super();
                this.id = id;
                this.name = name;
                this.abbreviation = abbreviation;
                this.isDeleted = isDeleted;
                this.country = country;
            }


            public String getId() {
                return id;
            }
            public void setId(String id) {
                this.id = id;
            }
            public String getName() {
                return name;
            }
            public void setName(String name) {
                this.name = name;
            }
            public String getAbbreviation() {
                return abbreviation;
            }
            public void setAbbreviation(String abbreviation) {
                this.abbreviation = abbreviation;
            }
            public int getIsDeleted() {
                return isDeleted;
            }
            public void setIsDeleted(int isDeleted) {
                this.isDeleted = isDeleted;
            }
            public Country getCountry() {
                return country;
            }
            public void setCountry(Country country) {
                this.country = country;
            }




        }

共 (2) 个答案

  1. # 1 楼答案

    Try This with match : 
    
    POST /index/type/_search?v
    {
        "query": {
            "match": {
               "state.country.name": "India"
            }
        }
    }
    
  2. # 2 楼答案

    这将为您提供所有有关印度的文件

    {
      "query": {
        "query_string": {
          "default_field": "country.name"
          , "query": "India"
        }
      }
    }