elasticsearch - 查询以从Elastic Search的前100个项目中随机获取n个项目

标签 elasticsearch kibana

我需要在elasticsearch中编写查询,以在排名前100位的项目中随机获取12个项目。

我尝试过类似的操作,但是我无法随机获得12个项目(我只能获得前12个项目)。

我使用的查询:

GET product/_search
{
  "sort": [
    {
      "DateAdded": {
        "order": "desc"
      }
    }
  ],
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "must": [
            {
              "term": {
                "definitionName": {
                  "value": "ABC"
                }
              }
            },
            {
              "range": {
                "price": {
                  "gt": 0
                }
              }
            }
          ]
        }
      },
      "functions": [
        {
          "random_score": {
            "seed": 314159265359
          }
        }
      ]
    }
  },
  "size": 12
}

谁能指导我哪里出问题了? (我是写ElasticQueries的初学者)

提前致谢。

最佳答案

编辑:无效,window_size重新计算X最佳结果的分数。
也:
需要在顶层将“track_scores”设置为true。
corect语法为:

"rescore": {
    "window_size": 10,
    "query": {
      "score_mode": "max", //wathever
      "rescore_query": {
        "bool": {
          "should": [
            {
              //your query here - you can use a function or a script score too
            }
          ]
        }
      },
      "query_weight": 0.7,
      "rescore_query_weight": 1.2
    }
  }

好吧,我理解得更好。

确实,您必须按日期排序(前100名)并使用随机函数重新评分(阅读https://www.elastic.co/guide/en/elasticsearch/reference/7.x/search-request-body.html#request-body-search-post-filter)。

应该是这样的:
{
  "sort": [
    {
      "DateAdded": {
        "order": "desc"
      }
    }
  ],
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "definitionName": {
              "value": "ABC"
            }
          }
        },
        {
          "range": {
            "price": {
              "gt": 0
            }
          }
        }
      ]
    }
  },
  "size": 100,
  "rescore": {
    "window_size": 12,
    "query": {
      "rescore_query": {
        "random_score": {
          "seed": 314159265359
        }
      }
    }
  }
}

关于elasticsearch - 查询以从Elastic Search的前100个项目中随机获取n个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60410025/

相关文章:

ruby-on-rails - Elasticsearch匹配字段=值(不包含)

laravel - 如何在AWS Elastic Search中进行批量索引

elasticsearch - 使用logstash重新出现Elasticsearch删除的文档

elasticsearch - .keyword已添加到Kibana中的所有字段以进行Elasticsearch可视化

apache - Metricbeat Apache 仪表板未显示可视化。 "Could not locate that index-pattern"

linux - 在 Linux 或 Windows 上运行 elasticsearch 有性能差异吗?

elasticsearch - 如何等待 Elasticsearch 中的重建索引?

elasticsearch - Logstash 无法启动,因为有多个实例,即使没有运行它的实例

kibana - Kibana 4 仪表板上的 OR 过滤器

elasticsearch - Kibana可视化分隔我要分组的X轴值