es - elasticsearch mapping - field data type - 26

80 阅读1分钟

世界上并没有完美的程序,但是我们并不因此而沮丧,因为写程序就是一个不断追求完美的过程。

问 :geo_shape查询有什么特点?
答 :
在这里插入图片描述
问 :geo_shape查询如何使用?
答 :

# 搜索 - envelope [[minLon, maxLat], [maxLon, minLat]]
GET /geo_shape_test/_search
{
  "query" : {
    "bool" : {
      "must" : [
        {"match_all" : {}}
      ],
      "filter" : {
        "geo_shape" : {
          "my_geo_shape" : {
            "shape" : {
              "type"        : "envelope",
              "coordinates" : [
                [
                  -100,
                  45
                ],
                [
                  100,
                  -45
                ]
              ]
            },
            "relation" : "within"
          }
        }
      }
    }
  }
}

# 结果
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "geo_shape_test",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "my_geo_shape" : {
            "type" : "point",
            "coordinates" : [
              -77,
              38
            ]
          }
        }
      }
    ]
  }
}

# 外部形状索引
PUT /shapes
{
  "mappings" : {
    "properties" : {
      "location" : {
        "type" : "geo_shape"
      }
    }
  }
}

# 索引
POST /shapes/_doc/deu
{
  "location" : {
    "type"        : "envelope",
    "coordinates" : [[-100, 53.0], [14.0, 0]]
  }
}

# 索引,利用外部索引搜索
GET /geo_shape_test/_search
{
  "query" : {
    "bool" : {
      "filter" : [
        {
          "geo_shape" : {
            "my_geo_shape" : {
              "indexed_shape" : {
               "index" : "shapes",
               "id"    : "deu",
               "path"  : "location"
              },
              "relation" : "within"
            }
          }
        }
      ]
    }
  }
}

# 结果
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.0,
    "hits" : [
      {
        "_index" : "geo_shape_test",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.0,
        "_source" : {
          "my_geo_shape" : {
            "type" : "point",
            "coordinates" : [
              -77,
              38
            ]
          }
        }
      }
    ]
  }
}