elasticsearch - 我可以查询具有不同结构的两个不同索引并在Elasticsearch中合并结果吗?

标签 elasticsearch

我试图弄清楚使用Elasticsearch是否可以进行特定类型的查询,如果可以,则如何实际编写该查询。
我有两个索引,groupschallenges。这两个索引的数据形状不同,但是它们都有一个名为id的字段。
我想在不同的字段中搜索两个索引。例如,对于groups,我想搜索codenamedescription。对于challenges,它是相同的字段,但还有一些其他字段,其中一些位于嵌套数据中。
我能够运行一个简单的查询,该查询似乎使用URI查询从两个索引返回结果的组合,而没有指定索引。 EG:/search?q=some%20search。凉。
但是,我需要过滤结果,以便它们仅包括允许当前用户看到的组和挑战。在我的应用程序中,我可以轻松获得这些列表的permitted_group_idspermitted_challenge_ids。我曾计划切换到json主体查询并添加过滤器,但我无法弄清楚语法,尤其是关于过滤器的语法。
因此,有几个问题:

  • 进行类似于/search?q=some%20search的自由格式查询的json查询语法是什么?该文档似乎说我应该以某种方式使用bool,但是似乎我需要手动指定要搜索的字段(两个索引之间的字段不同),并且它们都必须与搜索字符串匹配。
  • 如何过滤基于不同索引中id排除的结果?

  • 用伪代码,这就是我要完成的工作:

    Dear Elasticsearch,

    Please find all records in the groups and challenges indexes that match the string "zamboni". Once you've identified those, please restrict the groups to only those whose id value is in [1, 2, 3] and the challenges to those whose id value is in [3, 4, 5]. Thanks!

    Sincerely, Your Friendly Neighborhood Developer.


    任何形式的帮助将不胜感激,甚至将我指向Elasticsearch文档的特定部分以供阅读。谢谢!

    最佳答案

    像这样:

    GET groups,challenges/_search
    {
      "size": 10,
      "_source": false, // or ["whatever", "data", "you", "need"]
      "query": {
        "bool": {
          "minimum_should_match": 1,
          "should": [
            {"bool": {"must": [
              {"term": {"_index": "groups"}},
              {"term": {"code": "zamboni"}},
              {"term": {"name": "zamboni"}},
              {"term": {"description": "zamboni"}},
              {"terms": {"id": [1, 2, 3]}}
            ]}},
            {"bool": {"must": [
              {"term": {"_index": "challenges"}},
              {"term": {"code": "zamboni"}},
              {"term": {"name": "zamboni"}},
              {"term": {"description": "zamboni"}},
              {"terms": {"id": [3, 4, 5]}}
            ]}}
          ]
        }
      }
    } 
    

    关于elasticsearch - 我可以查询具有不同结构的两个不同索引并在Elasticsearch中合并结果吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63505993/

    相关文章:

    elasticsearch - Elasticsearch 中是否应有多个组

    elasticsearch - Elasticsearch 中的 BM25 相似度

    go - http.Post 数据二进制,golang 中的 curl 等价物

    elasticsearch - ElasticSearch bool 查询到Solrj查询

    elasticsearch - 无法将非对象映射与机器学习(测试版)模块中的对象映射错误合并

    elasticsearch - Elasticsearch,如何使词组提示返回准确建议?

    php - 具有不同限制的Elasticsearch索引搜索

    python - 在ElasticSearch中执行 "Transaction"

    elasticsearch - 注册自定义分析器并在模板中使用它

    elasticsearch-dsl:否定过滤器