如何仅获取 Elastic Search 中列表中匹配的元素值?

How do I get only the element values that match in the list in the Elastic Search?

[你好]

我想创建一个 ES 查询,它只检索列表中匹配的某些元素。

这是我的 ES 索引架构。

"test-es-2018":{
"aliases": {}, 
"mappings": { 
  "test-1": { 
    "properties": { 
      "categoryName": { 
        "type": "keyword", 
        "index": false 
      }, 
      "genDate": { 
        "type": "date" 
      }, 
      "docList": { 
        "properties": { 
          "rank": { 
            "type": "integer", 
            "index": false 
          }, 
          "doc-info": {
            "properties": { 
              "docId": { 
                "type": "keyword" 
              }, 
              "docName": { 
                "type": "keyword", 
                "index": false 
              }, 
            } 
          }
        } 
      }, 
      "categoryId": { 
        "type": "keyword" 
      }, 
    } 
  }
} 

}

该类别中列出了文档。列表中的文件有自己的信息。

*在 Kibana 中搜索查询。

source": { 
        "categoryName" : "food" , 
        "genDate" : 1577981646638, 
        "docList" [
        {
          "rank": 2, 
          "doc-info": {...} 
        },
        {
          "rank": 1, 
          "doc-info": {...} 
        },
        {
          "rank": 5, 
          "doc-info": {...}
        },
       ],
       "categoryId": "201"
       }

首先,我只想获取列表中匹配的元素值。

我只想查看列表中排名为 1 的文档。但是,如果我使用 match 进行查询,结果与 *kibana.

中的搜索查询相同

*在 Kibana 中匹配查询。

GET test-es-2018/_search
{
  "query": {
    "bool": {
      "must": [
        { "match": { "docList.rank": 1 } },
      ] 
    }
  }
}

在我看来,它似乎打印了整个列表,因为它包含排名第一的文档。

我想要的是:

source": { 
        "categoryName" : "food" , 
        "genDate" : 1577981646638, 
        "docList" [
          {
            "rank": 1, 
            "doc-info": {...} 
          },
       ],
       "categoryId": "201"
       }

这可能吗?

其次,我想按排名对 docList 进行排序。我尝试通过创建如下查询进行排序,但未排序。

*Kibana 中的排序查询。

GET test-es-2018/_search? 
{
  "query" : {
    "bool" : {...}
  },
  "sort" : [
    {
      "docList.rank" : {
          "order" : "asc"
      }
    }
  ]
}

我想要的是:

source": { 
    "categoryName" : "food" , 
    "genDate" : 1577981646638, 
    "docList" [
    {
      "rank": 1, 
      "doc-info": {...} 
    },
    {
      "rank": 2, 
      "doc-info": {...} 
    },
    {
      "rank": 5, 
      "doc-info": {...}
    },
   ],
   "categoryId": "201"
   }

我不知道如何访问列表。对这两个问题有什么好主意吗?

  1. 通常,您可以使用 source filter 来仅检索文档的一部分,但这样就无法根据值排除某些字段。
  2. 据我所知,Elasticsearch 不支持更改 _source 中字段值的顺序。通过使用 nested 字段和 inner_hits -> sort 查询表达式,可以部分地实现所需的结果。这样,排序后的子点击将返回到响应的 inner_hits 部分。

P.S。通常使用 Elasticsearch,您应该将索引文档视为最小的不可分割的搜索单元。