inner_hit.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. package elastic
  5. // InnerHit implements a simple join for parent/child, nested, and even
  6. // top-level documents in Elasticsearch.
  7. // It is an experimental feature for Elasticsearch versions 1.5 (or greater).
  8. // See http://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-request-inner-hits.html
  9. // for documentation.
  10. //
  11. // See the tests for SearchSource, HasChildFilter, HasChildQuery,
  12. // HasParentFilter, HasParentQuery, NestedFilter, and NestedQuery
  13. // for usage examples.
  14. type InnerHit struct {
  15. source *SearchSource
  16. path string
  17. typ string
  18. name string
  19. }
  20. // NewInnerHit creates a new InnerHit.
  21. func NewInnerHit() *InnerHit {
  22. return &InnerHit{source: NewSearchSource()}
  23. }
  24. func (hit *InnerHit) Path(path string) *InnerHit {
  25. hit.path = path
  26. return hit
  27. }
  28. func (hit *InnerHit) Type(typ string) *InnerHit {
  29. hit.typ = typ
  30. return hit
  31. }
  32. func (hit *InnerHit) Query(query Query) *InnerHit {
  33. hit.source.Query(query)
  34. return hit
  35. }
  36. func (hit *InnerHit) From(from int) *InnerHit {
  37. hit.source.From(from)
  38. return hit
  39. }
  40. func (hit *InnerHit) Size(size int) *InnerHit {
  41. hit.source.Size(size)
  42. return hit
  43. }
  44. func (hit *InnerHit) TrackScores(trackScores bool) *InnerHit {
  45. hit.source.TrackScores(trackScores)
  46. return hit
  47. }
  48. func (hit *InnerHit) Explain(explain bool) *InnerHit {
  49. hit.source.Explain(explain)
  50. return hit
  51. }
  52. func (hit *InnerHit) Version(version bool) *InnerHit {
  53. hit.source.Version(version)
  54. return hit
  55. }
  56. func (hit *InnerHit) StoredField(storedFieldName string) *InnerHit {
  57. hit.source.StoredField(storedFieldName)
  58. return hit
  59. }
  60. func (hit *InnerHit) StoredFields(storedFieldNames ...string) *InnerHit {
  61. hit.source.StoredFields(storedFieldNames...)
  62. return hit
  63. }
  64. func (hit *InnerHit) NoStoredFields() *InnerHit {
  65. hit.source.NoStoredFields()
  66. return hit
  67. }
  68. func (hit *InnerHit) FetchSource(fetchSource bool) *InnerHit {
  69. hit.source.FetchSource(fetchSource)
  70. return hit
  71. }
  72. func (hit *InnerHit) FetchSourceContext(fetchSourceContext *FetchSourceContext) *InnerHit {
  73. hit.source.FetchSourceContext(fetchSourceContext)
  74. return hit
  75. }
  76. func (hit *InnerHit) DocvalueFields(docvalueFields ...string) *InnerHit {
  77. hit.source.DocvalueFields(docvalueFields...)
  78. return hit
  79. }
  80. func (hit *InnerHit) DocvalueField(docvalueField string) *InnerHit {
  81. hit.source.DocvalueField(docvalueField)
  82. return hit
  83. }
  84. func (hit *InnerHit) ScriptFields(scriptFields ...*ScriptField) *InnerHit {
  85. hit.source.ScriptFields(scriptFields...)
  86. return hit
  87. }
  88. func (hit *InnerHit) ScriptField(scriptField *ScriptField) *InnerHit {
  89. hit.source.ScriptField(scriptField)
  90. return hit
  91. }
  92. func (hit *InnerHit) Sort(field string, ascending bool) *InnerHit {
  93. hit.source.Sort(field, ascending)
  94. return hit
  95. }
  96. func (hit *InnerHit) SortWithInfo(info SortInfo) *InnerHit {
  97. hit.source.SortWithInfo(info)
  98. return hit
  99. }
  100. func (hit *InnerHit) SortBy(sorter ...Sorter) *InnerHit {
  101. hit.source.SortBy(sorter...)
  102. return hit
  103. }
  104. func (hit *InnerHit) Highlight(highlight *Highlight) *InnerHit {
  105. hit.source.Highlight(highlight)
  106. return hit
  107. }
  108. func (hit *InnerHit) Highlighter() *Highlight {
  109. return hit.source.Highlighter()
  110. }
  111. func (hit *InnerHit) Name(name string) *InnerHit {
  112. hit.name = name
  113. return hit
  114. }
  115. func (hit *InnerHit) Source() (interface{}, error) {
  116. src, err := hit.source.Source()
  117. if err != nil {
  118. return nil, err
  119. }
  120. source, ok := src.(map[string]interface{})
  121. if !ok {
  122. return nil, nil
  123. }
  124. // Notice that hit.typ and hit.path are not exported here.
  125. // They are only used with SearchSource and serialized there.
  126. if hit.name != "" {
  127. source["name"] = hit.name
  128. }
  129. return source, nil
  130. }