Elasticsearch:Geoshape query

news/2024/7/7 6:40:55 标签: elasticsearch, 大数据, 搜索引擎, 全文检索

Geoshape 查询可以用于过滤使用 geo_shape 或 geo_point 类型索引的文档。

geo_shape 查询使用与 geo_shape 或 geo_point 映射相同的索引来查找具有与查询形状相关的形状的文档,并使用指定的空间关系:相交(intersect)、包含(contained)、包含(within)或不相交 (disjoin)。

该查询支持两种定义查询形状的方法,一种是提供整个形状定义,另一种是引用在另一个索引中预先索引的形状的名称。 下面通过示例定义了这两种格式。

内联形状定义

与 geo_point 类型类似,geo_shape 查询使用 GeoJSON 来表示形状。有关如何制作 GeoJSON,请参考我的另外一篇文章 “Elasticsearch:如何制作 GeoJSON 文件并进行地理位置搜索”。

sample.json

{
    "type": "FeatureCollection",
    "features": [
    {
        "type": "Feature",
        "properties": {},
        "geometry": {
            "type": "Polygon",
            "coordinates": [
                [
                    [ 
                      13.0,
                      52.0 
                    ],
                    [
                      14.0,
                      52.0
                    ],
                    [
                      14.0,
                      53.0
                    ],
                    [
                      13.0,
                      53.0
                    ],
                    [
                      13.0,
                      52.0
                    ]  
                  ]
            ]
        }
    }
]
}

给定以下索引,其中位置作为 geo_shape 字段:

PUT /example
{
  "mappings": {
    "properties": {
      "location": {
        "type": "geo_shape"
      }
    }
  }
}

POST /example/_doc?refresh
{
  "name": "Wind & Wetter, Berlin, Germany",
  "location": {
    "type": "point",
    "coordinates": [ 13.400544, 52.530286 ]
  }
}

以下查询将使用 Elasticsearch 的信封 GeoJSON 扩展查找点:

GET /example/_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_shape": {
          "location": {
            "shape": {
              "type": "envelope",
              "coordinates": [ [ 13.0, 53.0 ], [ 14.0, 52.0 ] ]
            },
            "relation": "within"
          }
        }
      }
    }
  }
}

类似地,可以在 geo_point 字段上查询上述查询。

PUT /example_points
{
  "mappings": {
    "properties": {
      "location": {
        "type": "geo_point"
      }
    }
  }
}

PUT /example_points/_doc/1?refresh
{
  "name": "Wind & Wetter, Berlin, Germany",
  "location": [13.400544, 52.530286]
}

使用相同的查询,将返回具有匹配 geo_point 字段的文档。

GET /example_points/_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_shape": {
          "location": {
            "shape": {
              "type": "envelope",
              "coordinates": [ [ 13.0, 53.0 ], [ 14.0, 52.0 ] ]
            },
            "relation": "intersects"
          }
        }
      }
    }
  }
}

预索引形状

该查询还支持使用已在另一个索引中建立索引的形状。 当你有预定义的形状列表并且你想要使用逻辑名称(例如 New Zealand 新西兰)引用该列表而不是每次都提供坐标时,这特别有用。 在这种情况下,只需提供:

  • id - 包含预索引形状的文档的 ID。
  • index - 预索引形状所在的索引的名称。 默认为 shapes。
  • path - 指定为包含预索引形状的路径的字段。 默认为 shape。
  • routing - 形状文档的路由(如果需要)。

以下是将过滤器与预索引形状一起使用的示例:

PUT /shapes
{
  "mappings": {
    "properties": {
      "location": {
        "type": "geo_shape"
      }
    }
  }
}

PUT /shapes/_doc/deu
{
  "location": {
    "type": "envelope",
    "coordinates" : [[13.0, 53.0], [14.0, 52.0]]
  }
}

GET /example/_search
{
  "query": {
    "bool": {
      "filter": {
        "geo_shape": {
          "location": {
            "indexed_shape": {
              "index": "shapes",
              "id": "deu",
              "path": "location"
            }
          }
        }
      }
    }
  }
}

空间关系

以下是搜索地理字段时可用的空间关系运算符的完整列表:

  • INTERSECTS -(默认)返回 geo_shape 或 geo_point 字段与查询几何图形相交的所有文档。
  • DISJOINT - 返回其 geo_shape 或 geo_point 字段与查询几何图形没有任何共同点的所有文档。
  • WITHIN - 返回 geo_shape 或 geo_point 字段位于查询几何图形内的所有文档。 不支持线几何形状。
  • CONTAINS - 返回 geo_shape 或 geo_point 字段包含查询几何图形的所有文档。

忽略未映射的

当设置为 true 时,ignore_unmapped 选项将忽略未映射的字段,并且不会匹配此查询的任何文档。 当查询可能具有不同映射的多个索引时,这非常有用。 当设置为 false(默认值)时,如果字段未映射,查询将引发异常。

注意

当数据在 geo_shape 字段中作为形状数组进行索引时,这些数组将被视为一个形状。 因此,以下请求是等效的。

PUT /test/_doc/1
{
  "location": [
    {
      "coordinates": [46.25,20.14],
      "type": "point"
    },
    {
      "coordinates": [47.49,19.04],
      "type": "point"
    }
  ]
}
PUT /test/_doc/1
{
  "location":
    {
      "coordinates": [[46.25,20.14],[47.49,19.04]],
      "type": "multipoint"
    }
}

geo_shape 查询假定 geo_shape 字段使用默认方向 RIGHT(逆时针)。 请参见 Polygon orientation。


http://www.niftyadmin.cn/n/5360975.html

相关文章

c++常用数据结构

数组 在这里插入图片描述 静态数组 运行期间无法改变数组大小&#xff0c;由编译期间确定&#xff0c;C中array即此类 int main () {array<int, 10> a {0,1,2,3,4,5,6,7,8,9};a[0] 22;cout<<a.at(0)<<endl;cout<<a.back()<<endl;return 0; }…

裁员对于程序员的影响到底有多大?

程序员裁员潮&#xff1a;技术变革下的职业危机 裁员对于任何行业的员工来说都是一个沉重的打击&#xff0c;对于程序员尤其如此。在技术快速变革的时代&#xff0c;程序员不仅要不断学习新技术以保持竞争力&#xff0c;还要面对行业波动和公司重组带来的不确定性。以下是裁员…

C++:this隐藏参数

你是否有一个问题&#xff1a;C中成员函数中究竟是如何访问成员变量的&#xff1f; 其实了解后回答起来这个问题很简单&#xff0c;通过一个不受限的隐藏参数this&#xff0c;this是类的指针&#xff0c;通过它可以访问到类内的各种成员。 明白了这个问题就很好理解&#xff…

如何在 Golang 中使用 crypto/ed25519 进行数字签名和验证

如何在 Golang 中使用 crypto/ed25519 进行数字签名和验证 引言crypto/ed25519 算法简介环境搭建和准备工作生成密钥对进行数字签名 验证签名实际应用场景案例总结 引言 在当今数字化时代&#xff0c;网络安全显得尤为重要。无论是在网上进行交易、签署合同&#xff0c;还是发…

java基础 -10 Set之ConcurrentSkipListSet、EnumSet

ConcurrentSkipListSet ConcurrentSkipListSet是java中的集合类&#xff0c;是在多线程的环境中使用的&#xff0c;实现Set接口&#xff0c;它基于跳表(skip List)的数据结构。跳表是一种基于并行排序的数据结构&#xff0c;允许快速的出入&#xff0c;删除和查找操作&#xf…

低代码助力ERP开发:实现负担得起、灵活与高效的解决方案

企业资源规划工具或 ERP 不再为大型国际企业所保留。如今&#xff0c;从 SME 到大型企业&#xff0c;各种规模的企业都使用 ERP 软件来管理其核心流程。全球ERP 软件市场每年价值超过 250 亿美元&#xff0c;年增长率为 10% 到 20%。如此巨大增长的原因是什么&#xff1f; 传统…

Spring之xml配置方式快速整合MongoDB

Spring之xml配置方式快速整合MongoDB 文章目录 Spring之xml配置方式快速整合MongoDB1. MongoDB1. 引依赖2. application.properties3. application-context-mongodb.xml 1. MongoDB MongoDB 版本&#xff1a; 4.2.21 1. 引依赖 <!-- https://mvnrepository.com/artifact/or…

XML传参方式

export function groupLoginAPI(xmlData) {return http.post(/tis/group/1.0/login, xmlData, {headers: {Content-Type: application/xml,X-Requested-With: AAServer/4.0,}}) }import {groupLoginAPI} from "../api/user"; function (e) { //xml格式传参let groupX…