elasticsearch - Elasticsearch geo_shape过滤器无结果

标签 elasticsearch nested elasticsearch-geo-shape

在嵌套位置上进行过滤时,让我的geo_shape过滤器返回结果时遇到问题。

假设我有以下内容:

PUT test/test/_mapping
{
  "properties": {
    "name": {
      "type": "string"
    },
    "gatheringEvent": {
      "properties": {
        "siteCoordinates": {
          "type": "nested",
          "properties": {
            "point": {
              "type": "geo_shape"
            }
          }
        }
      }
    },
    "point": {
      "type": "geo_shape"
    }
  }
}

现在,当我索引以下文档时:
POST test/test/1
{
  "name": "Bird",
  "gatheringEvent.siteCoordinates.point": {
    "type": "point",
    "coordinates": [
      5,
      5
    ]
  },
  "point": {
    "type": "point",
    "coordinates": [
      5,
      5
    ]
  }
}

执行以下查询:(在非嵌套位置使用geo_shape过滤器)
GET test/test/_search
{
  "query": {
    "filtered": {
      "query": {
        "match": {
          "name": "Bird"
        }
      },
      "filter": {
        "geo_shape": {
          "point": {
            "shape": {
              "type": "polygon",
              "coordinates": [
                [
                  [0 ,0 ],
                  [10 ,0],
                  [10,10],
                  [0,10 ],
                  [0 ,0 ]
                ]
              ]
            },
            "relation": "within"
          }
        }
      }
    }
  }
}

如我所愿,把文件还给我。

但是在嵌套位置执行geo_shape过滤器时:
GET test/test/_search
{
  "query": {
    "filtered": {
      "query": {
        "match": {
          "name": "Bird"
        }
      },
      "filter": {
        "nested": {
          "path": "gatheringEvent.siteCoordinates",
          "filter": {
            "geo_shape": {
              "gatheringEvent.siteCoordinates.point": {
                "shape": {
                  "type": "polygon",
                  "coordinates": [
                    [
                      [0 ,0 ],
                      [10 ,0],
                      [10,10],
                      [0,10 ],
                      [0 ,0 ]
                    ]
                  ]
                },
                "relation": "within"
              }
            }
          }
        }
      }
    }
  }
}

没有结果。

我也删除了嵌套映射,因为我认为这可能是问题所在,但是一旦“point”字段位于对象类型字段中,我就没有结果。

对我在这里做错的任何想法吗?

谢谢。

最佳答案

我在这里看到几个问题:

  • 看起来您想要两个级别的嵌套(除非这是一个错误),因此,如果您希望能够在查询中使用nested过滤器,则需要在映射中指定两个级别。
  • 使用嵌套结构索引文档时,不能使用点语法。该语法仅用于查询。如果您在对文档建立索引之前和之后查看映射,则会发现在为文档建立索引时会添加一个名为"gatheringEvent.siteCoordinates.point"的顶级属性,这大概不是您想要的。

  • 您可以通过几种不同的方式进行操作。这就是我能够使其工作的方式。首先,我修改了映射以包括两个嵌套层,并创建了如下索引:
    DELETE /test_index
    
    PUT /test_index
    {
       "settings": {
          "number_of_shards": 1,
          "number_of_replicas": 0
       }
    }
    
    PUT /test_index/doc/_mapping
    {
       "properties": {
          "name": {
             "type": "string"
          },
          "gatheringEvent": {
             "type": "nested",
             "properties": {
                "siteCoordinates": {
                   "type": "nested",
                   "properties": {
                      "point": {
                         "type": "geo_shape"
                      }
                   }
                }
             }
          },
          "point": {
             "type": "geo_shape"
          }
       }
    }
    

    然后,我为两个层次的嵌套使用正确的结构索引了您的文档:
    POST /test_index/doc/1
    {
       "name": "Bird",
       "gatheringEvent": [
          {
             "siteCoordinates": [
                {
                   "point": {
                      "type": "point",
                      "coordinates": [5, 5]
                   }
                }
             ]
          }
       ],
       "point": {
          "type": "point",
          "coordinates": [5, 5]
       }
    }
    

    我还在边界框外添加了第二个文档,以进行完整性检查:
    POST /test_index/doc/2
    {
       "name": "Bird",
       "gatheringEvent": [
          {
             "siteCoordinates": [
                {
                   "point": {
                      "type": "point",
                      "coordinates": [6, 11]
                   }
                }
             ]
          }
       ],
       "point": {
          "type": "point",
          "coordinates": [6, 11]
       }
    }
    

    现在,您的两个查询都能按预期工作:
    POST /test_index/doc/_search
    {
      "query": {
        "filtered": {
          "query": {
            "match": {
              "name": "Bird"
            }
          },
          "filter": {
            "nested": {
              "path": "gatheringEvent.siteCoordinates",
              "filter": {
                "geo_shape": {
                  "gatheringEvent.siteCoordinates.point": {
                    "shape": {
                      "type": "polygon",
                      "coordinates": [
                        [
                          [0, 0],
                          [10, 0],
                          [10, 10],
                          [0, 10],
                          [0, 0]
                        ]
                      ]
                    },
                    "relation": "within"
                  }
                }
              }
            }
          }
        }
      }
    }
    {
       "took": 2,
       "timed_out": false,
       "_shards": {
          "total": 1,
          "successful": 1,
          "failed": 0
       },
       "hits": {
          "total": 1,
          "max_score": 1.6931472,
          "hits": [
             {
                "_index": "test_index",
                "_type": "doc",
                "_id": "1",
                "_score": 1.6931472,
                "_source": {
                   "name": "Bird",
                   "gatheringEvent": [
                      {
                         "siteCoordinates": [
                            {
                               "point": {
                                  "type": "point",
                                  "coordinates": [
                                     5,
                                     5
                                  ]
                               }
                            }
                         ]
                      }
                   ],
                   "point": {
                      "type": "point",
                      "coordinates": [
                         5,
                         5
                      ]
                   }
                }
             }
          ]
       }
    } 
    

    如果您实际上只想要一个嵌套级别,那就更简单了。我也可以根据需要添加该代码。

    这是我使用的代码:

    http://sense.qbox.io/gist/e61259626d5f8525ee41ce7b049af25089bfb8f6

    关于elasticsearch - Elasticsearch geo_shape过滤器无结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28498494/

    相关文章:

    mysql - Elasticsearch : "reason": "failed to find geo_point field"

    javascript - 比较两个字符串时如何避免嵌套循环?

    html - 嵌套 Div 使按钮围绕电话 : Link?

    actionscript-3 - 评估 AS3 中包含嵌套影片剪辑的路径字符串

    join - 在 ElasticSearch 中模拟连接

    elasticsearch - Elasticsearch将动态映射的位置数据重新索引为几何形状

    groovy - Elasticsearch-geo_point数组上运行distanceInKm的MissingMethodException

    elasticsearch - Spring Data api(包含或开始)不适用于具有通配符的SPACE

    elasticsearch - 如何从Kafka Connect在Elastic Search中创建多个索引

    elasticsearch - 如何获取按字段分组的最新文档?