在ElasticSearch中建议嵌套数据

hl0ma9xz  于 2023-04-05  发布在  ElasticSearch
关注(0)|答案(1)|浏览(117)

我在ElasticSearch中有一些数据:

{
    "title": "example",
    "labels": [
        {
            "key": "region",
            "value": "us-la"
        },
        {
            "key": "url",
            "value": "www.google.com"
        }
    ]
},
{
    "title": "example",
    "labels": [
        {
            "key": "region",
            "value": "us-nyc"
        },
        {
            "key": "url",
            "value": "www.stackoverflow.com"
        }
    ]
}

ElasticSearchMap:

{
    "properties": {
        "title": {
            "type": "text"
        },
        "labels": {
            "type": "nested",
            "properties": {
                "key": {
                    "type": "text"
                }
                "value": {
                    "type": "text"
                }
            }
        }
    }
}

当我想建议labels.key

GET /my-index/_search
{
    "query": {
        "nested": {
            "path": "labels",
            "query": {
                "match": {
                    "labels.key": "us"
                }
            }
        }
    }
}

回应是:

{
    "title": "example",
    "labels": [
        {
            "key": "region",
            "value": "us-la"
        },
        {
            "key": "url",
            "value": "www.google.com"
        }
    ]
}

它在文档中返回,但我只想要labels字段的匹配对象,如下所示:

{
    "key": "region",
    "value": "us-la"
}

这将便于限制响应。
我试过inner_hit,但仍然不工作。
任何帮助/建议将不胜感激。

x7rlezfr

x7rlezfr1#

我认为你在字段“label.value”中搜索,因为唯一的字段包含值“us-la”。
我使用了“inner_hits”并获得了匹配。

GET /idx_test/_search?filter_path=hits.hits.inner_hits
{
  "query": {
    "nested": {
      "path": "labels",
      "query": {
        "match": {
          "labels.value": "us"
        }
      },
      "inner_hits": {}
    }
  }
}

结果:

{
  "hits": {
    "hits": [
      {
        "inner_hits": {
          "labels": {
            "hits": {
              "total": {
                "value": 1,
                "relation": "eq"
              },
              "max_score": 0.60996956,
              "hits": [
                {
                  "_index": "idx_test",
                  "_id": "VMefP4cB8815uFWkUuiJ",
                  "_nested": {
                    "field": "labels",
                    "offset": 0
                  },
                  "_score": 0.60996956,
                  "_source": {
                    "value": "us-la",
                    "key": "region"
                  }
                }
              ]
            }
          }
        }
      },
      {
        "inner_hits": {
          "labels": {
            "hits": {
              "total": {
                "value": 1,
                "relation": "eq"
              },
              "max_score": 0.60996956,
              "hits": [
                {
                  "_index": "idx_test",
                  "_id": "VcefP4cB8815uFWkVui1",
                  "_nested": {
                    "field": "labels",
                    "offset": 0
                  },
                  "_score": 0.60996956,
                  "_source": {
                    "value": "us-nyc",
                    "key": "region"
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}

相关问题