java - Java中的Elasticsearch条件评分

标签 java elasticsearch

我是Elastic Search的新手,我想问一下如何根据某些条件提升查询的某些结果。具体来说,Elastic Search中的项目具有以下格式:

"name":"item1",
"desc":"desc1",
"brand":"brandname"
我想使某些商品(带有"brand" = "nike"的商品)的得分低于其他品牌。有没有办法用Java进行 Elasticsearch 的QueryBuilders类来做到这一点?

最佳答案

Boosting查询也许可以解决问题。另外,您也可以使用function_score queries以获得更大的灵活性。
提升查询的示例。
(1)索引少量文件

curl -XPOST localhost:9200/idx/doc/1 -d '{"content": "foo"}' --header "Content-type:application/json" 
curl -XPOST localhost:9200/idx/doc/2 -d '{"content": "bar"}' --header "Content-type:application/json" 
curl -XPOST localhost:9200/idx/doc/3 -d '{"content": "foo fred bar"}' --header "Content-type:application/json" 
(2)查询bar为负数以降低bar的分数
 curl -XPOST localhost:9200/idx/_search?pretty -d '{
  "query": {
    "boosting": {
      "positive": {
        "match": {
          "content": "foo bar fred"
        }
      },
      "negative": {
        "match": {
          "content": "bar"
        }
      } , "negative_boost": 0.001
    }
  }
}' --header "Content-type:application/json" 


{
  "took" : 59,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 0.5619609,
    "hits" : [
      {
        "_index" : "idx",
        "_type" : "doc",
        "_id" : "1",
        "_score" : 0.5619609,
        "_source" : {
          "content" : "foo"
        }
      },
      {
        "_index" : "idx",
        "_type" : "doc",
        "_id" : "3",
        "_score" : 0.0014472058,
        "_source" : {
          "content" : "foo fred bar"
        }
      },
      {
        "_index" : "idx",
        "_type" : "doc",
        "_id" : "2",
        "_score" : 5.619609E-4,
        "_source" : {
          "content" : "bar"
        }
      }
    ]
  }
}

关于java - Java中的Elasticsearch条件评分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62882613/

相关文章:

java - 无法使用 maven 插件在 intellij 中导入 okhttp 项目

java - Android 版本 - 4.0.4 中的通话记录与短信日志位于同一位置

elasticsearch - ElasticSearch中的关键字搜索,与模式无关

elasticsearch - 在Elasticsearch中过滤折叠的结果

java - 为什么我会收到此编译器警告?

java - 带编码双引号的简单属性解析

java - SSL 证书未通过

elasticsearch - 在 Elasticsearch 中查找最大文档的大小

java - Elasticsearch:在传输层捕获异常

c# - 题-ElasticSearch-Nest v7.x “Ignore = true”在字段上不起作用