Elasticsearch: Failed to query bool nested query with a filter and sort

陌路散爱 提交于 2021-01-28 13:31:12

问题


I am new to Elasticsearch and I have to make a query that queries that two indexes. I want to return all the People with eye_color hazel from a nested data object within a polygon and I want the results to be sorted by name.

I have looked at many questions on Stackoverflow, but can't get the query right. My data object is nested in the mapping. Can someone tell me what I am doing wrong? I am getting a failed to create query exception. The geojson is in one index and the data.eye_color is in another index and I don't know if that is causing issues. If I remove the filter and the sort then query returns fine.

{
  "query": {
    "bool": {
      "should": [
        {
          "terms": {
            "primaryTopic.keyword": [
              "People"
            ]
          }
        },
        {
          "bool": {
            "must": [
              {
                "nested": {
                  "path": "data",
                  "query": {
                    "bool": {
                      "must": [
                        {
                          "term": {
                            "data.eye_color.keyword": "hazel"
                          }
                        }
                      ]
                    }
                  }
                }
              }
            ]
          }
        }
      ],
      "filter":{
          "geo_shape": {
            "geojson" : {
                "shape": {
                "type": "polygon",
                "orientation": "right",
                "coordinates": [[[-10,-10],[10,-10],[10,10],[-10,10],[-10,-10]]],
                "relation": "within"
              }
            }
          }
        }
    },
      "sort":{
        "date":{
           "data.name":"desc"
        }
     }
  }
}

回答1:


The only way I know of to query against geojson in another index is using pre-indexed shapes.

I'm going to assume your main index includes a geo point field (could be of type geo_point or geo_shape as shown below) and that your geojson index consists of polygons, one of which you query against in your question.

Set up an MRE index:

PUT my_main_index
{
  "mappings": {
    "properties": {
      "data": {
        "type": "nested"
      },
      "geojson": {
        "type": "geo_shape"
      }
    }
  }
}

Sync a doc

POST my_main_index/_doc
{
  "primaryTopic": "People",
  "data": [
    {
      "eye_color": "hazel",
      "name": "abc"
    }
  ],
  "geojson": {
    "type": "Point",
    "coordinates": [
      -5.625,
      5.965753671065536
    ]
  }
}

Create the index containing polygons to query against

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

Sync the one from your question

PUT /shapes/_doc/africa_poly
{
  "location": {
    "type": "polygon",
    "coordinates": [
      [
        [
          -10,
          -10
        ],
        [
          10,
          -10
        ],
        [
          10,
          10
        ],
        [
          -10,
          10
        ],
        [
          -10,
          -10
        ]
      ]
    ]
  }
}

Search using africa_poly:

GET my_main_index/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "terms": {
            "primaryTopic.keyword": [
              "People"
            ]
          }
        },
        {
          "bool": {
            "must": [
              {
                "nested": {
                  "path": "data",
                  "query": {
                    "bool": {
                      "must": [
                        {
                          "term": {
                            "data.eye_color.keyword": "hazel"
                          }
                        }
                      ]
                    }
                  }
                }
              }
            ]
          }
        }
      ],
      "filter": {
        "geo_shape": {
          "geojson": {
            "indexed_shape": {
              "index": "shapes",
              "id": "africa_poly",
              "path": "location"
            },
            "relation": "within"
          }
        }
      }
    }
  }
}

Which should yield our my_main_index doc.


Your sort is off too... This is how you sort on nested fields:

...
{
  "query: {...},
  "sort": [
    {
      "data.name.keyword": {
        "order": "desc",
        "nested_path": "data"
      }
    }
  ]
}


来源:https://stackoverflow.com/questions/62703303/elasticsearch-failed-to-query-bool-nested-query-with-a-filter-and-sort

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!