| 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 |
|
| 5 |
package elastic |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"fmt" |
| 10 |
"net/url" |
| 11 |
"strings" |
| 12 |
|
| 13 |
"gopkg.in/olivere/elastic.v5/uritemplates" |
| 14 |
) |
| 15 |
|
| 16 |
// TermvectorsService returns information and statistics on terms in the |
| 17 |
// fields of a particular document. The document could be stored in the |
| 18 |
// index or artificially provided by the user. |
| 19 |
// |
| 20 |
// See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/docs-termvectors.html |
| 21 |
// for documentation. |
| 22 |
type TermvectorsService struct { |
| 23 |
client *Client |
| 24 |
pretty bool |
| 25 |
id string |
| 26 |
index string |
| 27 |
typ string |
| 28 |
dfs *bool |
| 29 |
doc interface{} |
| 30 |
fieldStatistics *bool |
| 31 |
fields []string |
| 32 |
filter *TermvectorsFilterSettings |
| 33 |
perFieldAnalyzer map[string]string |
| 34 |
offsets *bool |
| 35 |
parent string |
| 36 |
payloads *bool |
| 37 |
positions *bool |
| 38 |
preference string |
| 39 |
realtime *bool |
| 40 |
routing string |
| 41 |
termStatistics *bool |
| 42 |
version interface{} |
| 43 |
versionType string |
| 44 |
bodyJson interface{} |
| 45 |
bodyString string |
| 46 |
} |
| 47 |
|
| 48 |
// NewTermvectorsService creates a new TermvectorsService. |
| 49 |
func NewTermvectorsService(client *Client) *TermvectorsService { |
| 50 |
return &TermvectorsService{ |
| 51 |
client: client, |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
// Index in which the document resides. |
| 56 |
func (s *TermvectorsService) Index(index string) *TermvectorsService { |
| 57 |
s.index = index |
| 58 |
return s |
| 59 |
} |
| 60 |
|
| 61 |
// Type of the document. |
| 62 |
func (s *TermvectorsService) Type(typ string) *TermvectorsService { |
| 63 |
s.typ = typ |
| 64 |
return s |
| 65 |
} |
| 66 |
|
| 67 |
// Id of the document. |
| 68 |
func (s *TermvectorsService) Id(id string) *TermvectorsService { |
| 69 |
s.id = id |
| 70 |
return s |
| 71 |
} |
| 72 |
|
| 73 |
// Dfs specifies if distributed frequencies should be returned instead |
| 74 |
// shard frequencies. |
| 75 |
func (s *TermvectorsService) Dfs(dfs bool) *TermvectorsService { |
| 76 |
s.dfs = &dfs |
| 77 |
return s |
| 78 |
} |
| 79 |
|
| 80 |
// Doc is the document to analyze. |
| 81 |
func (s *TermvectorsService) Doc(doc interface{}) *TermvectorsService { |
| 82 |
s.doc = doc |
| 83 |
return s |
| 84 |
} |
| 85 |
|
| 86 |
// FieldStatistics specifies if document count, sum of document frequencies |
| 87 |
// and sum of total term frequencies should be returned. |
| 88 |
func (s *TermvectorsService) FieldStatistics(fieldStatistics bool) *TermvectorsService { |
| 89 |
s.fieldStatistics = &fieldStatistics |
| 90 |
return s |
| 91 |
} |
| 92 |
|
| 93 |
// Fields a list of fields to return. |
| 94 |
func (s *TermvectorsService) Fields(fields ...string) *TermvectorsService { |
| 95 |
if s.fields == nil { |
| 96 |
s.fields = make([]string, 0) |
| 97 |
} |
| 98 |
s.fields = append(s.fields, fields...) |
| 99 |
return s |
| 100 |
} |
| 101 |
|
| 102 |
// Filter adds terms filter settings. |
| 103 |
func (s *TermvectorsService) Filter(filter *TermvectorsFilterSettings) *TermvectorsService { |
| 104 |
s.filter = filter |
| 105 |
return s |
| 106 |
} |
| 107 |
|
| 108 |
// PerFieldAnalyzer allows to specify a different analyzer than the one |
| 109 |
// at the field. |
| 110 |
func (s *TermvectorsService) PerFieldAnalyzer(perFieldAnalyzer map[string]string) *TermvectorsService { |
| 111 |
s.perFieldAnalyzer = perFieldAnalyzer |
| 112 |
return s |
| 113 |
} |
| 114 |
|
| 115 |
// Offsets specifies if term offsets should be returned. |
| 116 |
func (s *TermvectorsService) Offsets(offsets bool) *TermvectorsService { |
| 117 |
s.offsets = &offsets |
| 118 |
return s |
| 119 |
} |
| 120 |
|
| 121 |
// Parent id of documents. |
| 122 |
func (s *TermvectorsService) Parent(parent string) *TermvectorsService { |
| 123 |
s.parent = parent |
| 124 |
return s |
| 125 |
} |
| 126 |
|
| 127 |
// Payloads specifies if term payloads should be returned. |
| 128 |
func (s *TermvectorsService) Payloads(payloads bool) *TermvectorsService { |
| 129 |
s.payloads = &payloads |
| 130 |
return s |
| 131 |
} |
| 132 |
|
| 133 |
// Positions specifies if term positions should be returned. |
| 134 |
func (s *TermvectorsService) Positions(positions bool) *TermvectorsService { |
| 135 |
s.positions = &positions |
| 136 |
return s |
| 137 |
} |
| 138 |
|
| 139 |
// Preference specify the node or shard the operation |
| 140 |
// should be performed on (default: random). |
| 141 |
func (s *TermvectorsService) Preference(preference string) *TermvectorsService { |
| 142 |
s.preference = preference |
| 143 |
return s |
| 144 |
} |
| 145 |
|
| 146 |
// Realtime specifies if request is real-time as opposed to |
| 147 |
// near-real-time (default: true). |
| 148 |
func (s *TermvectorsService) Realtime(realtime bool) *TermvectorsService { |
| 149 |
s.realtime = &realtime |
| 150 |
return s |
| 151 |
} |
| 152 |
|
| 153 |
// Routing is a specific routing value. |
| 154 |
func (s *TermvectorsService) Routing(routing string) *TermvectorsService { |
| 155 |
s.routing = routing |
| 156 |
return s |
| 157 |
} |
| 158 |
|
| 159 |
// TermStatistics specifies if total term frequency and document frequency |
| 160 |
// should be returned. |
| 161 |
func (s *TermvectorsService) TermStatistics(termStatistics bool) *TermvectorsService { |
| 162 |
s.termStatistics = &termStatistics |
| 163 |
return s |
| 164 |
} |
| 165 |
|
| 166 |
// Version an explicit version number for concurrency control. |
| 167 |
func (s *TermvectorsService) Version(version interface{}) *TermvectorsService { |
| 168 |
s.version = version |
| 169 |
return s |
| 170 |
} |
| 171 |
|
| 172 |
// VersionType specifies a version type ("internal", "external", "external_gte", or "force"). |
| 173 |
func (s *TermvectorsService) VersionType(versionType string) *TermvectorsService { |
| 174 |
s.versionType = versionType |
| 175 |
return s |
| 176 |
} |
| 177 |
|
| 178 |
// Pretty indicates that the JSON response be indented and human readable. |
| 179 |
func (s *TermvectorsService) Pretty(pretty bool) *TermvectorsService { |
| 180 |
s.pretty = pretty |
| 181 |
return s |
| 182 |
} |
| 183 |
|
| 184 |
// BodyJson defines the body parameters. See documentation. |
| 185 |
func (s *TermvectorsService) BodyJson(body interface{}) *TermvectorsService { |
| 186 |
s.bodyJson = body |
| 187 |
return s |
| 188 |
} |
| 189 |
|
| 190 |
// BodyString defines the body parameters as a string. See documentation. |
| 191 |
func (s *TermvectorsService) BodyString(body string) *TermvectorsService { |
| 192 |
s.bodyString = body |
| 193 |
return s |
| 194 |
} |
| 195 |
|
| 196 |
// buildURL builds the URL for the operation. |
| 197 |
func (s *TermvectorsService) buildURL() (string, url.Values, error) { |
| 198 |
var pathParam = map[string]string{ |
| 199 |
"index": s.index, |
| 200 |
"type": s.typ, |
| 201 |
} |
| 202 |
var path string |
| 203 |
var err error |
| 204 |
|
| 205 |
// Build URL |
| 206 |
if s.id != "" { |
| 207 |
pathParam["id"] = s.id |
| 208 |
path, err = uritemplates.Expand("/{index}/{type}/{id}/_termvectors", pathParam) |
| 209 |
} else { |
| 210 |
path, err = uritemplates.Expand("/{index}/{type}/_termvectors", pathParam) |
| 211 |
} |
| 212 |
|
| 213 |
if err != nil { |
| 214 |
return "", url.Values{}, err |
| 215 |
} |
| 216 |
|
| 217 |
// Add query string parameters |
| 218 |
params := url.Values{} |
| 219 |
if s.pretty { |
| 220 |
params.Set("pretty", "1") |
| 221 |
} |
| 222 |
if s.dfs != nil { |
| 223 |
params.Set("dfs", fmt.Sprintf("%v", *s.dfs)) |
| 224 |
} |
| 225 |
if s.fieldStatistics != nil { |
| 226 |
params.Set("field_statistics", fmt.Sprintf("%v", *s.fieldStatistics)) |
| 227 |
} |
| 228 |
if len(s.fields) > 0 { |
| 229 |
params.Set("fields", strings.Join(s.fields, ",")) |
| 230 |
} |
| 231 |
if s.offsets != nil { |
| 232 |
params.Set("offsets", fmt.Sprintf("%v", *s.offsets)) |
| 233 |
} |
| 234 |
if s.parent != "" { |
| 235 |
params.Set("parent", s.parent) |
| 236 |
} |
| 237 |
if s.payloads != nil { |
| 238 |
params.Set("payloads", fmt.Sprintf("%v", *s.payloads)) |
| 239 |
} |
| 240 |
if s.positions != nil { |
| 241 |
params.Set("positions", fmt.Sprintf("%v", *s.positions)) |
| 242 |
} |
| 243 |
if s.preference != "" { |
| 244 |
params.Set("preference", s.preference) |
| 245 |
} |
| 246 |
if s.realtime != nil { |
| 247 |
params.Set("realtime", fmt.Sprintf("%v", *s.realtime)) |
| 248 |
} |
| 249 |
if s.routing != "" { |
| 250 |
params.Set("routing", s.routing) |
| 251 |
} |
| 252 |
if s.termStatistics != nil { |
| 253 |
params.Set("term_statistics", fmt.Sprintf("%v", *s.termStatistics)) |
| 254 |
} |
| 255 |
if s.version != nil { |
| 256 |
params.Set("version", fmt.Sprintf("%v", s.version)) |
| 257 |
} |
| 258 |
if s.versionType != "" { |
| 259 |
params.Set("version_type", s.versionType) |
| 260 |
} |
| 261 |
return path, params, nil |
| 262 |
} |
| 263 |
|
| 264 |
// Validate checks if the operation is valid. |
| 265 |
func (s *TermvectorsService) Validate() error { |
| 266 |
var invalid []string |
| 267 |
if s.index == "" { |
| 268 |
invalid = append(invalid, "Index") |
| 269 |
} |
| 270 |
if s.typ == "" { |
| 271 |
invalid = append(invalid, "Type") |
| 272 |
} |
| 273 |
if len(invalid) > 0 { |
| 274 |
return fmt.Errorf("missing required fields: %v", invalid) |
| 275 |
} |
| 276 |
return nil |
| 277 |
} |
| 278 |
|
| 279 |
// Do executes the operation. |
| 280 |
func (s *TermvectorsService) Do(ctx context.Context) (*TermvectorsResponse, error) { |
| 281 |
// Check pre-conditions |
| 282 |
if err := s.Validate(); err != nil { |
| 283 |
return nil, err |
| 284 |
} |
| 285 |
|
| 286 |
// Get URL for request |
| 287 |
path, params, err := s.buildURL() |
| 288 |
if err != nil { |
| 289 |
return nil, err |
| 290 |
} |
| 291 |
|
| 292 |
// Setup HTTP request body |
| 293 |
var body interface{} |
| 294 |
if s.bodyJson != nil { |
| 295 |
body = s.bodyJson |
| 296 |
} else if s.bodyString != "" { |
| 297 |
body = s.bodyString |
| 298 |
} else { |
| 299 |
data := make(map[string]interface{}) |
| 300 |
if s.doc != nil { |
| 301 |
data["doc"] = s.doc |
| 302 |
} |
| 303 |
if len(s.perFieldAnalyzer) > 0 { |
| 304 |
data["per_field_analyzer"] = s.perFieldAnalyzer |
| 305 |
} |
| 306 |
if s.filter != nil { |
| 307 |
src, err := s.filter.Source() |
| 308 |
if err != nil { |
| 309 |
return nil, err |
| 310 |
} |
| 311 |
data["filter"] = src |
| 312 |
} |
| 313 |
if len(data) > 0 { |
| 314 |
body = data |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
// Get HTTP response |
| 319 |
res, err := s.client.PerformRequest(ctx, "GET", path, params, body) |
| 320 |
if err != nil { |
| 321 |
return nil, err |
| 322 |
} |
| 323 |
|
| 324 |
// Return operation response |
| 325 |
ret := new(TermvectorsResponse) |
| 326 |
if err := s.client.decoder.Decode(res.Body, ret); err != nil { |
| 327 |
return nil, err |
| 328 |
} |
| 329 |
return ret, nil |
| 330 |
} |
| 331 |
|
| 332 |
// -- Filter settings -- |
| 333 |
|
| 334 |
// TermvectorsFilterSettings adds additional filters to a Termsvector request. |
| 335 |
// It allows to filter terms based on their tf-idf scores. |
| 336 |
// See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/docs-termvectors.html#_terms_filtering |
| 337 |
// for more information. |
| 338 |
type TermvectorsFilterSettings struct { |
| 339 |
maxNumTerms *int64 |
| 340 |
minTermFreq *int64 |
| 341 |
maxTermFreq *int64 |
| 342 |
minDocFreq *int64 |
| 343 |
maxDocFreq *int64 |
| 344 |
minWordLength *int64 |
| 345 |
maxWordLength *int64 |
| 346 |
} |
| 347 |
|
| 348 |
// NewTermvectorsFilterSettings creates and initializes a new TermvectorsFilterSettings struct. |
| 349 |
func NewTermvectorsFilterSettings() *TermvectorsFilterSettings { |
| 350 |
return &TermvectorsFilterSettings{} |
| 351 |
} |
| 352 |
|
| 353 |
// MaxNumTerms specifies the maximum number of terms the must be returned per field. |
| 354 |
func (fs *TermvectorsFilterSettings) MaxNumTerms(value int64) *TermvectorsFilterSettings { |
| 355 |
fs.maxNumTerms = &value |
| 356 |
return fs |
| 357 |
} |
| 358 |
|
| 359 |
// MinTermFreq ignores words with less than this frequency in the source doc. |
| 360 |
func (fs *TermvectorsFilterSettings) MinTermFreq(value int64) *TermvectorsFilterSettings { |
| 361 |
fs.minTermFreq = &value |
| 362 |
return fs |
| 363 |
} |
| 364 |
|
| 365 |
// MaxTermFreq ignores words with more than this frequency in the source doc. |
| 366 |
func (fs *TermvectorsFilterSettings) MaxTermFreq(value int64) *TermvectorsFilterSettings { |
| 367 |
fs.maxTermFreq = &value |
| 368 |
return fs |
| 369 |
} |
| 370 |
|
| 371 |
// MinDocFreq ignores terms which do not occur in at least this many docs. |
| 372 |
func (fs *TermvectorsFilterSettings) MinDocFreq(value int64) *TermvectorsFilterSettings { |
| 373 |
fs.minDocFreq = &value |
| 374 |
return fs |
| 375 |
} |
| 376 |
|
| 377 |
// MaxDocFreq ignores terms which occur in more than this many docs. |
| 378 |
func (fs *TermvectorsFilterSettings) MaxDocFreq(value int64) *TermvectorsFilterSettings { |
| 379 |
fs.maxDocFreq = &value |
| 380 |
return fs |
| 381 |
} |
| 382 |
|
| 383 |
// MinWordLength specifies the minimum word length below which words will be ignored. |
| 384 |
func (fs *TermvectorsFilterSettings) MinWordLength(value int64) *TermvectorsFilterSettings { |
| 385 |
fs.minWordLength = &value |
| 386 |
return fs |
| 387 |
} |
| 388 |
|
| 389 |
// MaxWordLength specifies the maximum word length above which words will be ignored. |
| 390 |
func (fs *TermvectorsFilterSettings) MaxWordLength(value int64) *TermvectorsFilterSettings { |
| 391 |
fs.maxWordLength = &value |
| 392 |
return fs |
| 393 |
} |
| 394 |
|
| 395 |
// Source returns JSON for the query. |
| 396 |
func (fs *TermvectorsFilterSettings) Source() (interface{}, error) { |
| 397 |
source := make(map[string]interface{}) |
| 398 |
if fs.maxNumTerms != nil { |
| 399 |
source["max_num_terms"] = *fs.maxNumTerms |
| 400 |
} |
| 401 |
if fs.minTermFreq != nil { |
| 402 |
source["min_term_freq"] = *fs.minTermFreq |
| 403 |
} |
| 404 |
if fs.maxTermFreq != nil { |
| 405 |
source["max_term_freq"] = *fs.maxTermFreq |
| 406 |
} |
| 407 |
if fs.minDocFreq != nil { |
| 408 |
source["min_doc_freq"] = *fs.minDocFreq |
| 409 |
} |
| 410 |
if fs.maxDocFreq != nil { |
| 411 |
source["max_doc_freq"] = *fs.maxDocFreq |
| 412 |
} |
| 413 |
if fs.minWordLength != nil { |
| 414 |
source["min_word_length"] = *fs.minWordLength |
| 415 |
} |
| 416 |
if fs.maxWordLength != nil { |
| 417 |
source["max_word_length"] = *fs.maxWordLength |
| 418 |
} |
| 419 |
return source, nil |
| 420 |
} |
| 421 |
|
| 422 |
// -- Response types -- |
| 423 |
|
| 424 |
type TokenInfo struct { |
| 425 |
StartOffset int64 `json:"start_offset"` |
| 426 |
EndOffset int64 `json:"end_offset"` |
| 427 |
Position int64 `json:"position"` |
| 428 |
Payload string `json:"payload"` |
| 429 |
} |
| 430 |
|
| 431 |
type TermsInfo struct { |
| 432 |
DocFreq int64 `json:"doc_freq"` |
| 433 |
Score float64 `json:"score"` |
| 434 |
TermFreq int64 `json:"term_freq"` |
| 435 |
Ttf int64 `json:"ttf"` |
| 436 |
Tokens []TokenInfo `json:"tokens"` |
| 437 |
} |
| 438 |
|
| 439 |
type FieldStatistics struct { |
| 440 |
DocCount int64 `json:"doc_count"` |
| 441 |
SumDocFreq int64 `json:"sum_doc_freq"` |
| 442 |
SumTtf int64 `json:"sum_ttf"` |
| 443 |
} |
| 444 |
|
| 445 |
type TermVectorsFieldInfo struct { |
| 446 |
FieldStatistics FieldStatistics `json:"field_statistics"` |
| 447 |
Terms map[string]TermsInfo `json:"terms"` |
| 448 |
} |
| 449 |
|
| 450 |
// TermvectorsResponse is the response of TermvectorsService.Do. |
| 451 |
type TermvectorsResponse struct { |
| 452 |
Index string `json:"_index"` |
| 453 |
Type string `json:"_type"` |
| 454 |
Id string `json:"_id,omitempty"` |
| 455 |
Version int `json:"_version"` |
| 456 |
Found bool `json:"found"` |
| 457 |
Took int64 `json:"took"` |
| 458 |
TermVectors map[string]TermVectorsFieldInfo `json:"term_vectors"` |
| 459 |
} |