Elasticsearch 按相关性分数顺序搜索所有文档

Elasticsearch search all the documents, in order of relavancy score

提问人:v78 提问时间:4/3/2021 更新时间:4/3/2021 访问量:47

问:

我在新闻头条文档列表中有一个复杂的匹配查询,

{
  "bool" : {
    "should" : [
      {
        "multi_match" : {
          "query" : " Reliance gets shareholders, creditors nod for hiving off O2C business into separate unit - The HinduBillionaire Mukesh Ambani's Reliance Industries Ltd on Friday said it has secured approval of its shareholders and creditors for hiving off its oil-to-chemical (O2C) business into a separate unit.",
          "fields" : [
            "article.description^1.0",
            "article.title^1.0"
          ],
          "type" : "best_fields",
          "operator" : "OR",
          "slop" : 0,
          "prefix_length" : 0,
          "max_expansions" : 50,
          "zero_terms_query" : "NONE",
          "auto_generate_synonyms_phrase_query" : true,
          "fuzzy_transpositions" : true,
          "boost" : 3.0
        }
      },
      {
        "match" : {
          "article.author" : {
            "query" : " PTI",
            "operator" : "OR",
            "prefix_length" : 0,
            "max_expansions" : 50,
            "fuzzy_transpositions" : true,
            "lenient" : false,
            "zero_terms_query" : "NONE",
            "auto_generate_synonyms_phrase_query" : true,
            "boost" : 1.0
          }
        }
      },
      {
        "match" : {
          "article.source.name" : {
            "query" : " The Hindu",
            "operator" : "OR",
            "prefix_length" : 0,
            "max_expansions" : 50,
            "fuzzy_transpositions" : true,
            "lenient" : false,
            "zero_terms_query" : "NONE",
            "auto_generate_synonyms_phrase_query" : true,
            "boost" : 1.0
          }
        }
      }
    ],
    "adjust_pure_negative" : true,
    "boost" : 1.0
  }
}

问题是 elasticsearch 只返回相关文档,我想要尽可能多的文档,按相关分数递减的顺序排列。归还所有文件是可以的,但订购应该按这个顺序。我找不到更好的方法 elasticsearch 从新闻存储库返回 5-10 个文档,而我有 1000 多篇文章。

Java Elasticsearch 匹配 spring-data-elasticsearch

评论


答:

1赞 ESCoder 4/3/2021 #1

默认情况下,elasticsearch 仅返回 10 个文档。如果要返回 10 个以上的文档,则需要设置 size 参数。

修改后的查询将是

{
  "size": 1000,         // note this
  "query": {
    "bool": {
      "should": [
        {
          "multi_match": {
            "query": " Reliance gets shareholders, creditors nod for hiving off O2C business into separate unit - The HinduBillionaire Mukesh Ambani's Reliance Industries Ltd on Friday said it has secured approval of its shareholders and creditors for hiving off its oil-to-chemical (O2C) business into a separate unit.",
            "fields": [
              "article.description^1.0",
              "article.title^1.0"
            ],
            "type": "best_fields",
            "operator": "OR",
            "slop": 0,
            "prefix_length": 0,
            "max_expansions": 50,
            "zero_terms_query": "NONE",
            "auto_generate_synonyms_phrase_query": true,
            "fuzzy_transpositions": true,
            "boost": 3.0
          }
        },
        {
          "match": {
            "article.author": {
              "query": " PTI",
              "operator": "OR",
              "prefix_length": 0,
              "max_expansions": 50,
              "fuzzy_transpositions": true,
              "lenient": false,
              "zero_terms_query": "NONE",
              "auto_generate_synonyms_phrase_query": true,
              "boost": 1.0
            }
          }
        },
        {
          "match": {
            "article.source.name": {
              "query": " The Hindu",
              "operator": "OR",
              "prefix_length": 0,
              "max_expansions": 50,
              "fuzzy_transpositions": true,
              "lenient": false,
              "zero_terms_query": "NONE",
              "auto_generate_synonyms_phrase_query": true,
              "boost": 1.0
            }
          }
        }
      ],
      "adjust_pure_negative": true,
      "boost": 1.0
    }
  }
}

评论

1赞 v78 4/7/2021
这解决了我的问题,谢谢你的回答