elasticsearch条件Boost索引时间和查询时间

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

我有twitter_user_index我想提高索引时间的推文分数

If user.verified:
   boost: 10
elif user.follower_count in range (1, 100):
   boost: 1
elif user.follower_count in range (101, 200):
   boost: 2

我如何在索引时间或查询时间上实现这种提升(欢迎回答)

elasticsearch
1个回答
0
投票

我已经在下面的映射中获取了索引。 “用户”我已视为“对象类型”,如果您的类型是“嵌套”,则需要使用嵌套查询,其余结构将相同。

映射:

PUT twitter_user_index 
  {
    "mappings": {
      "properties": {
        "user":{
          "type": "object",
          "properties": {
            "name":{
              "type":"text",
              "fields":{
                "keyword":{
                  "type":"keyword"
                }
              }
            },
            "verified":{
              "type":"boolean"
            },
            "follower_count":{
              "type": "integer"
            }
          }
        }
      }
    }
  }

数据:我已经记录了三张记录。 2个用户的经过验证的帐户分别具有少于100个关注者和100个关注者,而1个用户的未经验证的帐户中具有超过100个关注者]

"hits" : [
      {
        "_index" : "twitter_user_index",
        "_type" : "_doc",
        "_id" : "gwiu-HEBZgLhu13ZIerO",
        "_score" : 1.0,
        "_source" : {
          "user" : {
            "name" : "abc",
            "verified" : true,
            "follower_count" : 90
          }
        }
      },
      {
        "_index" : "twitter_user_index",
        "_type" : "_doc",
        "_id" : "hAiu-HEBZgLhu13Za-qL",
        "_score" : 1.0,
        "_source" : {
          "user" : {
            "name" : "efg",
            "verified" : true,
            "follower_count" : 120
          }
        }
      },
      {
        "_index" : "twitter_user_index",
        "_type" : "_doc",
        "_id" : "hQiu-HEBZgLhu13ZhOrr",
        "_score" : 1.0,
        "_source" : {
          "user" : {
            "name" : "xyz",
            "verified" : false,
            "follower_count" : 120
          }
        }
      }
    ]

Query:

我使用了should子句,该子句不会过滤掉任何文档,一个得分更高的文档匹配。每个子句都可以增强关联性。
GET twitter_user_index/_search
  {
    "query": {
      "bool": {
        "should": [
          {
            "term": {
              "user.verified": {
                "value": true,
                "boost": 10
              }
            }
          },
          {
            "range": {
              "user.follower_count": {
                "gte": 1,
                "lte": 100,
                "boost": 1
              }
            }
          },
          {
            "range": {
              "user.follower_count": {
                "gte": 101,
                "lte": 200,
                "boost": 2
              }
            }
          }
        ]
      }
    }
  }

[结果:

在查询的输出中,已验证帐户的得分要高于未验证帐户,并且在给定范围内关注者的帐户得分会更高。
"hits" : [
      {
        "_index" : "twitter_user_index",
        "_type" : "_doc",
        "_id" : "hAiu-HEBZgLhu13Za-qL",
        "_score" : 6.700036,
        "_source" : {
          "user" : {
            "name" : "efg",
            "verified" : true,
            "follower_count" : 120
          }
        }
      },
      {
        "_index" : "twitter_user_index",
        "_type" : "_doc",
        "_id" : "gwiu-HEBZgLhu13ZIerO",
        "_score" : 5.700036,
        "_source" : {
          "user" : {
            "name" : "abc",
            "verified" : true,
            "follower_count" : 90
          }
        }
      },
      {
        "_index" : "twitter_user_index",
        "_type" : "_doc",
        "_id" : "hQiu-HEBZgLhu13ZhOrr",
        "_score" : 2.0,
        "_source" : {
          "user" : {
            "name" : "xyz",
            "verified" : false,
            "follower_count" : 120
          }
        }
      }
    ]
© www.soinside.com 2019 - 2024. All rights reserved.