弹性-在进行增强查询+必须查询时Desc排序不再起作用 [英] Elastic - Desc sort is not working anymore when doing boost query + must query

查看:68
本文介绍了弹性-在进行增强查询+必须查询时Desc排序不再起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在按照优先级进行排序,而该优先级已在我以前的帖子中得到解决:弹性-优先排序值

So, i'm doing sort with priority that already solved on my previous post: Elastic - Sorting value with priority

但是我发现了新的问题,当我想过滤具有指定字段的数据时,timeInt的查询排序不再起作用

But i found the new problem, when i want to filter data with specified field, the query sort of timeInt doesn't work anymore

我已经尝试过该查询,但是timeInt的查询无法正常工作.这是我的查询:

I've tried this query, but the query sort of timeInt doesn't work. Here is my query:

{
    query: {
        bool: {
        must: {
            match: {
            'flag_type': "contract"
            }
        },
        should: [
            {
            match: {
                timeInt: {
                query: 0,
                boost: 3
                }
            }
            },
            {
            match: {
                timeInt: {
                query: 1,
                boost: 2
                }
            }
            }
        ]
        }
    },
    sort: [
        { _score: "desc"},
        {
            timeInt: {
                order: "desc"
            }
        }
    ]
}

注意:如果我删除查询排序: {_score:"desc"} desc sort工作正常,但无法将值0/1提升到顶部.

NOTE: If i delete the query sort: { _score: "desc" } desc sort is working properly, but can't boost up value 0 / 1 to the top.

预期结果:

0、1、100、99、98等...

0, 1, 100, 99, 98, etc...

当前结果:

如果我删除 {_score:"desc"} :100、99、98、97、96等...

If i delete { _score: "desc" }: 100, 99, 98, 97, 96, etc...

上面的查询:0、1、15、99、100、70、2等...

With query above: 0, 1, 15, 99, 100, 70, 2, etc...

我的查询出了什么问题?

What's wrong with my query?

请帮助我

谢谢.

推荐答案

为延迟回复表示歉意,这是您在具有增强逻辑的基础上还可以做的事情.

Apologies for the delay in reply, here is what you can do additionally on top of having the boosting logic.

我创建了一个自定义排序逻辑.因此,基本上,这将是首先基于 _score 对结果进行排序,然后根据 timeInt 字段的排序逻辑对结果进行排序.

I've created a custom sorting logic. So basically what this would do is, it would first sort the results based on _score and then based on sort logic of timeInt field, the results would get sorted.

自从我提升了 0 1 以来,boost的值以及基于我实现的自定义排序的排序逻辑中的值将始终根据您的查找返回 0 1 .

Since I've boosted 0 and 1 and that the values of boost as well as their values in sorting logic based on custom sort I've implemented, would always return 0 and 1 as per what you are looking for.

这应该可以帮助您获得所需的内容.

This should help you get what you are looking for.

POST myintindex/_search
{  
   "query":{  
      "bool": {
        "must": [
          {
            "match": {
              "flag_type": "contract"
            }
          }
        ],
        "should": [
          {
            "match": {
              "timeInt": {
                "query": "0",
                "boost": 200000
              }
            }
          },
          {
            "match": {
              "timeInt": {
                "query": "1",
                "boost": 10000
              }
            }
          }
        ]
      }
   },
   "sort":[  
      { "_score": {"order": "desc"}},
      {  
         "_script":{  
            "type":"number",
            "script":{  
               "lang":"painless",
               "source":"""
               String st = String.valueOf(doc['timeInt'].value);
               if(params.scores.containsKey(st)) { 
                 return params.scores[st];
               } 
               return doc['timeInt'].value;
               """,
               "params":{  
                  "scores":{  
                     "0":200000,
                     "1":100000
                  }
               }
            },
            "order":"desc"
         }
      }
   ]
}

再次道歉,我花了这么长时间做出回应,但我希望这会有所帮助!

Once again, I apologise it took me this long to respond, but I hope this helps!

这篇关于弹性-在进行增强查询+必须查询时Desc排序不再起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆