Geographic location maintenance and retrieval case sharing of elastic search

Posted by Dean Glass on Fri, 03 Apr 2020 00:43:09 +0200

Elasticsearch geographic location information maintenance and retrieval case sharing

1. Preparation

Reference documents< Introduction of high performance elastic search ORM development library >Import and configure es client to project

2. Define mapping with geographic location type

Create a city index table structure, the type of field location is geo point, and define a retrieved dsl statement

Create the file esmapper/address.xml in the resources directory, as follows:

<properties>
<property name="createCityIndice"><![CDATA[{
  "settings": {
         "number_of_shards": 6,
         "index.refresh_interval": "5s"
  },
  "mappings": {
                "city": {
                    "properties": {
                        "standardAddrId":{
                            "type":"keyword"
                        },
                        "detailName": {
                            "type": "text",
                             
                            "fields": {
                                "keyword": {
                                    "type": "keyword"
                                }
                            }
                        },
                        "cityName":{
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword"
                                }
                            }
                        },
                        "countyName":{
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword"
                                }
                            }
                        },
                        "location":{
                            "type": "geo_point"
                        }

                    }
                }
            }
}]]></property>
<property name="locationSearch"><![CDATA[{
        "size": 100,
        "query": {
            "bool": {
                "must": [
                    {
                          "match_phrase_prefix" : {
                                "detailName" : {
                                    "query" : #[detailName]
                                }
                            }

                    },
                    {
                        "geo_distance": {
                            "distance": #[distance],
                            "location": {
                                "lon": #[lon],
                                "lat": #[lat]
                            }
                        }
                    }
                ]
            }
        }
    }]]></property>
</properties>

Create index table

//Create the client tool to load the configuration file. Single instance and multithreading are safe. It's a bit slow to preload the first run
		ClientInterface clientUtil = ElasticSearchHelper.getConfigRestClientUtil("esmapper/address.xml");
		try {
			//Delete mapping first
			clientUtil.dropIndice("city");
		} catch (ElasticSearchException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//Recreate mapping
		clientUtil.createIndiceMapping("city",//Index table name
				"createCityIndice");//Index table mapping dsl script name, define createCityIndice in esmapper/address.xml

3. Add index document

Map<String,String> params = new HashMap<String,String>();
		params.put("cityName","Tan City");
		params.put("standardAddrId","38130122");
		params.put("detailName","No.4 living quarter, No.4, Jianshe Road, Huayuan office, Guixi City-11 1010, 1st floor, unit 33, building");
		params.put("location","28.292781,117.238963");
		params.put("countyName","China");
ClientInterface clientUtil = ElasticSearchHelper.getRestClientUtil();				
clientUtil.addDocument("city",//Index name
                       "city",//Index type
                        params);//Index data object

4. Geographic location search

ClientInterface clientUtil = ElasticSearchHelper.getConfigRestClientUtil("esmapper/address.xml");
		Map<String,String> params = new HashMap<String,String>();
		params.put("detailName","Room 302, floor 3, unit 1, building 160, Xiangting, Haihai District");
		params.put("distance","0.5km");
		params.put("lon","115.824994");
		params.put("lat","28.666162");
//Returns the list of map objects, or other entity objects
		ESDatas<Map> datas = clientUtil.searchList("city/_search","locationSearch",params,Map.class);
//Return json message
		System.out.print(clientUtil.executeRequest("city/_search","locationSearch",params));

5. Reference documents

For more bboss use documents, please refer to:

https://my.oschina.net/bboss/blog/1556866

Topics: xml ElasticSearch JSON