一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專(zhuān)注于服務(wù)器技術(shù)及軟件下載分享
分類(lèi)導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - java 使用ElasticSearch完成百萬(wàn)級(jí)數(shù)據(jù)查詢(xún)附近的人功能

java 使用ElasticSearch完成百萬(wàn)級(jí)數(shù)據(jù)查詢(xún)附近的人功能

2021-03-19 12:09天涯淚小武 Java教程

本篇文章主要介紹了java 使用ElasticSearch完成百萬(wàn)級(jí)數(shù)據(jù)查詢(xún)附近的人功能,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

上一篇文章介紹了elasticsearch使用repository和elasticsearchtemplate完成構(gòu)建復(fù)雜查詢(xún)條件,簡(jiǎn)單介紹了elasticsearch使用地理位置的功能。

這一篇我們來(lái)看一下使用elasticsearch完成大數(shù)據(jù)量查詢(xún)附近的人功能,搜索n米范圍的內(nèi)的數(shù)據(jù)。

準(zhǔn)備環(huán)境

本機(jī)測(cè)試使用了elasticsearch最新版5.5.1,springboot1.5.4,spring-data-elasticsearch2.1.4.

新建springboot項(xiàng)目,勾選elasticsearch和web。

pom文件如下

java" id="highlighter_966970">
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?xml version="1.0" encoding="utf-8"?>
<project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
  xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelversion>4.0.0</modelversion>
 
  <groupid>com.tianyalei</groupid>
  <artifactid>elasticsearch</artifactid>
  <version>0.0.1-snapshot</version>
  <packaging>jar</packaging>
 
  <name>elasticsearch</name>
  <description>demo project for spring boot</description>
 
  <parent>
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boot-starter-parent</artifactid>
    <version>1.5.4.release</version>
    <relativepath/> <!-- lookup parent from repository -->
  </parent>
 
  <properties>
    <project.build.sourceencoding>utf-8</project.build.sourceencoding>
    <project.reporting.outputencoding>utf-8</project.reporting.outputencoding>
    <java.version>1.8</java.version>
  </properties>
 
  <dependencies>
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-data-elasticsearch</artifactid>
    </dependency>
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-web</artifactid>
    </dependency>
 
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-test</artifactid>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupid>com.sun.jna</groupid>
      <artifactid>jna</artifactid>
      <version>3.0.9</version>
    </dependency>
  </dependencies>
 
  <build>
    <plugins>
      <plugin>
        <groupid>org.springframework.boot</groupid>
        <artifactid>spring-boot-maven-plugin</artifactid>
      </plugin>
    </plugins>
  </build> 
</project>

新建model類(lèi)person

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.tianyalei.elasticsearch.model; 
import org.springframework.data.annotation.id;
import org.springframework.data.elasticsearch.annotations.document;
import org.springframework.data.elasticsearch.annotations.geopointfield;
 
import java.io.serializable;
 
/**
 * model類(lèi)
 */
@document(indexname="elastic_search_project",type="person",indexstoretype="fs",shards=5,replicas=1,refreshinterval="-1")
public class person implements serializable {
  @id
  private int id;
 
  private string name;
 
  private string phone;
 
  /**
   * 地理位置經(jīng)緯度
   * lat緯度,lon經(jīng)度 "40.715,-74.011"
   * 如果用數(shù)組則相反[-73.983, 40.719]
   */
  @geopointfield
  private string address;
 
  public int getid() {
    return id;
  }
 
  public void setid(int id) {
    this.id = id;
  }
 
  public string getname() {
    return name;
  }
 
  public void setname(string name) {
    this.name = name;
  }
 
  public string getphone() {
    return phone;
  }
 
  public void setphone(string phone) {
    this.phone = phone;
  }
 
  public string getaddress() {
    return address;
  }
 
  public void setaddress(string address) {
    this.address = address;
  }
}

我用address字段表示經(jīng)緯度位置。注意,使用string[]和string分別來(lái)表示經(jīng)緯度時(shí)是不同的,見(jiàn)注釋。

?
1
2
3
4
5
import com.tianyalei.elasticsearch.model.person;
import org.springframework.data.elasticsearch.repository.elasticsearchrepository; 
public interface personrepository extends elasticsearchrepository<person, integer> {
 
}

看一下service類(lèi),完成插入測(cè)試數(shù)據(jù)的功能,查詢(xún)的功能我放在controller里了,為了方便查看,正常是應(yīng)該放在service里

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.tianyalei.elasticsearch.service; 
import com.tianyalei.elasticsearch.model.person;
import com.tianyalei.elasticsearch.repository.personrepository;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.data.elasticsearch.core.elasticsearchtemplate;
import org.springframework.data.elasticsearch.core.query.indexquery;
import org.springframework.stereotype.service;
import java.util.arraylist;
import java.util.list;
 
@service
public class personservice {
  @autowired
  personrepository personrepository;
  @autowired
  elasticsearchtemplate elasticsearchtemplate;
 
  private static final string person_index_name = "elastic_search_project";
  private static final string person_index_type = "person";
 
  public person add(person person) {
    return personrepository.save(person);
  }
 
  public void bulkindex(list<person> personlist) {
    int counter = 0;
    try {
      if (!elasticsearchtemplate.indexexists(person_index_name)) {
        elasticsearchtemplate.createindex(person_index_type);
      }
      list<indexquery> queries = new arraylist<>();
      for (person person : personlist) {
        indexquery indexquery = new indexquery();
        indexquery.setid(person.getid() + "");
        indexquery.setobject(person);
        indexquery.setindexname(person_index_name);
        indexquery.settype(person_index_type);
 
        //上面的那幾步也可以使用indexquerybuilder來(lái)構(gòu)建
        //indexquery index = new indexquerybuilder().withid(person.getid() + "").withobject(person).build();
 
        queries.add(indexquery);
        if (counter % 500 == 0) {
          elasticsearchtemplate.bulkindex(queries);
          queries.clear();
          system.out.println("bulkindex counter : " + counter);
        }
        counter++;
      }
      if (queries.size() > 0) {
        elasticsearchtemplate.bulkindex(queries);
      }
      system.out.println("bulkindex completed.");
    } catch (exception e) {
      system.out.println("indexerservice.bulkindex e;" + e.getmessage());
      throw e;
    }
  }
}

注意看bulkindex方法,這個(gè)是批量插入數(shù)據(jù)用的,bulk也是es官方推薦使用的批量插入數(shù)據(jù)的方法。這里是每逢500的整數(shù)倍就bulk插入一次。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package com.tianyalei.elasticsearch.controller; 
import com.tianyalei.elasticsearch.model.person;
import com.tianyalei.elasticsearch.service.personservice;
import org.elasticsearch.common.unit.distanceunit;
import org.elasticsearch.index.query.geodistancequerybuilder;
import org.elasticsearch.index.query.querybuilders;
import org.elasticsearch.search.sort.geodistancesortbuilder;
import org.elasticsearch.search.sort.sortbuilders;
import org.elasticsearch.search.sort.sortorder;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.data.domain.pagerequest;
import org.springframework.data.domain.pageable;
import org.springframework.data.elasticsearch.core.elasticsearchtemplate;
import org.springframework.data.elasticsearch.core.query.nativesearchquerybuilder;
import org.springframework.data.elasticsearch.core.query.searchquery;
import org.springframework.web.bind.annotation.getmapping;
import org.springframework.web.bind.annotation.restcontroller;
import java.text.decimalformat;
import java.util.arraylist;
import java.util.list;
import java.util.random;
 
@restcontroller
public class personcontroller {
  @autowired
  personservice personservice;
  @autowired
  elasticsearchtemplate elasticsearchtemplate;
 
  @getmapping("/add")
  public object add() {
    double lat = 39.929986;
    double lon = 116.395645;
    list<person> personlist = new arraylist<>(900000);
    for (int i = 100000; i < 1000000; i++) {
      double max = 0.00001;
      double min = 0.000001;
      random random = new random();
      double s = random.nextdouble() % (max - min + 1) + max;
      decimalformat df = new decimalformat("######0.000000");
      // system.out.println(s);
      string lons = df.format(s + lon);
      string lats = df.format(s + lat);
      double dlon = double.valueof(lons);
      double dlat = double.valueof(lats); 
      person person = new person();
      person.setid(i);
      person.setname("名字" + i);
      person.setphone("電話" + i);
      person.setaddress(dlat + "," + dlon);
      personlist.add(person);
    }
    personservice.bulkindex(personlist);
 
//    searchquery searchquery = new nativesearchquerybuilder().withquery(querybuilders.querystringquery("spring boot or 書(shū)籍")).build();
//    list<article> articles = elas、ticsearchtemplate.queryforlist(se、archquery, article.class);
//    for (article article : articles) {
//      system.out.println(article.tostring());
//    }
 
    return "添加數(shù)據(jù)";
  }
 
  /**
   *
   geo_distance: 查找距離某個(gè)中心點(diǎn)距離在一定范圍內(nèi)的位置
   geo_bounding_box: 查找某個(gè)長(zhǎng)方形區(qū)域內(nèi)的位置
   geo_distance_range: 查找距離某個(gè)中心的距離在min和max之間的位置
   geo_polygon: 查找位于多邊形內(nèi)的地點(diǎn)。
   sort可以用來(lái)排序
   */
  @getmapping("/query")
  public object query() {
    double lat = 39.929986;
    double lon = 116.395645
    long nowtime = system.currenttimemillis();
    //查詢(xún)某經(jīng)緯度100米范圍內(nèi)
    geodistancequerybuilder builder = querybuilders.geodistancequery("address").point(lat, lon)
        .distance(100, distanceunit.meters);
 
    geodistancesortbuilder sortbuilder = sortbuilders.geodistancesort("address")
        .point(lat, lon)
        .unit(distanceunit.meters)
        .order(sortorder.asc);
 
    pageable pageable = new pagerequest(0, 50);
    nativesearchquerybuilder builder1 = new nativesearchquerybuilder().withfilter(builder).withsort(sortbuilder).withpageable(pageable);
    searchquery searchquery = builder1.build(); 
    //queryforlist默認(rèn)是分頁(yè),走的是queryforpage,默認(rèn)10個(gè)
    list<person> personlist = elasticsearchtemplate.queryforlist(searchquery, person.class); 
    system.out.println("耗時(shí):" + (system.currenttimemillis() - nowtime));
    return personlist;
  }
}

看controller類(lèi),在add方法中,我們插入90萬(wàn)條測(cè)試數(shù)據(jù),隨機(jī)產(chǎn)生不同的經(jīng)緯度地址。

在查詢(xún)方法中,我們構(gòu)建了一個(gè)查詢(xún)100米范圍內(nèi)、按照距離遠(yuǎn)近排序,分頁(yè)每頁(yè)50條的查詢(xún)條件。如果不指明pageable的話,estemplate的queryforlist默認(rèn)是10條,通過(guò)源碼可以看到。

啟動(dòng)項(xiàng)目,先執(zhí)行add,等待百萬(wàn)數(shù)據(jù)插入,大概幾十秒。

然后執(zhí)行查詢(xún),看一下結(jié)果。

java 使用ElasticSearch完成百萬(wàn)級(jí)數(shù)據(jù)查詢(xún)附近的人功能

第一次查詢(xún)花費(fèi)300多ms,再次查詢(xún)后時(shí)間就大幅下降,到30ms左右,因?yàn)閑s已經(jīng)自動(dòng)緩存到內(nèi)存了。

可見(jiàn),es完成地理位置的查詢(xún)還是非??斓摹_m用于查詢(xún)附近的人、范圍查詢(xún)之類(lèi)的功能。

后記,在后來(lái)的使用中,elasticsearch2.3版本時(shí),按上面的寫(xiě)法出現(xiàn)了geo類(lèi)型無(wú)法索引的情況,進(jìn)入es的為string,而不是標(biāo)注的geofiled。在此記錄一下解決方法,將string類(lèi)型修改為geopoint,且是org.springframework.data.elasticsearch.core.geo.geopoint包下的。然后需要在創(chuàng)建index時(shí),顯式調(diào)用一下mapping方法,才能正確的映射為geofield。

如下

?
1
2
3
4
if (!elasticsearchtemplate.indexexists("abc")) {
      elasticsearchtemplate.createindex("abc");
      elasticsearchtemplate.putmapping(person.class);
    }

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:http://blog.csdn.net/tianyaleixiaowu/article/details/76177583

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产日韩欧美在线一二三四 | 国产一级在线观看 | 免费 视频 | 教室里的激情电影 | 欧美成人手机 | 久久er国产精品免费观看2 | www.一级片.com | 亚州笫一色惰网站 | yy8090韩国日本三理论免费 | 国产亚洲欧美日韩综合综合二区 | 免费大片a一级一级 | 肥胖女人一级毛片 | 亚洲一欧洲中文字幕在线 | 2021最新国产成人精品免费 | 国产在线激情视频 | 纲手被鸣人插 | 久久久免费观成人影院 | 欧美日韩在线观看精品 | 日本不卡1卡2卡三卡网站二百 | 轻轻色在线视频中文字幕 | 维修工的调教 | 超兴奋朋友的中文字幕下 | 国产大片视频免费观看 | 四虎2021地址入口 | 日韩天堂视频 | 免费刷10000名片赞网站 | 好深快点再快点好爽视频 | 青草网址 | 网友自拍咪咪爱 | 四虎最新永久免费视频 | 精品国产麻豆免费人成网站 | 1717she精品视频在线观看 | 99视频在线观看免费视频 | 亚洲精品午夜视频 | 91天堂一区二区 | 欧美老人与小伙子性生交 | 亚洲99久久无色码中文字幕 | www视频在线免费观看 | 日本艳鉧动漫1~6完整版在 | 深夜成人 | 性xx色3d动画xx无尽 |