【问题标题】:Is it possible to "join" two indexes in elasticsearch using query是否可以使用查询“加入”弹性搜索中的两个索引
【发布时间】:2020-02-02 16:08:00
【问题描述】:

我知道在elasticsearch中没有加入索引的选项,但我需要找到解决这个问题的方法:

我有 2 个索引,例如 A、B

IndexAfield1, field2, field3
IndexBfield4, field5, field6 等信息

如果我将通过查询字段5(在本例中为“测试”)进行搜索,我希望在树中拥有所有关系,例如:

匹配 IndexA 中与“field2”匹配的所有文档 - 来自 IndexA 和来自 IndexB 的“field5”

例如
索引A文件:
5、“测试”、“测试2”、
10、“测试”、“测试7”
11、“test10”、“test11”

IndexB 文档:
1、“测试”、(...)
2、“测试”、(...)
3, "test100", (...)

示例响应:
对于 id5(来自 indexA),我想要一个来自 indexB 的 id 为 1 和 2 的对象,例如 {id:5,responses: {1, 2}}
对于 id10(来自 IndexA),我想要一个来自 indexB 的 id 为 1 和 2 的对象,例如 {id:10,响应:{1, 2}}
对于 id11 没有匹配项 ("test10" != "test")
{id:11,responses:{}}

Meybe 有什么办法可以解决这个问题吗?最后我需要为四个索引执行此操作(但如果可以在两个索引之间执行,那么我也可以在 4 上执行此操作)。

【问题讨论】:

    标签: elasticsearch aggregation


    【解决方案1】:

    就像你说的那样,我认为在弹性搜索中是不可能的。您不应该创建具有这种关系的索引。最好重新考虑您的模型并对数据进行非规范化。

    为了解决这个问题,您必须在后端以编程方式进行处理。伪代码:

    //Get all objects from indexA
    const allIndexA = indexA.getAll();
    const result = new Array();
    //For each object in indexA, select the corresponding object in indexB
    allIndexA.forEach((entryA) => {
        const entriesB = indexB.get({field5: entryA.field2});
        result.push({
            entryA,
            entriesB
        });
    });
    

    【讨论】:

      【解决方案2】:
                      I was tring as bellow:
      
                      GET /_msearch
                      {
                        "_index": [
                          "index1",
                          "index2",
                          "index3"
                        ]
                      }
                      {
                        "query": {
                          "bool": {
                            "should": [
                              {
                                "match": {
                                  "index3id": "1"  // it is in the 3th index so i have responses from 3th index
                                }                  // only
                              }                                           
                            ]
                          }
                        },
                        "size": 100,
                        "aggregations": {
                          "firstLevel": {
                            "top_hits": {
                              "size": 100,
                              "_source": {
                                "includes": "index3id"
                              }
                            }
                          }
                        }
                      }
      
                      response of aggregation here:
      
                       "aggregations": {
                         "firstLevel": {
                                "hits": {
                                  "total": 2,
                                  "max_score": 1,
                                  "hits": [
                                    {
                                      "_index": "index3",
                                      "_type": "someTypeNotRelevant",
                                      "_id": "81",
                                      "_score": 1,
                                      "_source": {
                                        "index3id": 1
                                      }
                                    },
                                    {
                                      "_index": "index3",
                                      "_type": "someTypeNotRelevant",
                                      "_id": "61",
                                      "_score": 1,
                                      "_source": {
                                        "index3id": 1
                                      }
                                    }
                                  ]
                                }
                              }
                            }
      
          Now I just want to do a new query in index2 for some field but with values which were in 
          _source(in this case - for the all index3id's) (i was thinking about some sub-aggregation to firstLevel": {} aggregation - but with use of new query to index2). 
      There are 2 problems:
      1. How to pass these index3id's?
      2. After first query, I have only "data" from index3 because of using index3id
      Anyway thank you for advice.
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-06-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-17
        相关资源
        最近更新 更多