Elasticsearch:嵌套对象列表中的Geo_spacial搜索

时间:2019-04-21 17:06:40

标签: java elasticsearch geospatial spring-data-elasticsearch elasticsearch-6

我正在使用Elasticsearch 6.3和spring-data-elasticsearch,索引为 restaurant ,其映射如下:

{
  "mapping": {
    "restaurant": {
      "properties": {
        "id": {
          "type": "keyword"
        },
        "name": {
          "type": "text",
          "store": true,
          "analyzer": "custom_analyzer",
          "search_analyzer": "custom_search_analyzer"
        },
        "locations": {
          "type": "nested",
          "include_in_parent": true,
          "properties": {
            "id": {
              "type": "long"
            },
            "fullAddress": {
              "type": "text"
            },
            "geo": {
              "type": "geo_point"
            },
            "phone": {
              "type": "keyword"
            }
          }
        },
        "menus": {
          "type": "nested",
          "properties": {
            "id": {
              "type": "long"
            },
            "name": {
              "type": "text",
              "store": true,
              "analyzer": "custom_analyzer",
              "search_analyzer": "custom_search_analyzer"
            }
          }
        }
      }
    }
  }
}

这是此映射的文档示例:

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "restaurants",
        "_type": "restaurants",
        "_id": "1",
        "_score": 1,
        "_source": {
          "id": 1,
          "name": "Mc Donald",
          "menus": [
            {
              "id": 7097,
              "name": "Big Fish"
            },
            {
              "id": 13899,
              "name": "Big Mac"
            }
          ],
          "locations": [
            {
              "id": 1,
              "fullAddress": "7 Bd Solférino, 59000 Lille",
              "geo": { "lat": 50.6423461,"lon": 3.1390441},
              "phone": "0305060804"
            },
            {
              "id": 2,
              "fullAddress": "7 Rue Nationale, 75000 Paris",
              "geo": { "lat": 40.6423461,"lon": -7.1390441},
              "phone": "0305060804"
            }
          ]
        }
      }
    ]
  }
}

我尝试搜索周围的所有餐馆,例如,搜索最近的麦当劳。

我尝试了以下2个查询,但它们返回了空列表:

查询1 :(使用帖子过滤)

GET restaurants/_search
{
  "query": {
    "match": {
      "name": "donald"
    }
  },
  "post_filter": {
    "nested": {
      "path": "locations",
      "query": {
        "geo_distance": {
          "distance": "10km",
          "locations.geo": [50.6423461, 3.1390441]
        }
      }
    }
  }
}

查询2 :(在查询中使用过滤)

GET restaurants/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "donald"
          }
        }
      ],
      "filter": {
        "nested": {
          "path": "locations",
          "query": {
            "geo_distance": {
              "distance": "10km",
              "locations.geo": [50.6423461,3.1390441]
            }
          },
          "inner_hits": {}
        }
      }
    }
  }
}

请告诉我我在映射或查询中做错了什么。 提前谢谢。

0 个答案:

没有答案