Java uses elasticsearch to complete the function of querying people near millions of data

In the previous article, elasticsearch used repository and elasticsearchtemplate to build complex query conditions, and briefly introduced the function of elasticsearch using geographic location.

In this article, let's take a look at using elasticsearch to complete the function of querying nearby people with a large amount of data and search data within n meters.

Prepare environment

The native test uses the latest version of elasticsearch 5.5 1,SpringBoot1. 5.4,spring-data-ElasticSearch2. 1.4.

Create a new springboot project and check elastic search and web.

POM files are as follows

Create a new model class person

I use the address field to represent the latitude and longitude position. Note that it is different when using string [] and string to represent longitude and latitude respectively. See notes.

Take a look at the service class to complete the function of inserting test data. I put the query function in the controller. In order to facilitate viewing, it should normally be placed in the service

Pay attention to the bulkindex method, which is used to insert data in batches. Bulk is also the method of inserting data in batches recommended by ES officials. Here, the bulk is inserted every integer multiple of 500.

Look at the controller class. In the add method, we insert 900000 test data to randomly generate different longitude and latitude addresses.

In the query method, we constructed a query condition within 100 meters, sorted by distance, and paged 50 items per page. If pageable is not specified, the queryforlist of estemplate defaults to 10, which can be seen from the source code.

Start the project, execute add first, and wait for millions of data to be inserted, about tens of seconds.

Then execute the query and look at the results.

The first query takes more than 300 ms. after the second query, the time decreases significantly to about 30 ms, because the ES has been automatically cached in memory.

It can be seen that ES completes the query of geographical location very quickly. It is applicable to functions such as querying nearby people and range query.

Postscript, in later use, elasticsearch2 In version 3, the geo type cannot be indexed according to the above writing method. The entered es is string instead of the annotated geofile. Record the solution here. Change the string type to geopoint and org springframework. data. elasticsearch. core. geo. Under the geopoint package. Then you need to explicitly call the mapping method when creating the index to correctly map to geofield.

as follows

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>