我们可以优化这个弹性查询以获得更好的性能吗

问题描述 投票:0回答:1

弹性查询:

{
  "from": 0,
  "size": 50,
  "query": {
    "query_string": {
      "query": "((txtdisplayname:leave) OR (txtsearchkeyword:leave) OR (txtdisplayname:leave*) OR (txtsearchkeyword:leave*)) AND (txtbenefitid:* OR txtbenefitid:NULL)"
    }
  },
  "sort": [
    {
      "_script": {
        "script": {
          "source": "def order = ['Manage Leave','Holiday Calendar','Onsite Leave']; int index = order.indexOf(doc['txtdisplayname.keyword'].value); return index == -1 ? _score :_score *( 1.0 / (index + 1)) * 50;"
        },
        "type": "number",
        "order": "desc"
      }
    },
    {
      "_score": {
        "order": "desc"
      }
    }
  ]
}

对于查询休假,我将得到一些结果,如果存在管理休假、假日日历和现场休假,我需要将这些结果优先考虑到第一个结果,其他结果应该遵循它们,上面的查询也有帮助,但是是否有可能优化查询以获得更好的性能是值得怀疑的。

spring-boot elasticsearch kibana
1个回答
0
投票

您不需要诉诸脚本评分。您可以简单地在

bool/must
中进行查询字符串查询,然后使用
bool/should
来增强一些结果,并为每个您想要的值使用不同的增强参数。

可以根据您的脚本轻松选择提升值:

  • Manage Leave
    的索引为 0,因此
    1 / 1 * 50 = 50
  • Holiday Calendar
    的索引为 1,因此
    1 / 2 * 50 = 25
  • Onsite Leave
    的索引为 2,因此
    1 / 3 * 50 = 17

以下查询应该已经更加高效:

POST test/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "query": "((txtdisplayname:leave) OR (txtsearchkeyword:leave) OR (txtdisplayname:leave*) OR (txtsearchkeyword:leave*)) AND (txtbenefitid:* OR txtbenefitid:NULL)"
          }
        }
      ],
      "should": [
        {
          "match": {
            "txtdisplayname.keyword": {
              "query": "Manage Leave",
              "boost": 50
            }
          }
        },
        {
          "match": {
            "txtdisplayname.keyword": {
              "query": "Holiday Calendar",
              "boost": 25
            }
          }
        },
        {
          "match": {
            "txtdisplayname.keyword": {
              "query": "Onsite Leave",
              "boost": 17
            }
          }
        }
      ]
    }
  }
}

下一个性能消耗者将是文本字段上的通配符搜索

txtdisplayname:leave*
,但我会将其留作家庭作业:-)

© www.soinside.com 2019 - 2024. All rights reserved.