对嵌套文档进行排序和分页

时间:2016-07-17 15:39:10

标签: elasticsearch

Elasticsearch具有开箱即用的分类和分页支持。但是,如果我只想检索嵌套对象,对它们进行分页并按其字段排序,该怎么办? 例如:

{
  "mappings" : {
    "library" : {
      "properties" : {
        "name" : {"type": "string"},
        "books" : {
          "type": "nested",
          "properties" : {
            "title" : {"type": "string"},
            "author" : {"type": "string"},
            "year" : {"type": "integer"}
          }
        }
      }
    }
  }
}

我怎样才能问Elasticsearch:“先{10} books offset = 20 title= 'Elasticsearch'排序year”?是否可以使用嵌套类型,或者我应该使用较慢的父子关系?

2 个答案:

答案 0 :(得分:0)

是的,可以,您可以使用nested inner hits来实现您的目标:

POST index/library/_search
{
    "query" : {
        "nested" : {
            "path" : "books",
            "query" : {
                "match" : {"books.title" : "Elasticsearch"}
            },
            "inner_hits" : {
                "from": 20,
                "size": 10,
                "sort": {"books.year": "asc"}
            } 
        }
    }
}

答案 1 :(得分:0)

我发现无法对嵌套文档进行排序和分页。本主题证明了https://discuss.elastic.co/t/nested-objects-hits-inner-hits-and-sorting/32565