Elasticsearch对复杂嵌套查询的应用-nested

创建索引

PUT /area
{
  "mappings": {
    "_doc": {
      "properties": {
        "province": {
          "type": "nested",
          "properties": {
            "name": {
              "type": "text",
              "analyzer": "ik_max_word"
            },
            "cities": {
              "type": "nested",
              "properties": {
                  "name": {
                "type": "text",
                "analyzer": "ik_max_word"
              },
                "district": {
                  "type": "nested",
                  "properties": {
                    "name": {
                      "type": "text",
                      "analyzer": "ik_max_word"
                    }
                  }
                }
              }
            
            }
          }
        }
      }
    }
  }
}

插入示例数据

PUT /area/_doc/1
{
  "province": {
    "name": "北京",
    "cities": [
      {
        "name": "北京市",
        "district": [
          {
            "name": "朝阳区"
          },
          {
            "name": "海淀区"
          },
          {
            "name": "丰台区"
          },
          {
            "name": "大兴区"
          },
          {
            "name": "房山区"
          },
          {
            "name": "通州区"
          },
           {
            "name": "延庆区"
          },
           {
            "name": "昌平区"
          },
           {
            "name": "密云区"
          },
           {
            "name": "平谷区"
          }
        ]
      }
    ]
  }
}

示例2

PUT /area/_doc/2
{
  "province": {
    "name": "辽宁省",
    "cities": [
      {
        "name": "沈阳市",
        "district": [
          {
            "name": "和平区"
          },
          {
            "name": "皇姑区"
          },
          {
            "name": "沈河区"
          },
          {
            "name": "铁西区"
          },
          {
            "name": "浑南区"
          },
          {
            "name": "苏家屯区"
          },
           {
            "name": "沈北新区"
          },
           {
            "name": "大东区"
          },
           {
            "name": "辽中区"
          },
           {
            "name": "于洪区"
          }
        ]
      },
      {
        "name":"葫芦岛市",
       "district":[
         {"name":"连山区"},
         {"name":"龙港区"},
         {"name":"男票区"}
         ]
      }
    ]
  }
}

使用java完成对复杂嵌套的搜索

package com.cn.service.impl;

import com.cn.ES.ESManager;
import org.apache.lucene.search.join.ScoreMode;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.index.query.*;

public class ProvinceInfoSearch {

    public static void search() throws Exception {
        String indexName = "area";
        //ES连接
        Client client = ESManager.ESClient();
        String path="province.cities.district.name";

        MatchQueryBuilder  districtQuery= QueryBuilders.matchQuery("province.cities.district.name", "浑南");

        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        boolQueryBuilder.should(QueryBuilders.matchQuery("province.cities.name","北京"));
        NestedQueryBuilder districtQueryNested = QueryBuilders.nestedQuery("province.cities.district", districtQuery, ScoreMode.None);
        boolQueryBuilder.should(districtQueryNested);
        NestedQueryBuilder province_city = QueryBuilders.nestedQuery("province.cities", boolQueryBuilder, ScoreMode.None);

        NestedQueryBuilder provinceQueryBuilder = QueryBuilders.nestedQuery("province", province_city, ScoreMode.None);
        SearchRequestBuilder srb = client.prepareSearch(indexName);
        SearchResponse searchResponse = srb.setQuery(provinceQueryBuilder).get();
        System.out.println(1111);

    }

}


查询语句:

GET area/_search
{
  "query": {
    "nested": {
      "path": "province",
      "query": {
        "nested": {
          "path": "province.cities",
          "query": {
            "bool": {
              "should": [
                {
                  "match": {
                    "province.cities.name": "北京"
                  }
                },
                {
                 "nested": {
                   "path": "province.cities.district",
                   "query": {
                     "match": {
                       "province.cities.district.name": "浑南"
                     }
                   }
                 }
                }
              ]
            }
            
            
          }
        }
        
      }
    }
    
    
  }
}

searchResponse 中包含的查询结果:

{
    "took": 15,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "_clusters": {
        "total": 0,
        "successful": 0,
        "skipped": 0
    },
    "hits": {
        "total": 2,
        "max_score": 0,
        "hits": [
            {
                "_index": "area",
                "_type": "_doc",
                "_id": "2",
                "_score": 0,
                "_source": {
                    "province": {
                        "name": "辽宁省",
                        "cities": [
                            {
                                "name": "沈阳市",
                                "district": [
                                    {
                                        "name": "和平区"
                                    },
                                    {
                                        "name": "皇姑区"
                                    },
                                    {
                                        "name": "沈河区"
                                    },
                                    {
                                        "name": "铁西区"
                                    },
                                    {
                                        "name": "浑南区"
                                    },
                                    {
                                        "name": "苏家屯区"
                                    },
                                    {
                                        "name": "沈北新区"
                                    },
                                    {
                                        "name": "大东区"
                                    },
                                    {
                                        "name": "辽中区"
                                    },
                                    {
                                        "name": "于洪区"
                                    }
                                ]
                            },
                            {
                                "name": "葫芦岛市",
                                "district": [
                                    {
                                        "name": "连山区"
                                    },
                                    {
                                        "name": "龙港区"
                                    },
                                    {
                                        "name": "男票区"
                                    }
                                ]
                            }
                        ]
                    }
                }
            },
            {
                "_index": "area",
                "_type": "_doc",
                "_id": "1",
                "_score": 0,
                "_source": {
                    "province": {
                        "name": "北京",
                        "cities": [
                            {
                                "name": "北京市",
                                "district": [
                                    {
                                        "name": "朝阳区"
                                    },
                                    {
                                        "name": "海淀区"
                                    },
                                    {
                                        "name": "丰台区"
                                    },
                                    {
                                        "name": "大兴区"
                                    },
                                    {
                                        "name": "房山区"
                                    },
                                    {
                                        "name": "通州区"
                                    },
                                    {
                                        "name": "延庆区"
                                    },
                                    {
                                        "name": "昌平区"
                                    },
                                    {
                                        "name": "密云区"
                                    },
                                    {
                                        "name": "平谷区"
                                    }
                                ]
                            }
                        ]
                    }
                }
            }
        ]
    }
}

你可能感兴趣的:(elasticsearch,中文分词)