Skip to content

Commit f7acaa1

Browse files
committed
add SearchSource; refactor search and highlight
* Add SearchSource (SearchSourceBuilder in Elasticsearch) * Use SearchSource in SearchService to build search body * Refactor Highlight to represent structure of ES 1.3.0 * Add FetchSourceContext * Add Rescore and Rescorer * Move SortInfo into its own file
1 parent e5a80b0 commit f7acaa1

11 files changed

+1087
-268
lines changed

fetch_source_context.go

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package elastic
2+
3+
type FetchSourceContext struct {
4+
fetchSource bool
5+
transformSource bool
6+
includes []string
7+
excludes []string
8+
}
9+
10+
func NewFetchSourceContext(fetchSource bool) *FetchSourceContext {
11+
return &FetchSourceContext{
12+
fetchSource: fetchSource,
13+
includes: make([]string, 0),
14+
excludes: make([]string, 0),
15+
}
16+
}
17+
18+
func (fsc *FetchSourceContext) FetchSource() bool {
19+
return fsc.fetchSource
20+
}
21+
22+
func (fsc *FetchSourceContext) SetFetchSource(fetchSource bool) {
23+
fsc.fetchSource = fetchSource
24+
}
25+
26+
func (fsc *FetchSourceContext) Include(includes ...string) *FetchSourceContext {
27+
fsc.includes = append(fsc.includes, includes...)
28+
return fsc
29+
}
30+
31+
func (fsc *FetchSourceContext) Exclude(excludes ...string) *FetchSourceContext {
32+
fsc.excludes = append(fsc.excludes, excludes...)
33+
return fsc
34+
}
35+
36+
func (fsc *FetchSourceContext) TransformSource(transformSource bool) *FetchSourceContext {
37+
fsc.transformSource = transformSource
38+
return fsc
39+
}
40+
41+
func (fsc *FetchSourceContext) Source() interface{} {
42+
if !fsc.fetchSource {
43+
return false
44+
}
45+
return map[string]interface{}{
46+
"includes": fsc.includes,
47+
"excludes": fsc.excludes,
48+
}
49+
}

fetch_source_context_test.go

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package elastic
2+
3+
import (
4+
"encoding/json"
5+
"testing"
6+
)
7+
8+
func TestFetchSourceContextNoFetchSource(t *testing.T) {
9+
builder := NewFetchSourceContext(false)
10+
data, err := json.Marshal(builder.Source())
11+
if err != nil {
12+
t.Fatalf("marshaling to JSON failed: %v", err)
13+
}
14+
got := string(data)
15+
expected := `false`
16+
if got != expected {
17+
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
18+
}
19+
}
20+
21+
func TestFetchSourceContextNoFetchSourceIgnoreIncludesAndExcludes(t *testing.T) {
22+
builder := NewFetchSourceContext(false).Include("a", "b").Exclude("c")
23+
data, err := json.Marshal(builder.Source())
24+
if err != nil {
25+
t.Fatalf("marshaling to JSON failed: %v", err)
26+
}
27+
got := string(data)
28+
expected := `false`
29+
if got != expected {
30+
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
31+
}
32+
}
33+
34+
func TestFetchSourceContextFetchSource(t *testing.T) {
35+
builder := NewFetchSourceContext(true)
36+
data, err := json.Marshal(builder.Source())
37+
if err != nil {
38+
t.Fatalf("marshaling to JSON failed: %v", err)
39+
}
40+
got := string(data)
41+
expected := `{"excludes":[],"includes":[]}`
42+
if got != expected {
43+
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
44+
}
45+
}
46+
47+
func TestFetchSourceContextFetchSourceWithIncludesAndExcludes(t *testing.T) {
48+
builder := NewFetchSourceContext(true).Include("a", "b").Exclude("c")
49+
data, err := json.Marshal(builder.Source())
50+
if err != nil {
51+
t.Fatalf("marshaling to JSON failed: %v", err)
52+
}
53+
got := string(data)
54+
expected := `{"excludes":["c"],"includes":["a","b"]}`
55+
if got != expected {
56+
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
57+
}
58+
}

0 commit comments

Comments
 (0)