Search in sources :

Example 66 with IndexRequestBuilder

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"));
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) ArrayList(java.util.ArrayList) SearchHits(org.elasticsearch.search.SearchHits) ElasticsearchAssertions.assertOrderedSearchHits(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertOrderedSearchHits) ElasticsearchAssertions.assertSearchHits(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHits) Settings(org.elasticsearch.common.settings.Settings) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 67 with IndexRequestBuilder

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()));
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) ArrayList(java.util.ArrayList) SearchHits(org.elasticsearch.search.SearchHits) ElasticsearchAssertions.assertOrderedSearchHits(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertOrderedSearchHits) ElasticsearchAssertions.assertSearchHits(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHits) GeoPoint(org.elasticsearch.common.geo.GeoPoint) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 68 with IndexRequestBuilder

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--;
    }
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) ExplainableSearchScript(org.elasticsearch.script.ExplainableSearchScript) Script(org.elasticsearch.script.Script) AbstractDoubleSearchScript(org.elasticsearch.script.AbstractDoubleSearchScript) ExecutableScript(org.elasticsearch.script.ExecutableScript) SearchHit(org.elasticsearch.search.SearchHit) ArrayList(java.util.ArrayList) SearchHits(org.elasticsearch.search.SearchHits) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 69 with IndexRequestBuilder

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)));
    }
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) Script(org.elasticsearch.script.Script) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) ArrayList(java.util.ArrayList) SearchHitField(org.elasticsearch.search.SearchHitField) Matchers.containsString(org.hamcrest.Matchers.containsString) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 70 with IndexRequestBuilder

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 *&amp;? elasticsearch"));
    }
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) GeoPoint(org.elasticsearch.common.geo.GeoPoint) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Aggregations

IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)227 ArrayList (java.util.ArrayList)134 SearchResponse (org.elasticsearch.action.search.SearchResponse)125 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)48 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)45 Matchers.containsString (org.hamcrest.Matchers.containsString)38 GeoPoint (org.elasticsearch.common.geo.GeoPoint)36 CreateIndexRequestBuilder (org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder)31 CompletionSuggestionBuilder (org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder)23 Settings (org.elasticsearch.common.settings.Settings)21 IOException (java.io.IOException)19 BulkRequestBuilder (org.elasticsearch.action.bulk.BulkRequestBuilder)19 Client (org.elasticsearch.client.Client)18 SearchHit (org.elasticsearch.search.SearchHit)17 LinkedHashMap (java.util.LinkedHashMap)16 SearchHits (org.elasticsearch.search.SearchHits)16 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)15 CompletionMappingBuilder (org.elasticsearch.search.suggest.CompletionSuggestSearchIT.CompletionMappingBuilder)15 CategoryContextMapping (org.elasticsearch.search.suggest.completion.context.CategoryContextMapping)15 ContextMapping (org.elasticsearch.search.suggest.completion.context.ContextMapping)15