elasticsearch - Elasticsearch如何排除分数= 0文档

标签 elasticsearch lucene

我有这样的查询:

{ 
    "size": 4, 
    "query": { 
        "bool": { 
            "should": [{ 
                "terms": { "property": ["iPhone earbuds", "iPhone 6 case"] } 
            }, { 
                "constant_score": { 
                    "filter": { "match_all": {} }, 
                    "boost": 0 
                } 
            }], 
            "must_not": { 
                "ids": { 
                    "values": ["doc 1", "doc 2"], 
                    "boost": 0 
                } 
            }, 
            "minimum_should_match": 1 
        } 
    }, 
    "sort": [] 
}

一些空对象只是我如何构建查询JSON的人工产物,它们在大多数情况下都具有含义,这只是我找到的最简单的示例。

为什么会以分数= 0返回文档,我该如何阻止这种情况发生?

最佳答案

那会是

{ 
    "constant_score": { 
        "filter": { "match_all": {} },
        "boost": 0
    }
}

匹配所有内容,这部分的得分为0;这也满足您的"minimum_should_match": 1规则。

您可以进一步简化示例(但我仍然不确定此查询要实现什么):
PUT /test/test/1
{
  "foo": "iphone"
}
PUT /test/test/2
{
  "foo": "iphone earbuds"
}
PUT /test/test/3
{
  "foo": "iphone case"
}
PUT /test/test/4
{
  "foo": "bar"
}

POST /test/_search
{
  "query": { 
        "bool": { 
            "should": [
                { 
                    "terms": { "foo": ["iphone"] } 
                }, { 
                    "constant_score": { 
                        "filter": { "match_all": {} },
                        "boost": 0 
                    }
                }
            ], 
            "minimum_should_match": 1 
        } 
    }
}

DELETE /test

关于elasticsearch - Elasticsearch如何排除分数= 0文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41473796/

相关文章:

elasticsearch - Elasticsearch数组(标签/标签查询

elasticsearch - 创建具有30-40或更多列的索引时,我应该了解/关注什么?

elasticsearch - 在不同级别查询多级嵌套文档

amazon-web-services - AWS ElasticSearch Service - 从 CF 模板设置加密选项

elasticsearch - 如何为Kibana 5.6.3创建插件

grails - 将 grails 应用程序升级到 2.3.7 时出错

java - 如何在 Apache Lucene 中将 PhraseQuery 与 RangeQuery 结合起来?

search - Solr 中的通配符搜索

Lucene 6 添加 IntFields

algorithm - 图像抓取和索引算法(通过图像的颜色)和文本搜索给出相应的图像