Search in sources :

Example 11 with SearchHit

use of org.elasticsearch.search.SearchHit 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 12 with SearchHit

use of org.elasticsearch.search.SearchHit in project elasticsearch by elastic.

the class SearchFieldsIT method testStoredFieldsWithoutSource.

public void testStoredFieldsWithoutSource() throws Exception {
    createIndex("test");
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("_source").field("enabled", false).endObject().startObject("properties").startObject("byte_field").field("type", "byte").field("store", true).endObject().startObject("short_field").field("type", "short").field("store", true).endObject().startObject("integer_field").field("type", "integer").field("store", true).endObject().startObject("long_field").field("type", "long").field("store", true).endObject().startObject("float_field").field("type", "float").field("store", true).endObject().startObject("double_field").field("type", "double").field("store", true).endObject().startObject("date_field").field("type", "date").field("store", true).endObject().startObject("boolean_field").field("type", "boolean").field("store", true).endObject().startObject("binary_field").field("type", "binary").field("store", true).endObject().endObject().endObject().endObject().string();
    client().admin().indices().preparePutMapping().setType("type1").setSource(mapping, XContentType.JSON).execute().actionGet();
    client().prepareIndex("test", "type1", "1").setSource(jsonBuilder().startObject().field("byte_field", (byte) 1).field("short_field", (short) 2).field("integer_field", 3).field("long_field", 4L).field("float_field", 5.0f).field("double_field", 6.0d).field("date_field", Joda.forPattern("dateOptionalTime").printer().print(new DateTime(2012, 3, 22, 0, 0, DateTimeZone.UTC))).field("boolean_field", true).field("binary_field", Base64.getEncoder().encodeToString("testing text".getBytes("UTF-8"))).endObject()).execute().actionGet();
    client().admin().indices().prepareRefresh().execute().actionGet();
    SearchResponse searchResponse = client().prepareSearch().setQuery(matchAllQuery()).addStoredField("byte_field").addStoredField("short_field").addStoredField("integer_field").addStoredField("long_field").addStoredField("float_field").addStoredField("double_field").addStoredField("date_field").addStoredField("boolean_field").addStoredField("binary_field").execute().actionGet();
    assertThat(searchResponse.getHits().getTotalHits(), equalTo(1L));
    assertThat(searchResponse.getHits().getHits().length, equalTo(1));
    Set<String> fields = new HashSet<>(searchResponse.getHits().getAt(0).getFields().keySet());
    assertThat(fields, equalTo(newHashSet("byte_field", "short_field", "integer_field", "long_field", "float_field", "double_field", "date_field", "boolean_field", "binary_field")));
    SearchHit searchHit = searchResponse.getHits().getAt(0);
    assertThat(searchHit.getFields().get("byte_field").getValue().toString(), equalTo("1"));
    assertThat(searchHit.getFields().get("short_field").getValue().toString(), equalTo("2"));
    assertThat(searchHit.getFields().get("integer_field").getValue(), equalTo((Object) 3));
    assertThat(searchHit.getFields().get("long_field").getValue(), equalTo((Object) 4L));
    assertThat(searchHit.getFields().get("float_field").getValue(), equalTo((Object) 5.0f));
    assertThat(searchHit.getFields().get("double_field").getValue(), equalTo((Object) 6.0d));
    String dateTime = Joda.forPattern("dateOptionalTime").printer().print(new DateTime(2012, 3, 22, 0, 0, DateTimeZone.UTC));
    assertThat(searchHit.getFields().get("date_field").getValue(), equalTo((Object) dateTime));
    assertThat(searchHit.getFields().get("boolean_field").getValue(), equalTo((Object) Boolean.TRUE));
    assertThat(searchHit.getFields().get("binary_field").getValue(), equalTo(new BytesArray("testing text".getBytes("UTF8"))));
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) SearchHit(org.elasticsearch.search.SearchHit) Matchers.containsString(org.hamcrest.Matchers.containsString) DateTime(org.joda.time.DateTime) ReadableDateTime(org.joda.time.ReadableDateTime) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse) Sets.newHashSet(org.elasticsearch.common.util.set.Sets.newHashSet) HashSet(java.util.HashSet)

Example 13 with SearchHit

use of org.elasticsearch.search.SearchHit 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 14 with SearchHit

use of org.elasticsearch.search.SearchHit in project elasticsearch by elastic.

the class HighlighterSearchIT method testPostingsHighlighterNumberOfFragments.

public void testPostingsHighlighterNumberOfFragments() throws Exception {
    assertAcked(prepareCreate("test").addMapping("type1", type1PostingsffsetsMapping()));
    ensureGreen();
    client().prepareIndex("test", "type1", "1").setSource("field1", "The quick brown fox jumps over the lazy dog. The lazy red fox jumps over the quick dog. " + "The quick brown dog jumps over the lazy fox.", "field2", "The quick brown fox jumps over the lazy dog. The lazy red fox jumps over the quick dog. " + "The quick brown dog jumps over the lazy fox.").get();
    refresh();
    for (String type : UNIFIED_AND_NULL) {
        logger.info("--> highlighting and searching on field1");
        SearchSourceBuilder source = searchSource().query(termQuery("field1", "fox")).highlighter(highlight().field(new Field("field1").numOfFragments(5).preTags("<field1>").postTags("</field1>").highlighterType(type)));
        SearchResponse searchResponse = client().search(searchRequest("test").source(source)).actionGet();
        assertHighlight(searchResponse, 0, "field1", 0, equalTo("The quick brown <field1>fox</field1> jumps over the lazy dog."));
        assertHighlight(searchResponse, 0, "field1", 1, equalTo("The lazy red <field1>fox</field1> jumps over the quick dog."));
        assertHighlight(searchResponse, 0, "field1", 2, 3, equalTo("The quick brown dog jumps over the lazy <field1>fox</field1>."));
        client().prepareIndex("test", "type1", "2").setSource("field1", new String[] { "The quick brown fox jumps over the lazy dog. Second sentence not finished", "The lazy red fox jumps over the quick dog.", "The quick brown dog jumps over the lazy fox." }).get();
        refresh();
        source = searchSource().query(termQuery("field1", "fox")).highlighter(highlight().field(new Field("field1").numOfFragments(0).preTags("<field1>").postTags("</field1>").highlighterType(type)));
        searchResponse = client().search(searchRequest("test").source(source)).actionGet();
        assertHitCount(searchResponse, 2L);
        for (SearchHit searchHit : searchResponse.getHits()) {
            if ("1".equals(searchHit.getId())) {
                assertHighlight(searchHit, "field1", 0, 1, equalTo("The quick brown <field1>fox</field1> jumps over the lazy dog. " + "The lazy red <field1>fox</field1> jumps over the quick dog. " + "The quick brown dog jumps over the lazy <field1>fox</field1>."));
            } else if ("2".equals(searchHit.getId())) {
                assertHighlight(searchHit, "field1", 0, 3, equalTo("The quick brown <field1>fox</field1> jumps over the lazy dog. Second sentence not finished"));
                assertHighlight(searchHit, "field1", 1, 3, equalTo("The lazy red <field1>fox</field1> jumps over the quick dog."));
                assertHighlight(searchHit, "field1", 2, 3, equalTo("The quick brown dog jumps over the lazy <field1>fox</field1>."));
            } else {
                fail("Only hits with id 1 and 2 are returned");
            }
        }
    }
}
Also used : Field(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder.Field) SearchHit(org.elasticsearch.search.SearchHit) Matchers.containsString(org.hamcrest.Matchers.containsString) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 15 with SearchHit

use of org.elasticsearch.search.SearchHit in project elasticsearch by elastic.

the class HighlighterSearchIT method testPostingsHighlighterMultiMapperWithStore.

public void testPostingsHighlighterMultiMapperWithStore() throws Exception {
    assertAcked(prepareCreate("test").addMapping("type1", jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("title").field("type", "text").field("store", true).field("index_options", "offsets").field("analyzer", "classic").startObject("fields").startObject("key").field("type", "text").field("store", true).field("index_options", "offsets").field("analyzer", "whitespace").endObject().endObject().endObject().endObject().endObject().endObject()));
    ensureGreen();
    client().prepareIndex("test", "type1", "1").setSource("title", "this is a test . Second sentence.").get();
    refresh();
    for (String type : UNIFIED_AND_NULL) {
        // simple search on body with standard analyzer with a simple field query
        SearchResponse searchResponse = client().prepareSearch().setQuery(matchQuery("title", "This is a Test")).highlighter(new HighlightBuilder().field("title").highlighterType(type)).get();
        assertHitCount(searchResponse, 1L);
        SearchHit hit = searchResponse.getHits().getAt(0);
        //stopwords are not highlighted since not indexed
        assertHighlight(hit, "title", 0, 1, equalTo("this is a <em>test</em> ."));
        // search on title.key and highlight on title
        searchResponse = client().prepareSearch().setQuery(matchQuery("title.key", "this is a test")).highlighter(new HighlightBuilder().field("title.key")).get();
        assertHitCount(searchResponse, 1L);
        //stopwords are now highlighted since we used only whitespace analyzer here
        assertHighlight(searchResponse, 0, "title.key", 0, 1, equalTo("<em>this</em> <em>is</em> <em>a</em> <em>test</em> ."));
    }
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) Matchers.containsString(org.hamcrest.Matchers.containsString) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Aggregations

SearchHit (org.elasticsearch.search.SearchHit)160 SearchResponse (org.elasticsearch.action.search.SearchResponse)112 SearchHits (org.elasticsearch.search.SearchHits)49 ArrayList (java.util.ArrayList)27 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)25 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)20 IOException (java.io.IOException)18 Matchers.containsString (org.hamcrest.Matchers.containsString)17 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)16 HashMap (java.util.HashMap)14 ScoreDoc (org.apache.lucene.search.ScoreDoc)14 SearchHitField (org.elasticsearch.search.SearchHitField)14 HashSet (java.util.HashSet)12 Test (org.junit.Test)12 Map (java.util.Map)10 AtomicReference (java.util.concurrent.atomic.AtomicReference)10 TopDocs (org.apache.lucene.search.TopDocs)10 Text (org.elasticsearch.common.text.Text)10 InnerHitBuilder (org.elasticsearch.index.query.InnerHitBuilder)9 FetchSearchResult (org.elasticsearch.search.fetch.FetchSearchResult)9