如何在ES中的前缀匹配中增加权重

时间:2018-09-25 07:16:48

标签: elasticsearch

索引映射如下:

from django.contrib.auth.models import User


def default_admin():
    return User.objects.get(username='admin@gmail.com')


class Store(models.Model):
    created_by = ForeignKey(settings.AUTH_USER_MODEL, related_name='stores_of_created_by', default=default_admin)

现在,我想查询索引以获取必填字段(field1,field2),其中包含单词“ apple”。下面是DSL(为field1添加权重):

"field1": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
 "field2": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
...

我更喜欢结果的排名是:

  1. 完全匹配

  2. 以“苹果”开头

  3. 包含“苹果”

例如:

{
  "aggs": {
    "field1": {
      "terms": {
        "field": "field1.keyword"
      },
      "aggs": {
        "field2": {
          "terms": {
            "field": "field2.keyword",
            "missing": ""
          }
        }
      }
    }
  },
  "query": {
    "bool": {
      "minimum_should_match": 1,
      "should": [
        {
          "multi_match": {
            "query": "apple*",
            "fields": [
              "field1^2",
              "field2"
            ]
          }
        },
        {
          "query_string": {
            "query": "*apple*",
            "fields": [
              "field1^2",
              "field2"
            ]
          }
        },
        {
          "prefix": {
            "field1": {
            "value": "apple"
            }
          }
        }
      ]
    }
  },
  "size": 0
}

但是上述DSL无法实现。我无法为“前缀”布尔查询添加权重。以“ apple”开头的项目仅次于包含“ apple”的项目。我怎样才能做到这一点?谢谢。

0 个答案:

没有答案