【问题标题】:ElasticSearch - How can i apply a filter over the results of the query to limit the document that have a certain valueElasticSearch - 我如何对查询结果应用过滤器以限制具有特定值的文档
【发布时间】:2020-12-26 11:54:48
【问题描述】:

我有一个关于弹性搜索的问题,但我不确定从哪里开始搜索或我应该使用 google 搜索哪个精确操作。

假设我有一个包含数据的文档,其中一个字段是“the_best”(这是一个布尔值)。事情是(目前),超过 48 个结果(由工作查询给出),我有 15 个文档返回,the_best 字段设置为 true。

现在,我想将结果限制为最多 2 个设置为 true 的文档。所以现在,它(elasticsearch)现在应该返回 35 个结果(如果我们坚持上面的故事):

  • 基数(共 48 个结果):[15 the_best=true, 33 the_best=false]
  • 预期(最多 2 个 the_best=true):我应该得到 35 个结果 [2 the_best=true, 33 the_best=false])

有什么想法吗? :)

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    一种方法是使用m_search

    使用 m_Search 您可以组合多个查询

    GET <index>/_msearch
    {}
    {"query":{"term":{"the_best":true}},"from":0,"size":2}
    {}
    {"query":{"term":{"the_best":false}},"from":0,"size":15}
    

    如果你想在单个搜索聚合中进行,可以使用(性能会降低)

    我用过filter aggregationtop_hits aggregation

    {
      "size": 0, 
      "aggs": {
        "true": {
          "filter": {
            "term": {
              "the_best": true
            }
          },
          "aggs": {
            "docs": {
              "top_hits": {
                "size": 2
              }
            }
          }
        },
        "false": {
          "filter": {
            "term": {
              "the_best": false
            }
          },
          "aggs": {
            "docs": {
              "top_hits": {
                "size": 10
              }
            }
          }
        }
      }
    }
    
    

    【讨论】:

    • 嘿!我会尽快检查,谢谢您的回复:)
    猜你喜欢
    • 2019-03-26
    • 2014-04-18
    • 1970-01-01
    • 2016-02-03
    • 1970-01-01
    • 2013-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多