use of org.elasticsearch.action.index.IndexRequestBuilder in project elasticsearch by elastic.
the class DecayFunctionScoreIT method testBoostModeSettingWorks.
public void testBoostModeSettingWorks() throws Exception {
Settings settings = Settings.builder().put(IndexMetaData.INDEX_NUMBER_OF_SHARDS_SETTING.getKey(), 1).build();
assertAcked(prepareCreate("test").setSettings(settings).addMapping("type1", jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("test").field("type", "text").endObject().startObject("loc").field("type", "geo_point").endObject().endObject().endObject().endObject()));
List<IndexRequestBuilder> indexBuilders = new ArrayList<>();
indexBuilders.add(client().prepareIndex().setType("type1").setId("1").setIndex("test").setSource(jsonBuilder().startObject().field("test", "value").startObject("loc").field("lat", 11).field("lon", 21).endObject().endObject()));
indexBuilders.add(client().prepareIndex().setType("type1").setId("2").setIndex("test").setSource(jsonBuilder().startObject().field("test", "value value").startObject("loc").field("lat", 11).field("lon", 20).endObject().endObject()));
// force no dummy docs
indexRandom(true, false, indexBuilders);
// Test Gauss
List<Float> lonlat = new ArrayList<>();
lonlat.add(20f);
lonlat.add(11f);
ActionFuture<SearchResponse> response = client().search(searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(searchSource().query(functionScoreQuery(termQuery("test", "value"), gaussDecayFunction("loc", lonlat, "1000km")).boostMode(CombineFunction.MULTIPLY))));
SearchResponse sr = response.actionGet();
SearchHits sh = sr.getHits();
assertThat(sh.getTotalHits(), equalTo((long) (2)));
assertThat(sh.getAt(0).getId(), isOneOf("1"));
assertThat(sh.getAt(1).getId(), equalTo("2"));
// Test Exp
response = client().search(searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(searchSource().query(functionScoreQuery(termQuery("test", "value"), gaussDecayFunction("loc", lonlat, "1000km")).boostMode(CombineFunction.REPLACE))));
sr = response.actionGet();
sh = sr.getHits();
assertThat(sh.getTotalHits(), equalTo((long) (2)));
assertThat(sh.getAt(0).getId(), equalTo("2"));
assertThat(sh.getAt(1).getId(), equalTo("1"));
}
use of org.elasticsearch.action.index.IndexRequestBuilder in project elasticsearch by elastic.
the class DecayFunctionScoreIT method testDistanceScoreGeoLinGaussExpWithOffset.
public void testDistanceScoreGeoLinGaussExpWithOffset() throws Exception {
assertAcked(prepareCreate("test").addMapping("type1", jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("test").field("type", "text").endObject().startObject("num").field("type", "double").endObject().endObject().endObject().endObject()));
// add tw docs within offset
List<IndexRequestBuilder> indexBuilders = new ArrayList<>();
indexBuilders.add(client().prepareIndex().setType("type1").setId("1").setIndex("test").setSource(jsonBuilder().startObject().field("test", "value").field("num", 0.5).endObject()));
indexBuilders.add(client().prepareIndex().setType("type1").setId("2").setIndex("test").setSource(jsonBuilder().startObject().field("test", "value").field("num", 1.7).endObject()));
// add docs outside offset
int numDummyDocs = 20;
for (int i = 0; i < numDummyDocs; i++) {
indexBuilders.add(client().prepareIndex().setType("type1").setId(Integer.toString(i + 3)).setIndex("test").setSource(jsonBuilder().startObject().field("test", "value").field("num", 3.0 + i).endObject()));
}
indexRandom(true, indexBuilders);
// Test Gauss
ActionFuture<SearchResponse> response = client().search(searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(searchSource().size(numDummyDocs + 2).query(functionScoreQuery(termQuery("test", "value"), gaussDecayFunction("num", 1.0, 5.0, 1.0)).boostMode(CombineFunction.REPLACE))));
SearchResponse sr = response.actionGet();
SearchHits sh = sr.getHits();
assertThat(sh.getTotalHits(), equalTo((long) (numDummyDocs + 2)));
assertThat(sh.getAt(0).getId(), anyOf(equalTo("1"), equalTo("2")));
assertThat(sh.getAt(1).getId(), anyOf(equalTo("1"), equalTo("2")));
assertThat(sh.getAt(1).getScore(), equalTo(sh.getAt(0).getScore()));
for (int i = 0; i < numDummyDocs; i++) {
assertThat(sh.getAt(i + 2).getId(), equalTo(Integer.toString(i + 3)));
}
// Test Exp
response = client().search(searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(searchSource().size(numDummyDocs + 2).query(functionScoreQuery(termQuery("test", "value"), exponentialDecayFunction("num", 1.0, 5.0, 1.0)).boostMode(CombineFunction.REPLACE))));
sr = response.actionGet();
sh = sr.getHits();
assertThat(sh.getTotalHits(), equalTo((long) (numDummyDocs + 2)));
assertThat(sh.getAt(0).getId(), anyOf(equalTo("1"), equalTo("2")));
assertThat(sh.getAt(1).getId(), anyOf(equalTo("1"), equalTo("2")));
assertThat(sh.getAt(1).getScore(), equalTo(sh.getAt(0).getScore()));
for (int i = 0; i < numDummyDocs; i++) {
assertThat(sh.getAt(i + 2).getId(), equalTo(Integer.toString(i + 3)));
}
// Test Lin
response = client().search(searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(searchSource().size(numDummyDocs + 2).query(functionScoreQuery(termQuery("test", "value"), linearDecayFunction("num", 1.0, 20.0, 1.0)).boostMode(CombineFunction.REPLACE))));
sr = response.actionGet();
sh = sr.getHits();
assertThat(sh.getTotalHits(), equalTo((long) (numDummyDocs + 2)));
assertThat(sh.getAt(0).getId(), anyOf(equalTo("1"), equalTo("2")));
assertThat(sh.getAt(1).getId(), anyOf(equalTo("1"), equalTo("2")));
assertThat(sh.getAt(1).getScore(), equalTo(sh.getAt(0).getScore()));
}
use of org.elasticsearch.action.index.IndexRequestBuilder in project elasticsearch by elastic.
the class ExplainableScriptIT method testNativeExplainScript.
public void testNativeExplainScript() throws InterruptedException, IOException, ExecutionException {
List<IndexRequestBuilder> indexRequests = new ArrayList<>();
for (int i = 0; i < 20; i++) {
indexRequests.add(client().prepareIndex("test", "type").setId(Integer.toString(i)).setSource(jsonBuilder().startObject().field("number_field", i).field("text", "text").endObject()));
}
indexRandom(true, true, indexRequests);
client().admin().indices().prepareRefresh().execute().actionGet();
ensureYellow();
SearchResponse response = client().search(searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(searchSource().explain(true).query(functionScoreQuery(termQuery("text", "text"), scriptFunction(new Script(ScriptType.INLINE, "native", "native_explainable_script", Collections.emptyMap()))).boostMode(CombineFunction.REPLACE)))).actionGet();
ElasticsearchAssertions.assertNoFailures(response);
SearchHits hits = response.getHits();
assertThat(hits.getTotalHits(), equalTo(20L));
int idCounter = 19;
for (SearchHit hit : hits.getHits()) {
assertThat(hit.getId(), equalTo(Integer.toString(idCounter)));
assertThat(hit.getExplanation().toString(), containsString(Double.toString(idCounter) + " = This script returned " + Double.toString(idCounter)));
assertThat(hit.getExplanation().toString(), containsString("freq=1.0 = termFreq=1.0"));
assertThat(hit.getExplanation().getDetails().length, equalTo(2));
idCounter--;
}
}
use of org.elasticsearch.action.index.IndexRequestBuilder in project elasticsearch by elastic.
the class SearchFieldsIT method testScriptFields.
public void testScriptFields() throws Exception {
assertAcked(prepareCreate("index").addMapping("type", "s", "type=keyword", "l", "type=long", "d", "type=double", "ms", "type=keyword", "ml", "type=long", "md", "type=double").get());
final int numDocs = randomIntBetween(3, 8);
List<IndexRequestBuilder> reqs = new ArrayList<>();
for (int i = 0; i < numDocs; ++i) {
reqs.add(client().prepareIndex("index", "type", Integer.toString(i)).setSource("s", Integer.toString(i), "ms", new String[] { Integer.toString(i), Integer.toString(i + 1) }, "l", i, "ml", new long[] { i, i + 1 }, "d", i, "md", new double[] { i, i + 1 }));
}
indexRandom(true, reqs);
ensureSearchable();
SearchRequestBuilder req = client().prepareSearch("index");
for (String field : Arrays.asList("s", "ms", "l", "ml", "d", "md")) {
req.addScriptField(field, new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "doc['" + field + "'].values", Collections.emptyMap()));
}
SearchResponse resp = req.get();
assertSearchResponse(resp);
for (SearchHit hit : resp.getHits().getHits()) {
final int id = Integer.parseInt(hit.getId());
Map<String, SearchHitField> fields = hit.getFields();
assertThat(fields.get("s").getValues(), equalTo(Collections.<Object>singletonList(Integer.toString(id))));
assertThat(fields.get("l").getValues(), equalTo(Collections.<Object>singletonList((long) id)));
assertThat(fields.get("d").getValues(), equalTo(Collections.<Object>singletonList((double) id)));
assertThat(fields.get("ms").getValues(), equalTo(Arrays.<Object>asList(Integer.toString(id), Integer.toString(id + 1))));
assertThat(fields.get("ml").getValues(), equalTo(Arrays.<Object>asList((long) id, id + 1L)));
assertThat(fields.get("md").getValues(), equalTo(Arrays.<Object>asList((double) id, id + 1d)));
}
}
use of org.elasticsearch.action.index.IndexRequestBuilder in project elasticsearch by elastic.
the class HighlighterSearchIT method testEscapeHtmlVector.
public void testEscapeHtmlVector() throws Exception {
assertAcked(prepareCreate("test").addMapping("type1", "title", "type=text,store=true,term_vector=with_positions_offsets"));
IndexRequestBuilder[] indexRequestBuilders = new IndexRequestBuilder[5];
for (int i = 0; i < 5; i++) {
indexRequestBuilders[i] = client().prepareIndex("test", "type1", Integer.toString(i)).setSource("title", "This is a html escaping highlighting test for *&? elasticsearch");
}
indexRandom(true, indexRequestBuilders);
SearchResponse search = client().prepareSearch().setQuery(matchQuery("title", "test")).highlighter(new HighlightBuilder().encoder("html").field("title", 30, 1, 10)).get();
for (int i = 0; i < 5; i++) {
assertHighlight(search, i, "title", 0, 1, equalTo("highlighting <em>test</em> for *&? elasticsearch"));
}
}
Aggregations