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
5package elastic
6
7// Filters documents that have fiels containing terms
8// with a specified prefix (not analyzed).
9// For details, see:
10// http://www.elasticsearch.org/guide/reference/query-dsl/prefix-filter.html
11type PrefixFilter struct {
12	Filter
13	name       string
14	prefix     string
15	cache      *bool
16	cacheKey   string
17	filterName string
18}
19
20func NewPrefixFilter(name string, prefix string) PrefixFilter {
21	f := PrefixFilter{name: name, prefix: prefix}
22	return f
23}
24
25func (f PrefixFilter) Cache(cache bool) PrefixFilter {
26	f.cache = &cache
27	return f
28}
29
30func (f PrefixFilter) CacheKey(cacheKey string) PrefixFilter {
31	f.cacheKey = cacheKey
32	return f
33}
34
35func (f PrefixFilter) FilterName(filterName string) PrefixFilter {
36	f.filterName = filterName
37	return f
38}
39
40func (f PrefixFilter) Source() interface{} {
41	// {
42	//   "prefix" : {
43	//     "..." : "..."
44	//   }
45	// }
46
47	source := make(map[string]interface{})
48
49	params := make(map[string]interface{})
50	source["prefix"] = params
51
52	params[f.name] = f.prefix
53
54	if f.filterName != "" {
55		params["_name"] = f.filterName
56	}
57
58	if f.cache != nil {
59		params["_cache"] = *f.cache
60	}
61
62	if f.cacheKey != "" {
63		params["_cache_key"] = f.cacheKey
64	}
65
66	return source
67}
68