国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Elasticsearch ES實(shí)現(xiàn)GEO位置搜索

這篇具有很好參考價(jià)值的文章主要介紹了Elasticsearch ES實(shí)現(xiàn)GEO位置搜索。希望對大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

ES實(shí)現(xiàn)GEO位置搜索

Elasticsearch-7.15.2
附近查詢,也叫做距離查詢(geo_distance):查詢到指定中心點(diǎn)小于某個(gè)距離值的所有文檔。

創(chuàng)建索引 (my_geo),直接設(shè)置mapping

GEO字段的創(chuàng)建:添加一個(gè)字段location,類型為 geo_point。

GEO類型的字段是不能使用動態(tài)映射自動生成的,我們需要在創(chuàng)建索引時(shí)指定字段的類型為geo_point,geo_point 類型的字段存儲的經(jīng)緯度。

curl -X PUT http://192.168.11.21:9200/my_geo -H 'Content-Type:application/json' -d'
{
  "mappings": {
    "properties": {
      "name": {"type": "text"},
      "location": {"type":"geo_point"}
    }
  }
}'

插入2條數(shù)據(jù)

curl -X POST 192.168.11.21:9200/my_geo/_doc/1 -H 'Content-Type: application/json' -d '{
  "name": "路人甲北京站",
  "location": {
    "lat": 39.90279998006104,
    "lon": 116.42703999493406
  }
}'

curl -X POST 192.168.11.21:9200/my_geo/_doc/2 -H 'Content-Type: application/json' -d '{
  "name": "路人乙朝陽公園",
  "location": {
    "lat": 39.93367367974064,
    "lon": 116.47845257733152
  }
}'
查詢語句 curl

我的位置在“工體”,“北京站”的路人甲和“朝陽公園”的路人乙都在5km的范圍內(nèi),查詢5km和3km范圍內(nèi)都有誰。

把范圍縮短distance改為3km,請求如下:

curl -XGET '192.168.11.21:9200/my_geo/_search?pretty=true' -H 'Content-Type:application/json' -d '
{
  "query":{
      "bool":{
          "must":{"match_all":{ }},
          "filter":{
              "geo_distance":{
                  "distance":"3km",
                  "location":{"lat": 39.93031708627304,"lon": 116.4470385453491}
              }
          }
      }
  }}'

結(jié)果:在“朝陽公園”的路人乙被搜索了出來。

{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {"total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0},
  "hits" : {"total" : { "value": 1, "relation" : "eq"},
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "my_geo",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "name" : "路人乙朝陽公園",
          "location" : {"lat" : 39.93367367974064,"lon": 116.47845257733152}
        }
      }
    ]
  }}

距離排序

5公里范圍內(nèi)排序查詢。

curl -XGET  'http://192.168.11.21:9200/my_geo/_search?pretty=true' -H 'Content-Type:application/json' -d '
{
  "query":{
      "bool":{
          "must":{
              "match_all":{ }
          },
          "filter":{
              "geo_distance":{ // 按距離搜索
                  "distance":"5km", // 搜索范圍
                  "location":{"lat": 39.93031708627304,"lon": 116.4470385453491} // 當(dāng)前緯度 經(jīng)度
              }
          }
      }
  },
    "sort": [
    {
      "_geo_distance": { // _geo_distance代表根據(jù)距離排序
        "location": { // 根據(jù)location存儲的經(jīng)緯度計(jì)算距離
            "lat": 39.93031708627304, // 當(dāng)前緯度 經(jīng)度
            "lon": 116.4470385453491
        },
        "order": "asc"
      }
    }
  ]
}' 

curl查詢結(jié)果:離我“工體”比較近的“路人乙”排在了第一個(gè),也是符合預(yù)期的。

{
  "took" : 10,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "my_geo",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : null,
        "_source" : {
          "name" : "路人乙",
          "location" : {
            "lat" : 39.93367367974064,
            "lon" : 116.47845257733152
          }
        },
        "sort" : [
          2704.400492813901
        ]
      },
      {
        "_index" : "my_geo",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : null,
        "_source" : {
          "name" : "路人甲",
          "location" : {
            "lat" : 39.90279998006104,
            "lon" : 116.42703999493406
          }
        },
        "sort" : [
          3503.0165324004943
        ]
      }
    ]
  }}

JAVA程序中使用GEO搜索

在定義實(shí)體類時(shí),對應(yīng)的GEO字段要使用特殊的類型。location的類型是GeoPoint,添加數(shù)據(jù)時(shí)轉(zhuǎn)成Json存儲。

import lombok.Data;
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;
import org.springframework.data.elasticsearch.annotations.GeoPointField;
import org.springframework.data.elasticsearch.core.geo.GeoPoint;

@Data
@Document(indexName = "my_geo")
public class MyGeo {

    @Field(type = FieldType.Keyword)
    private String goodsName;

    @Field(store = true)
    @GeoPointField
    private GeoPoint location;
}

geo距離查詢

    public void geoDistanceQuery(){
        //創(chuàng)建查詢請求對象
        SearchRequest request = new SearchRequest("my_geo");
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        GeoPoint geoPoint = new GeoPoint(39.93031708627304, 116.4470385453491);//工體的坐標(biāo)
        //geo距離查詢
        QueryBuilder queryBuilder = QueryBuilders.geoDistanceQuery("location")
                .distance(5, DistanceUnit.KILOMETERS)
                .point(geoPoint);

        sourceBuilder.query(queryBuilder);
        request.source(sourceBuilder);
        try {
            SearchResponse response = client.search(request, RequestOptions.DEFAULT);
            for(SearchHit hit : response.getHits().getHits()){
                
                System.out.println(hit.getSourceAsString());
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
結(jié)果:
{"name":"路人甲","location":{"lat":39.90279998006104,"lon":116.42703999493406}}
{"name":"路人乙","location":{"lat":39.93367367974064,"lon":116.47845257733152}}

距離排序

    public void geoDistanceSortQuery(){
        SearchRequest request = new SearchRequest("my_geo"); //創(chuàng)建查詢請求對象
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();

        GeoPoint geoPoint = new GeoPoint(39.93031708627304, 116.4470385453491);//工體的坐標(biāo)
        
        GeoDistanceSortBuilder sortBuilder = SortBuilders.geoDistanceSort("location", geoPoint).order(SortOrder.ASC);
        sourceBuilder.sort(sortBuilder);
        request.source(sourceBuilder);
        
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
            for(SearchHit hit : response.getHits().getHits()){
                System.out.println(hit.getSourceAsString());
    }
結(jié)果:
{"name":"路人乙","location":{"lat":39.93367367974064,"lon":116.47845257733152}}
{"name":"路人甲","location":{"lat":39.90279998006104,"lon":116.42703999493406}}

其他

距離排序(帶分頁)
GeoDistanceQueryBuilder

    /**
     *  ElasticSearchRepository和 RestHighLevelClient ElasticsearchRestTemplate的區(qū)別
     *  https://blog.csdn.net/zhiyikeji/article/details/128908596
     *
     *  從名字就能看出來,QueryBuilder主要用來構(gòu)建查詢條件、過濾條件,SortBuilder主要是構(gòu)建排序。
     *  譬如,我們要查詢距離某個(gè)位置100米范圍內(nèi)的所有人、并且按照距離遠(yuǎn)近進(jìn)行排序:
     */
    public void findGeoDistanceSort(){
        double lat = 39.93031708627304, lng = 116.4470385453491; //工體

        //設(shè)定搜索半徑
        GeoDistanceQueryBuilder queryBuilder = QueryBuilders.geoDistanceQuery("location")
                //.geoDistance(GeoDistance.PLANE)
                .point(lat, lng).distance(300, DistanceUnit.KILOMETERS);

        //計(jì)算距離多少公里 獲取點(diǎn)與點(diǎn)之間的距離
        GeoDistanceSortBuilder sortBuilder = SortBuilders.geoDistanceSort("location", lat, lng)
                .point(lat, lng).unit(DistanceUnit.METERS).order(SortOrder.ASC);

        Pageable pageable = PageRequest.of(0, 10);

        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder().withPageable(pageable)
                .withFilter(queryBuilder).withSort(sortBuilder);

        NativeSearchQuery nativeSearchQuery = builder.build();

        org.springframework.data.elasticsearch.core.SearchHits<MyGeo> searchHits = elasticsearchRestTemplate.search(nativeSearchQuery, MyGeo.class);
        List<org.springframework.data.elasticsearch.core.SearchHit<MyGeo>> searchHitList = searchHits.getSearchHits();
        if(searchHitList.isEmpty()){
            System.out.println("沒有查詢到數(shù)據(jù)!");
            return;
        }

        searchHitList.forEach(hit ->{
            // 此處的索引和查詢返回結(jié)果中sort集合的索引一致,目的在于取返回結(jié)果中的距離計(jì)算結(jié)果,以免二次計(jì)算,造成資源浪費(fèi)
            //Object geoDistance = hit.getSortValues().get(2);
            System.out.println("hit -- " + JSONObject.toJSONString(hit));
        });
    }

結(jié)果:
{"name":"路人乙","location":{"lat":39.93367367974064,"lon":116.47845257733152}}
{"name":"路人甲","location":{"lat":39.90279998006104,"lon":116.42703999493406}}

參考資料

ES7學(xué)習(xí)筆記(十三)GEO位置搜索
https://www.modb.pro/db/73991

ES GEO地理空間查詢 基于geo-point的多邊形查詢
https://huaweicloud.csdn.net/637eedd2df016f70ae4c9b19.html

通過ElasticsearchRestTemplate 完成地理搜索 矩形搜索,附近人搜索, 距離搜索
https://blog.csdn.net/qq_41712271/article/details/134881584

###復(fù)雜查詢包含ES按距離排序
https://blog.csdn.net/m0_56726104/article/details/120785048


geo 距離排序檢索
https://blog.csdn.net/wenxingchen/article/details/95448215/

GEO位置搜索 https://www.modb.pro/db/73991
ElasticsearchTemplate 經(jīng)緯度按距離排序 http://www.javashuo.com/article/p-uqiafsey-hx.html

ES 位置查詢之geo_point
https://blog.csdn.net/weixin_43918355/article/details/118366065文章來源地址http://www.zghlxwxcb.cn/news/detail-793945.html

到了這里,關(guān)于Elasticsearch ES實(shí)現(xiàn)GEO位置搜索的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • ElasticSearch系列 - SpringBoot整合ES:實(shí)現(xiàn)搜索結(jié)果排序 sort

    00. 數(shù)據(jù)準(zhǔn)備 01. Elasticsearch 默認(rèn)的排序方式是什么? ElasticSearch 默認(rèn)的排序方式是相關(guān)性排序。相關(guān)性排序是根據(jù)查詢條件與文檔的匹配程度來計(jì)算每個(gè)文檔的相關(guān)性得分,然后按照得分從高到低進(jìn)行排序。相關(guān)性排序是 ElasticSearch 中最常用的排序方式,因?yàn)樗梢愿鶕?jù)查詢

    2024年02月02日
    瀏覽(22)
  • Es elasticsearch 二十 站內(nèi)搜索示例 高亮內(nèi)容 java springboot 實(shí)現(xiàn)

    目錄 實(shí)現(xiàn)思路 代碼 全依賴 參數(shù)對象 搜索實(shí)現(xiàn)代碼全代碼 日志 重點(diǎn) 權(quán)重 分頁 入?yún)⒏吡翑?shù)據(jù)處理 返回出參數(shù)據(jù)處理 構(gòu)建請求 請求體設(shè)置搜索字段 返回?cái)?shù)據(jù)解析獲取高亮 高亮通過設(shè)置標(biāo)簽和class? 前端設(shè)置class字體顏色 也可直接寫在后端 ? 全依賴 參數(shù)對象 搜索實(shí)現(xiàn)代碼

    2024年02月02日
    瀏覽(29)
  • ES es Elasticsearch 十三 Java api 實(shí)現(xiàn)搜索 分頁查詢 復(fù)雜查詢 過濾查詢 ids查詢 等

    目錄 Java api 實(shí)現(xiàn)搜索 Pom.xml 建立鏈接 搜索全部記錄 增加規(guī)則值查某些字段 搜索分頁 全代碼 Ids 搜索 搜索Match搜索 multi_match 搜索 多字段搜索 復(fù)雜查詢 bool查詢 filter? bool 復(fù)雜查詢增加過濾器查詢 復(fù)雜擦好像加排序 日志 思路 參考 api 寫法 寫Java代碼 請求條件構(gòu)建層次

    2024年02月04日
    瀏覽(25)
  • elasticsearch[五]:深入探索ES搜索引擎的自動補(bǔ)全與拼寫糾錯(cuò):如何實(shí)現(xiàn)高效智能的搜索體驗(yàn)

    elasticsearch[五]:深入探索ES搜索引擎的自動補(bǔ)全與拼寫糾錯(cuò):如何實(shí)現(xiàn)高效智能的搜索體驗(yàn)

    前一章講了搜索中的拼寫糾錯(cuò)功能,里面一個(gè)很重要的概念就是萊文斯坦距離。這章會講解搜索中提升用戶體驗(yàn)的另一項(xiàng)功能 - [自動補(bǔ)全]。本章直接介紹 ES 中的實(shí)現(xiàn)方式以及真正的搜索引擎對自動補(bǔ)全功能的優(yōu)化。 大家對上面的這個(gè)應(yīng)該都不陌生,搜索引擎會根據(jù)你輸入的

    2024年01月24日
    瀏覽(33)
  • Elasticsearch的地理位置搜索與功能

    地理位置搜索是一種非常重要的搜索功能,它可以根據(jù)用戶的位置信息來提供相關(guān)的搜索結(jié)果。在現(xiàn)代的互聯(lián)網(wǎng)和移動應(yīng)用中,地理位置搜索已經(jīng)成為一種基本的功能需求。Elasticsearch是一個(gè)強(qiáng)大的搜索引擎,它提供了一套完善的地理位置搜索功能。在本文中,我們將深入探討

    2024年02月21日
    瀏覽(40)
  • Elasticsearch 基于地理位置的搜索查詢

    ? ? ? ? ?ES為用戶提供了基于地理位置的搜索功能。它主要支持兩種類型的地理查詢:一種是地理點(diǎn)(geo_point),即經(jīng)緯度查詢,另一種是地理形狀查詢(geo_shape),即支持點(diǎn),線,圓形和多邊形等查詢。 ? ? ? ? 從實(shí)用性來說,地理點(diǎn)(即geo_point)數(shù)據(jù)類型的使用更多一些,

    2024年02月12日
    瀏覽(21)
  • ElasticSearch系列 - SpringBoot整合ES:實(shí)現(xiàn)分頁搜索 from+size、search after、scroll

    01. 數(shù)據(jù)準(zhǔn)備 ElasticSearch 向 my_index 索引中索引了 12 條文檔: 02. ElasticSearch 如何查詢所有文檔? ElasticSearch 查詢所有文檔 根據(jù)查詢結(jié)果可以看出,集群中總共有12個(gè)文檔,hits.total.value=12, 但是在 hits 數(shù)組中只有 10 個(gè)文檔。如何才能看到其他的文檔? 03. ElasticSearch 如何指定搜

    2023年04月08日
    瀏覽(28)
  • Java SpringBoot API 實(shí)現(xiàn)ES(Elasticsearch)搜索引擎的一系列操作(超詳細(xì))(模擬數(shù)據(jù)庫操作)

    Java SpringBoot API 實(shí)現(xiàn)ES(Elasticsearch)搜索引擎的一系列操作(超詳細(xì))(模擬數(shù)據(jù)庫操作)

    小編使用的是elasticsearch-7.3.2 基礎(chǔ)說明: 啟動:進(jìn)入elasticsearch-7.3.2/bin目錄,雙擊elasticsearch.bat進(jìn)行啟動,當(dāng)出現(xiàn)一下界面說明,啟動成功。也可以訪問http://localhost:9200/ 啟動ES管理:進(jìn)入elasticsearch-head-master文件夾,然后進(jìn)入cmd命令界面,輸入npm?run?start?即可啟動。訪問http

    2024年02月04日
    瀏覽(34)
  • ElasticSearch(ES) 搜索入門筆記

    ElasticSearch(ES) 搜索入門筆記

    ElasticSearch簡稱ES,經(jīng)過多年的發(fā)展,已是很流行的搜索工具了,無需多介紹,下面就粘一點(diǎn)官方介紹 You know, for search (and analysis) Elasticsearch is the distributed search and analytics engine at the heart of the Elastic Stack. Logstash and Beats facilitate collecting, aggregating, and enriching your data and storing it i

    2024年01月22日
    瀏覽(42)
  • 【ES專題】ElasticSearch搜索進(jìn)階

    【ES專題】ElasticSearch搜索進(jìn)階

    丑話說在前頭 ,說實(shí)在這篇筆記寫的不是很好,確實(shí)很多沒有實(shí)操。 系列上一篇文章:《【ES專題】ElasticSearch 高級查詢語法Query DSL實(shí)戰(zhàn)》 系列下一篇文章:《【ES專題】ElasticSearch集群架構(gòu)剖析》 理解ES的核心概念,最最重要的是【索引】和【文檔】 理解基本的Query DSL語法

    2024年01月16日
    瀏覽(17)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包