名称/值对列的弹性搜索查询拉取

Elastic search query for name / value pair columns pull

我们在弹性搜索中有一个文档包含 name/value 对的多个部分,我们只想根据名称列值获取值。

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

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

如果字段 envelopeData.details.detailnested type then you can perform a match query for the desired name on the nested path and can use inner_hits 则只获取值。

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

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

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

GET Whosebug/_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": "Whosebug",
  "_type": "_doc",
  "_id": "W5GUW2gB3GnGVyg-Sf4T",
  "_score": 0.6931472,
  "_source": {},
  "inner_hits": {
    "envelopeData.details.detail": {
      "hits": {
        "total": 1,
        "max_score": 0.6931472,
        "hits": [
          {
            "_index": "Whosebug",
            "_type": "_doc",
            "_id": "W5GUW2gB3GnGVyg-Sf4T",
            "_nested": {
              "field": "envelopeData.details.detail",
              "offset": 0
            },
            "_score": 0.6931472,
            "_source": {
              "value": "76483"  -> Outputs value only
            }
          }
        ]
      }
    }
  }
}