search_queries_ids.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright 2012-2015 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. package elastic
  5. // Filters documents that only have the provided ids.
  6. // Note, this filter does not require the _id field to be indexed
  7. // since it works using the _uid field.
  8. // For more details, see
  9. // http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-ids-query.html
  10. type IdsQuery struct {
  11. Query
  12. types []string
  13. values []string
  14. boost float32
  15. queryName string
  16. }
  17. // NewIdsQuery creates a new ids query.
  18. func NewIdsQuery(types ...string) IdsQuery {
  19. q := IdsQuery{
  20. types: types,
  21. values: make([]string, 0),
  22. boost: -1.0,
  23. }
  24. return q
  25. }
  26. func (q IdsQuery) Ids(ids ...string) IdsQuery {
  27. q.values = append(q.values, ids...)
  28. return q
  29. }
  30. func (q IdsQuery) Boost(boost float32) IdsQuery {
  31. q.boost = boost
  32. return q
  33. }
  34. func (q IdsQuery) QueryName(queryName string) IdsQuery {
  35. q.queryName = queryName
  36. return q
  37. }
  38. // Creates the query source for the ids query.
  39. func (q IdsQuery) Source() interface{} {
  40. // {
  41. // "ids" : {
  42. // "type" : "my_type",
  43. // "values" : ["1", "4", "100"]
  44. // }
  45. // }
  46. source := make(map[string]interface{})
  47. query := make(map[string]interface{})
  48. source["ids"] = query
  49. // type(s)
  50. if len(q.types) == 1 {
  51. query["type"] = q.types[0]
  52. } else if len(q.types) > 1 {
  53. query["types"] = q.types
  54. }
  55. // values
  56. query["values"] = q.values
  57. if q.boost != -1.0 {
  58. query["boost"] = q.boost
  59. }
  60. if q.queryName != "" {
  61. query["_name"] = q.queryName
  62. }
  63. return source
  64. }