是否有方法更改两次出现之间的加法操作(在布尔查询中)

u91tlkcl  于 2021-06-14  发布在  ElasticSearch
关注(0)|答案(1)|浏览(411)

我有下一个问题:

curl -X POST "localhost:9200/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "query": {
    "bool" : {

      "must" : 
        { "term" : 
          { "message" : 
            {
              "value": "message",
              "boost": 2.0 
            }
          } 
        },
        "should" : 
        { "term" : 
          { "message" : 
            {
              "value": "message",
              "boost": 2.0 
            }
          } 
        }

    }
  }
}
'

这个 _score 我从这个React中得到的是 _score (我没有望远镜也能看到( must / should ) )
我的问题是,是否有一种方法可以将动作改为倍数而不是加法
例如-取而代之 _score + _score 我去拿 _score * _score

rdrgkggo

rdrgkggo1#

您可以使用函数得分查询
一个工作示例(使用脚本分数): _score 将乘以 _score Map

PUT index
{
  "mappings": {
    "properties": {
      "message": {
        "type": "keyword"
      }
    }
  }
}

插入文档

POST index/_doc/1
{
  "message":"message"
}

POST index/_doc/2
{
  "message":"message"
}

搜索查询不带 script_score ```
GET index/_search
{
"query": {
"function_score": {
"query": {
"bool": {
"must": [
{
"term": {
"message": {
"value": "message"
}
}
}
],"should": [
{
"term": {
"message": {
"value": "message"
}
}
}
]
}
}
}
}
}

结果:

"hits" : [
{
"_index" : "index",
"_type" : "_doc",
"_id" : "3",
"_score" : 1.3862944,
"_source" : {
"message" : "message"
}
},
{
"_index" : "index",
"_type" : "_doc",
"_id" : "4",
"_score" : 1.3862944,
"_source" : {
"message" : "message"
}
}
]

搜索查询使用 `script_score` ```
GET index/_search
{
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "must": [
            {
              "term": {
                "message": {
                  "value": "message"
                }
              }
            }
          ],"should": [
            {
              "term": {
                "message": {
                  "value": "message"
                }
              }
            }
          ]
        }
      },
      "script_score": {
        "script": {
          "source": "_score"
        }
      }
    }
  }
}

结果

"hits" : [
      {
        "_index" : "index",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 1.921812,
        "_source" : {
          "message" : "message"
        }
      },
      {
        "_index" : "index",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 1.921812,
        "_source" : {
          "message" : "message"
        }
      }
    ]

相关问题