Browse Source

Add test for using script query

Oliver Eilhard 11 months ago
parent
commit
8bd59ebad7
1 changed files with 33 additions and 0 deletions
  1. 33
    0
      search_test.go

+ 33
- 0
search_test.go View File

@@ -1318,3 +1318,36 @@ func TestSearchResultWithFieldCollapsingAndInnerHits(t *testing.T) {
1318 1318
 		}
1319 1319
 	}
1320 1320
 }
1321
+
1322
+func TestSearchScriptQuery(t *testing.T) {
1323
+	client := setupTestClientAndCreateIndexAndAddDocs(t) //, SetTraceLog(log.New(os.Stdout, "", 0)))
1324
+
1325
+	// ES uses Painless as default scripting engine in 6.x
1326
+	// Another example of using painless would be:
1327
+	//
1328
+	//	script := NewScript(`
1329
+	//	String username = doc['user'].value;
1330
+	//	return username == 'olivere'
1331
+	//`)
1332
+	// See https://www.elastic.co/guide/en/elasticsearch/painless/6.3/painless-examples.html
1333
+	script := NewScript("doc['user'].value == 'olivere'")
1334
+	query := NewScriptQuery(script)
1335
+
1336
+	searchResult, err := client.Search().
1337
+		Index(testIndexName).
1338
+		Query(query).
1339
+		Pretty(true).
1340
+		Do(context.TODO())
1341
+	if err != nil {
1342
+		t.Fatal(err)
1343
+	}
1344
+	if searchResult.Hits == nil {
1345
+		t.Errorf("expected SearchResult.Hits != nil; got nil")
1346
+	}
1347
+	if want, have := int64(2), searchResult.Hits.TotalHits; want != have {
1348
+		t.Errorf("expected SearchResult.Hits.TotalHits = %d; got %d", want, have)
1349
+	}
1350
+	if want, have := 2, len(searchResult.Hits.Hits); want != have {
1351
+		t.Errorf("expected len(SearchResult.Hits.Hits) = %d; got %d", want, have)
1352
+	}
1353
+}

Loading…
Cancel
Save