1 Star 1 Fork 0

宇宙蒙面侠X/github.com-olivere-elastic

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
search_queries_boosting.go 2.19 KB
一键复制 编辑 原始数据 按行查看 历史
Oliver Eilhard 提交于 2017-02-10 14:58 . Fix documentation links
// Copyright 2012-present Oliver Eilhard. All rights reserved.
// Use of this source code is governed by a MIT-license.
// See http://olivere.mit-license.org/license.txt for details.
package elastic
// A boosting query can be used to effectively
// demote results that match a given query.
// For more details, see:
// https://www.elastic.co/guide/en/elasticsearch/reference/5.2/query-dsl-boosting-query.html
type BoostingQuery struct {
Query
positiveClause Query
negativeClause Query
negativeBoost *float64
boost *float64
}
// Creates a new boosting query.
func NewBoostingQuery() *BoostingQuery {
return &BoostingQuery{}
}
func (q *BoostingQuery) Positive(positive Query) *BoostingQuery {
q.positiveClause = positive
return q
}
func (q *BoostingQuery) Negative(negative Query) *BoostingQuery {
q.negativeClause = negative
return q
}
func (q *BoostingQuery) NegativeBoost(negativeBoost float64) *BoostingQuery {
q.negativeBoost = &negativeBoost
return q
}
func (q *BoostingQuery) Boost(boost float64) *BoostingQuery {
q.boost = &boost
return q
}
// Creates the query source for the boosting query.
func (q *BoostingQuery) Source() (interface{}, error) {
// {
// "boosting" : {
// "positive" : {
// "term" : {
// "field1" : "value1"
// }
// },
// "negative" : {
// "term" : {
// "field2" : "value2"
// }
// },
// "negative_boost" : 0.2
// }
// }
query := make(map[string]interface{})
boostingClause := make(map[string]interface{})
query["boosting"] = boostingClause
// Negative and positive clause as well as negative boost
// are mandatory in the Java client.
// positive
if q.positiveClause != nil {
src, err := q.positiveClause.Source()
if err != nil {
return nil, err
}
boostingClause["positive"] = src
}
// negative
if q.negativeClause != nil {
src, err := q.negativeClause.Source()
if err != nil {
return nil, err
}
boostingClause["negative"] = src
}
if q.negativeBoost != nil {
boostingClause["negative_boost"] = *q.negativeBoost
}
if q.boost != nil {
boostingClause["boost"] = *q.boost
}
return query, nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/awol2010ex/github.com-olivere-elastic.git
git@gitee.com:awol2010ex/github.com-olivere-elastic.git
awol2010ex
github.com-olivere-elastic
github.com-olivere-elastic
v5.0.68

搜索帮助

0d507c66 1850385 C8b1a773 1850385