0

我们在弹性搜索中有一个文档,其中包含多个名称/值对部分,我们希望仅根据名称列值获取值。

"envelopeData": {
  "envelopeName": "Bills",
  "details": {
    "detail": [
      {
        "name": "UC_CORP",
        "value": "76483"
      },
      {
        "name": "UC_CYCLE",
        "value": "V"
      }    

我们预计只有 76483 作为基于名称等于UC_CORP的结果

4

1 回答 1

2

如果字段信封Data.details.detail是嵌套类型,那么您可以对嵌套路径上的所需名称执行匹配查询,并且可以使用inner_hits仅获取该值。

将字段信封Data.details.detail 映射为嵌套(如果未嵌套):

PUT stackoverflow
{
  "mappings": {
    "_doc": {
      "properties": {
        "envelopeData.details.detail": {
          "type": "nested" 
        }
      }
    }
  }
}

然后您可以使用inner_hits执行以下查询以获取值:

GET stackoverflow/_search
{
  "_source": "false", 
  "query": {
    "nested": {
      "path": "envelopeData.details.detail",
      "query": {
        "match": {
          "envelopeData.details.detail.name.keyword": "UC_CORP"
        }
      }, 
      "inner_hits": {
        "_source": "envelopeData.details.detail.value"
      }
    }
  }
}

输出:

{
  "_index": "stackoverflow",
  "_type": "_doc",
  "_id": "W5GUW2gB3GnGVyg-Sf4T",
  "_score": 0.6931472,
  "_source": {},
  "inner_hits": {
    "envelopeData.details.detail": {
      "hits": {
        "total": 1,
        "max_score": 0.6931472,
        "hits": [
          {
            "_index": "stackoverflow",
            "_type": "_doc",
            "_id": "W5GUW2gB3GnGVyg-Sf4T",
            "_nested": {
              "field": "envelopeData.details.detail",
              "offset": 0
            },
            "_score": 0.6931472,
            "_source": {
              "value": "76483"  -> Outputs value only
            }
          }
        ]
      }
    }
  }
}
于 2019-01-17T11:46:58.803 回答