是否可以使用查询

时间:2019-10-04 10:13:57

标签: elasticsearch aggregation

我知道在Elasticsearch中没有连接索引的选项,但是我需要找到一种解决此问题的方法:

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

IndexA 具有类似 field1,field2,field3
的信息 索引B 具有 field4,field5,field6

如果我要通过查询field5(在本例中为“ test”)进行搜索,我希望树中具有所有关系,如:

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

例如
IndexA文件:
5,“ test”,“ test2”,
10,“测试”,“ test7”
11,“ test10”,“ test11”

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

示例响应:
对于id5(来自indexA),我想拥有一个对象,其ID来自indexB的ID为1和2,例如{id:5,响应:{1,2}}
对于id10(来自IndexA),我希望有一个对象,其索引B中的id为1和2,例如{id:10,响应:{1,2}}
对于id11,没有匹配项(“ test10”!=“ test”)
{id:11,响应:{}}

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

2 个答案:

答案 0 :(得分:1)

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

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

//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
    });
});

答案 1 :(得分:0)

                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.