Elasticsearch java API (23)查詢 DSL Geo查詢


地理查詢編輯

Elasticsearch支持兩種類型的地理數據: geo_point緯度/經度對字段的支持,和 geo_shape領域,支持點、線、圓、多邊形、多等。

這組查詢:

geo_shape 查詢
發現文檔與幾何圖型相交,包含,或與指定的geo-shape不相交。
geo_bounding_box 查詢
發現文檔與geo-points落入指定的矩形。
geo_distance 查詢
發現文檔geo-points內指定的中心點的距離。
geo_distance_range 查詢
就像  geo_point查詢,但是范圍是從一個指定的中心點的距離。
geo_polygon 查詢
發現文檔geo-points內指定的多邊形。
geohash_cell 查詢
找到的geo-points geohash相交的geohash指定點。

GeoShape查詢編輯

看到Geo形狀查詢

注意: geo_shape類型使用 Spatial4J JTS,這兩個都是可選的依賴性。因此您必須添加 Spatial4JJTS到類路徑中為了使用這種類型:

[java]  view plain  copy
 
  1. <dependency>  
  2.     <groupId>com.spatial4j</groupId>  
  3.     <artifactId>spatial4j</artifactId>  
  4.     <version>0.4.1</version>     <!--1-->                     
  5. </dependency>  
  6.   
  7. <dependency>  
  8.     <groupId>com.vividsolutions</groupId>  
  9.     <artifactId>jts</artifactId>  
  10.     <version>1.13</version>         <!--2-->                  
  11.     <exclusions>  
  12.         <exclusion>  
  13.             <groupId>xerces</groupId>  
  14.             <artifactId>xercesImpl</artifactId>  
  15.         </exclusion>  
  16.     </exclusions>  
  17. </dependency>  

檢查更新Maven中央

檢查更新Maven中央

[java]  view plain  copy
 
  1. // Import ShapeRelation and ShapeBuilder  
  2. import org.elasticsearch.common.geo.ShapeRelation;  
  3. import org.elasticsearch.common.geo.builders.ShapeBuilder;  
[java]  view plain  copy
 
  1. QueryBuilder qb = geoShapeQuery(  
  2.     "pin.location",               //1        
  3.     ShapeBuilder.newMultiPoint()      //2    
  4.         .point(0, 0)  
  5.         .point(0, 10)  
  6.         .point(10, 10)  
  7.         .point(10, 0)  
  8.         .point(0, 0),  
  9.     ShapeRelation.WITHIN);   //3             

 

形狀

關系可以 ShapeRelation.WITHIN, ShapeRelation.INTERSECTS ShapeRelation.DISJOINT

[java]  view plain  copy
 
  1. // Using pre-indexed shapes  
  2. QueryBuilder qb = geoShapeQuery(  
  3.         "pin.location",        //1       
  4.         "DEU",                    //2    
  5.         "countries",                //3  
  6.         ShapeRelation.WITHIN)       //4  
  7.     .indexedShapeIndex("shapes")    //5  
  8.     .indexedShapePath("location");  //6  

 

文檔的ID包含預先索引的形狀。

索引類型預先索引形狀在哪里。

關系

預先索引的索引的名稱,形狀。默認為 形狀.

包含預先索引的字段指定為路徑的形狀。默認為 形狀.

地理邊界框查詢編輯

 

看到地理邊界框查詢

[java]  view plain  copy
 
  1. QueryBuilder qb = geoBoundingBoxQuery("pin.location") //1  
  2.     .topLeft(40.73, -74.1)                //2              
  3.     .bottomRight(40.717, -73.99);        //3               

邊界框左上角點

邊界框右下角點

地理距離查詢編輯

看到地理距離查詢

[java]  view plain  copy
 
  1. QueryBuilder qb = geoDistanceQuery("pin.location")  //1  
  2.     .point(40, -70)                                 //2  
  3.     .distance(200, DistanceUnit.KILOMETERS)         //3  
  4.     .optimizeBbox("memory")                         //4  
  5.     .geoDistance(GeoDistance.ARC);                  //5  

中心點

距離中心點

優化邊界框: memory, indexed none

距離計算模式: GeoDistance.SLOPPY_ARC(默認), GeoDistance.ARC或(更精確,但明顯慢)GeoDistance.PLANE(更快,但不准確的長距離和接近兩極)

地理距離范圍查詢編輯

看到地理距離范圍查詢

[java]  view plain  copy
 
  1. QueryBuilder qb = geoDistanceRangeQuery("pin.location")      //1     
  2.     .point(40, -70)                                             //2  
  3.     .from("200km")                                              //3  
  4.     .to("400km")                                                //4  
  5.     .includeLower(true)                                         //5  
  6.     .includeUpper(false)                                        //6  
  7.     .optimizeBbox("memory")                                     //7  
  8.     .geoDistance(GeoDistance.ARC);                              //8  

 

中心點

距離中心點開始

結束中心點的距離

包括意味着更低的價值 from gt false gte true

包括上意味着價值 to lt false lte true

優化邊界框: memory, indexed none

距離計算模式: GeoDistance.SLOPPY_ARC(默認), GeoDistance.ARC或(更精確,但明顯慢)GeoDistance.PLANE(更快,但不准確的長距離和接近兩極)

Geo多邊形查詢編輯

看到Geo多邊形查詢

[java]  view plain  copy
 
  1. QueryBuilder qb = geoPolygonQuery("pin.location")    //1     
  2.     .addPoint(40, -70)                                  //2  
  3.     .addPoint(30, -80)                                  //3  
  4.     .addPoint(20, -90);                                 //4  

 

添加一個文檔應落在多邊形的點

Geohash細胞查詢編輯

看到Geohash細胞查詢

[java]  view plain  copy
 
  1. QueryBuilder qb = geoHashCellQuery("pin.location",  //1  
  2.             new GeoPoint(13.4080, 52.5186))         //2  
  3.         .neighbors(true)                            //3  
  4.         .precision(3);                              //4  

點。也可以是一個散列 u30

 neighbors選擇過濾提供了可能性的篩選細胞旁邊給定的細胞。

精度水平

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM