ElasticSearch中Shape类型操作

说明

        1、用户必须明确的将字段type定义为shape类型

        2、官方文档地址:https://www.elastic.co/guide/en/elasticsearch/reference/7.4/shape-queries.html

        3、本文档中ES存入数据,分别采用GeoJSON和WKT方式,查询都是通过GeoJSON方式进行查询

        4、7.4(包含)以后的版本才开始支持shape操作

一、GeoJSON方式

        1、创建索引

PUT /example_geojson

{
  "mappings": {
    "properties": {
      "geometry": {
        "type": "shape"
      }
    }
  }

        2、插入数据

POST /example_geojson/_doc

{
  "name": "Lucky Landing",
  "geometry": {
    "type": "point",
    "coordinates": [ x, y ]
}

        3、查询数据

POST /example_geojson/_search

查询方式1(矩形):
{
  "query": {
    "shape": {
      "geometry": {
        "shape": {
          "type": "envelope",
          "coordinates": [ 
            [ 116.01, 39.01 ], 
            [ 116.02, 39.02 ]
          ]
        },
        "relation": "within"
      }
    }
  }
}

查询方式2(多边形):
{
  "query": {
    "shape": {
      "geometry": {
        "shape": {
          "type": "polygon",
          "coordinates": [[
              [116.01,39.01],
              [116.02,39.02],
              [116.03,39.03],
              [116.04,39.04],
              [116.05,39.05],
              [116.06,39.06],
              [116.01,39.01]
            ]]
        },
        "relation": "within"
      }
    }
  }
}

查询方式3(多面):
{
  "query": {
    "shape": {
      "geometry": {
        "shape": {
          "type": "multipolygon",
          "coordinates": [
            [[
              [116.01,39.01],
              [116.02,39.02],
              [116.03,39.03],
              [116.04,39.04],
              [116.05,39.05],
              [116.06,39.06],
              [116.01,39.01]
            ]],
            [[
              [116.01,39.01],
              [116.02,39.02],
              [116.03,39.03],
              [116.04,39.04],
              [116.01,39.01]
            ]]
          ]
        },
        "relation": "within"
      }
    }
  }
}

注:以上数据都是虚拟数据,仅提供格式参照

                4、返回数据

{
	"took": 2,
	"timed_out": false,
	"_shards": {
		"total": 1,
		"successful": 1,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 3,
			"relation": "eq"
		},
		"max_score": 1,
		"hits": [{
				"_index": "test02",
				"_type": "_doc",
				"_id": "km1t4XoBGyDewCtudsly",
				"_score": 1,
				"_source": {
					"name": "测试01",
					"geometry": {
						"type": "point",
						"coordinates": [
							116.01,
							39.01
						]
					}
				}
			},
			{
				"_index": "test02",
				"_type": "_doc",
				"_id": "k21t4XoBGyDewCtuwsmh",
				"_score": 1,
				"_source": {
					"name": "测试02",
					"geometry": {
						"type": "point",
						"coordinates": [
							116.02,
							39.02
						]
					}
				}
			},
			{
				"_index": "test02",
				"_type": "_doc",
				"_id": "lG1t4XoBGyDewCtu_slb",
				"_score": 1,
				"_source": {
					"name": "测试03",
					"geometry": {
						"type": "point",
						"coordinates": [
							116.03,
							39.03
						]
					}
				}
			}
		]
	}
}

                5、mapping信息

"mappings": {
	"_doc": {
		"properties": {
			"name": {
				"type": "text",
				"fields": {
					"keyword": {
						"ignore_above": 256,
						"type": "keyword"
					}
				}
			},
			"geometry": {
				"type": "shape"
			}
		}
	}
}

二、Well-Known Text(WKT)方式

                1、创建索引

PUT /example_wkt
{
  "mappings": {
    "properties": {
      "geometry": {
        "type": "shape"
      }
    }
  }
}

                2、插入数据

POST /example_wkt/_doc
{
  "geometry": "POINT (x y)",
  "name": "testName"
}

                3、查询数据

POST /example_wkt/_search

查询方式1(矩形):
{
  "query": {
    "shape": {
      "geometry": {
        "shape": {
          "type": "envelope",
          "coordinates": [ 
            [ 116.01, 39.01 ], 
            [ 116.02, 39.02 ]
          ]
        },
        "relation": "within"
      }
    }
  }
}

查询方式2(多边形):
{
  "query": {
    "shape": {
      "geometry": {
        "shape": {
          "type": "polygon",
          "coordinates": [[
              [116.01,39.01],
              [116.02,39.02],
              [116.03,39.03],
              [116.04,39.04],
              [116.05,39.05],
              [116.06,39.06],
              [116.01,39.07]
            ]]
        },
        "relation": "within"
      }
    }
  }
}

查询方式3(多面):
{
  "query": {
    "shape": {
      "geometry": {
        "shape": {
          "type": "multipolygon",
          "coordinates": [
            [[
              [116.01,39.01],
              [116.02,39.02],
              [116.03,39.03],
              [116.04,39.04],
              [116.05,39.05],
              [116.06,39.06],
              [116.01,39.01]
            ]],
            [[
              [116.01,39.01],
              [116.02,39.02],
              [116.03,39.03],
              [116.04,39.04],
              [116.01,39.05]
            ]]
          ]
        },
        "relation": "within"
      }
    }
  }
}

注:以上数据都是虚拟数据,仅提供格式参照

                4、返回数据

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 3,
      "relation": "eq"
    },
    "max_score": 1,
    "hits": [
      {
        "_index": "test03",
        "_type": "_doc",
        "_id": "6G3d5XoBGyDewCtuAMn4",
        "_score": 1,
        "_source": {
          "name": "测试01",
          "geometry": "POINT (116.01 39.01)"
        }
      },
      {
        "_index": "test03",
        "_type": "_doc",
        "_id": "6W3d5XoBGyDewCtuisnl",
        "_score": 1,
        "_source": {
          "name": "测试02",
          "geometry": "POINT (116.02 39.02)"
        }
      },
      {
        "_index": "test03",
        "_type": "_doc",
        "_id": "6m3d5XoBGyDewCtu7snt",
        "_score": 1,
        "_source": {
          "name": "测试03",
          "geometry": "POINT (116.03 39.03)"
        }
      }
    ]
  }
}

                5、mapping信息

"mappings": {
  "_doc": {
    "properties": {
      "name": {
        "type": "text",
        "fields": {
          "keyword": {
            "ignore_above": 256,
            "type": "keyword"
          }
        }
      },
      "geometry": {
        "type": "shape"
      }
    }
  }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值