使用一个ElasticSearch查询链接两个索引

r9f1avp5  于 2023-03-17  发布在  ElasticSearch
关注(0)|答案(1)|浏览(178)

我在elastic中有两个索引,index_a和index_B,索引a中的示例记录如下所示

"id": "8",
"name": "pepsi",
"id_b": "13",

索引B中的样本记录看起来像

"id": "13",
"mapping": "15"

搜索时,我将从索引a获得名称,例如"pepsi"。我必须从索引b返回Map字段,即"15"。我知道这可以通过两个搜索查询轻松完成,其中i返回与来自index_a的输入文本匹配的所有"id_b",然后第二个查询将与id_B,并返回Map"15"。我的问题是这能在一个查询中完成吗?

ukxgm1gy

ukxgm1gy1#

我做了这个代码和响应Map值.参考:article .

POST index_a/_doc?refresh
{
  "id": "8",
  "name": "pepsi",
  "id_b": "13"
}

PUT index_b/_doc/1?refresh
{
  "id": "13",
  "mapping": 15
}

POST index_a/_search
{
  "query": {
    "match": {
      "name": "pepsi"
    }
  }, 
  "runtime_mappings": {
    "mapping": {
        "type": "lookup", 
        "target_index": "index_b", 
        "input_field": "id_b", 
        "target_field": "id", 
        "fetch_fields": ["mapping"] 
    }
  },
  "fields": [
    "mapping"
  ],
  "_source": false
}

回复:

"hits": [
  {
    "_index": "index_a",
    "_id": "n3YLnYYB_18wwUoiAnA5",
    "_score": 0.2876821,
    "fields": {
      "mapping": [
        {
          "mapping": [
            15
          ]
        }
      ]
    }
  }
]

相关问题