1// Copyright 2012-present Oliver Eilhard. All rights reserved. 2// Use of this source code is governed by a MIT-license. 3// See http://olivere.mit-license.org/license.txt for details. 4 5package elastic 6 7// IdsQuery filters documents that only have the provided ids. 8// Note, this query uses the _uid field. 9// 10// For more details, see 11// https://www.elastic.co/guide/en/elasticsearch/reference/5.2/query-dsl-ids-query.html 12type IdsQuery struct { 13 types []string 14 values []string 15 boost *float64 16 queryName string 17} 18 19// NewIdsQuery creates and initializes a new ids query. 20func NewIdsQuery(types ...string) *IdsQuery { 21 return &IdsQuery{ 22 types: types, 23 values: make([]string, 0), 24 } 25} 26 27// Ids adds ids to the filter. 28func (q *IdsQuery) Ids(ids ...string) *IdsQuery { 29 q.values = append(q.values, ids...) 30 return q 31} 32 33// Boost sets the boost for this query. 34func (q *IdsQuery) Boost(boost float64) *IdsQuery { 35 q.boost = &boost 36 return q 37} 38 39// QueryName sets the query name for the filter. 40func (q *IdsQuery) QueryName(queryName string) *IdsQuery { 41 q.queryName = queryName 42 return q 43} 44 45// Source returns JSON for the function score query. 46func (q *IdsQuery) Source() (interface{}, error) { 47 // { 48 // "ids" : { 49 // "type" : "my_type", 50 // "values" : ["1", "4", "100"] 51 // } 52 // } 53 54 source := make(map[string]interface{}) 55 query := make(map[string]interface{}) 56 source["ids"] = query 57 58 // type(s) 59 if len(q.types) == 1 { 60 query["type"] = q.types[0] 61 } else if len(q.types) > 1 { 62 query["types"] = q.types 63 } 64 65 // values 66 query["values"] = q.values 67 68 if q.boost != nil { 69 query["boost"] = *q.boost 70 } 71 if q.queryName != "" { 72 query["_name"] = q.queryName 73 } 74 75 return source, nil 76} 77