1 Star 1 Fork 0

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

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
search_filters_bool.go 3.19 KB
一键复制 编辑 原始数据 按行查看 历史
// Copyright 2012-2015 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 filter that matches documents matching boolean combinations
// of other queries. Similar in concept to Boolean query,
// except that the clauses are other filters.
// Can be placed within queries that accept a filter.
// For more details, see:
// http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-bool-filter.html
type BoolFilter struct {
mustClauses []Filter
shouldClauses []Filter
mustNotClauses []Filter
cache *bool
cacheKey string
filterName string
}
// NewBoolFilter creates a new bool filter.
func NewBoolFilter() BoolFilter {
f := BoolFilter{
mustClauses: make([]Filter, 0),
shouldClauses: make([]Filter, 0),
mustNotClauses: make([]Filter, 0),
}
return f
}
func (f BoolFilter) Must(filters ...Filter) BoolFilter {
f.mustClauses = append(f.mustClauses, filters...)
return f
}
func (f BoolFilter) MustNot(filters ...Filter) BoolFilter {
f.mustNotClauses = append(f.mustNotClauses, filters...)
return f
}
func (f BoolFilter) Should(filters ...Filter) BoolFilter {
f.shouldClauses = append(f.shouldClauses, filters...)
return f
}
func (f BoolFilter) FilterName(filterName string) BoolFilter {
f.filterName = filterName
return f
}
func (f BoolFilter) Cache(cache bool) BoolFilter {
f.cache = &cache
return f
}
func (f BoolFilter) CacheKey(cacheKey string) BoolFilter {
f.cacheKey = cacheKey
return f
}
// Creates the query source for the bool query.
func (f BoolFilter) Source() interface{} {
// {
// "bool" : {
// "must" : {
// "term" : { "user" : "kimchy" }
// },
// "must_not" : {
// "range" : {
// "age" : { "from" : 10, "to" : 20 }
// }
// },
// "should" : [
// {
// "term" : { "tag" : "wow" }
// },
// {
// "term" : { "tag" : "elasticsearch" }
// }
// ],
// "_cache" : true
// }
// }
source := make(map[string]interface{})
boolClause := make(map[string]interface{})
source["bool"] = boolClause
// must
if len(f.mustClauses) == 1 {
boolClause["must"] = f.mustClauses[0].Source()
} else if len(f.mustClauses) > 1 {
clauses := make([]interface{}, 0)
for _, subQuery := range f.mustClauses {
clauses = append(clauses, subQuery.Source())
}
boolClause["must"] = clauses
}
// must_not
if len(f.mustNotClauses) == 1 {
boolClause["must_not"] = f.mustNotClauses[0].Source()
} else if len(f.mustNotClauses) > 1 {
clauses := make([]interface{}, 0)
for _, subQuery := range f.mustNotClauses {
clauses = append(clauses, subQuery.Source())
}
boolClause["must_not"] = clauses
}
// should
if len(f.shouldClauses) == 1 {
boolClause["should"] = f.shouldClauses[0].Source()
} else if len(f.shouldClauses) > 1 {
clauses := make([]interface{}, 0)
for _, subQuery := range f.shouldClauses {
clauses = append(clauses, subQuery.Source())
}
boolClause["should"] = clauses
}
if f.filterName != "" {
boolClause["_name"] = f.filterName
}
if f.cache != nil {
boolClause["_cache"] = *f.cache
}
if f.cacheKey != "" {
boolClause["_cache_key"] = f.cacheKey
}
return source
}
马建仓 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
v2.0.36

搜索帮助

0d507c66 1850385 C8b1a773 1850385