Search in sources :

Example 16 with HighlightBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder in project elasticsearch by elastic.

the class TopHitsIT method testTopHitsInNested.

public void testTopHitsInNested() throws Exception {
    SearchResponse searchResponse = client().prepareSearch("articles").addAggregation(histogram("dates").field("date").interval(5).order(Histogram.Order.aggregation("to-comments", true)).subAggregation(nested("to-comments", "comments").subAggregation(topHits("comments").highlighter(new HighlightBuilder().field(new HighlightBuilder.Field("comments.message").highlightQuery(matchQuery("comments.message", "text")))).sort("comments.id", SortOrder.ASC)))).get();
    Histogram histogram = searchResponse.getAggregations().get("dates");
    for (int i = 0; i < numArticles; i += 5) {
        Histogram.Bucket bucket = histogram.getBuckets().get(i / 5);
        assertThat(bucket.getDocCount(), equalTo(5L));
        long numNestedDocs = 10 + (5 * i);
        Nested nested = bucket.getAggregations().get("to-comments");
        assertThat(nested.getDocCount(), equalTo(numNestedDocs));
        TopHits hits = nested.getAggregations().get("comments");
        SearchHits searchHits = hits.getHits();
        assertThat(searchHits.getTotalHits(), equalTo(numNestedDocs));
        for (int j = 0; j < 3; j++) {
            assertThat(searchHits.getAt(j).getNestedIdentity().getField().string(), equalTo("comments"));
            assertThat(searchHits.getAt(j).getNestedIdentity().getOffset(), equalTo(0));
            assertThat((Integer) searchHits.getAt(j).getSourceAsMap().get("id"), equalTo(0));
            HighlightField highlightField = searchHits.getAt(j).getHighlightFields().get("comments.message");
            assertThat(highlightField.getFragments().length, equalTo(1));
            assertThat(highlightField.getFragments()[0].string(), equalTo("some <em>text</em>"));
        }
    }
}
Also used : SearchHitField(org.elasticsearch.search.SearchHitField) HighlightField(org.elasticsearch.search.fetch.subphase.highlight.HighlightField) Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) TopHits(org.elasticsearch.search.aggregations.metrics.tophits.TopHits) Nested(org.elasticsearch.search.aggregations.bucket.nested.Nested) HighlightField(org.elasticsearch.search.fetch.subphase.highlight.HighlightField) SearchHits(org.elasticsearch.search.SearchHits) HighlightBuilder(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 17 with HighlightBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder in project elasticsearch by elastic.

the class TopHitsIT method testFetchFeatures.

public void testFetchFeatures() {
    SearchResponse response = client().prepareSearch("idx").setTypes("type").setQuery(matchQuery("text", "text").queryName("test")).addAggregation(terms("terms").executionHint(randomExecutionHint()).field(TERMS_AGGS_FIELD).subAggregation(topHits("hits").size(1).highlighter(new HighlightBuilder().field("text")).explain(true).storedField("text").fieldDataField("field1").scriptField("script", new Script(ScriptType.INLINE, MockScriptEngine.NAME, "5", Collections.emptyMap())).fetchSource("text", null).version(true))).get();
    assertSearchResponse(response);
    Terms terms = response.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    assertThat(terms.getName(), equalTo("terms"));
    assertThat(terms.getBuckets().size(), equalTo(5));
    for (Terms.Bucket bucket : terms.getBuckets()) {
        TopHits topHits = bucket.getAggregations().get("hits");
        SearchHits hits = topHits.getHits();
        assertThat(hits.getTotalHits(), equalTo(10L));
        assertThat(hits.getHits().length, equalTo(1));
        SearchHit hit = hits.getAt(0);
        HighlightField highlightField = hit.getHighlightFields().get("text");
        assertThat(highlightField.getFragments().length, equalTo(1));
        assertThat(highlightField.getFragments()[0].string(), equalTo("some <em>text</em> to entertain"));
        Explanation explanation = hit.getExplanation();
        assertThat(explanation.toString(), containsString("text:text"));
        long version = hit.getVersion();
        assertThat(version, equalTo(1L));
        assertThat(hit.getMatchedQueries()[0], equalTo("test"));
        SearchHitField field = hit.field("field1");
        assertThat(field.getValue().toString(), equalTo("5"));
        assertThat(hit.getSourceAsMap().get("text").toString(), equalTo("some text to entertain"));
        field = hit.field("script");
        assertThat(field.getValue().toString(), equalTo("5"));
        assertThat(hit.getSourceAsMap().size(), equalTo(1));
        assertThat(hit.getSourceAsMap().get("text").toString(), equalTo("some text to entertain"));
    }
}
Also used : Script(org.elasticsearch.script.Script) TopHits(org.elasticsearch.search.aggregations.metrics.tophits.TopHits) SearchHit(org.elasticsearch.search.SearchHit) Explanation(org.apache.lucene.search.Explanation) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) HighlightField(org.elasticsearch.search.fetch.subphase.highlight.HighlightField) SearchHitField(org.elasticsearch.search.SearchHitField) SearchHits(org.elasticsearch.search.SearchHits) HighlightBuilder(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 18 with HighlightBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder in project elasticsearch by elastic.

the class SearchStatsIT method testSimpleStats.

public void testSimpleStats() throws Exception {
    // clear all stats first
    client().admin().indices().prepareStats().clear().execute().actionGet();
    final int numNodes = cluster().numDataNodes();
    assertThat(numNodes, greaterThanOrEqualTo(2));
    // we make sure each node gets at least a single shard...
    final int shardsIdx1 = randomIntBetween(1, 10);
    final int shardsIdx2 = Math.max(numNodes - shardsIdx1, randomIntBetween(1, 10));
    assertThat(numNodes, lessThanOrEqualTo(shardsIdx1 + shardsIdx2));
    assertAcked(prepareCreate("test1").setSettings(Settings.builder().put(SETTING_NUMBER_OF_SHARDS, shardsIdx1).put(SETTING_NUMBER_OF_REPLICAS, 0)));
    int docsTest1 = scaledRandomIntBetween(3 * shardsIdx1, 5 * shardsIdx1);
    for (int i = 0; i < docsTest1; i++) {
        client().prepareIndex("test1", "type", Integer.toString(i)).setSource("field", "value").execute().actionGet();
        if (rarely()) {
            refresh();
        }
    }
    assertAcked(prepareCreate("test2").setSettings(Settings.builder().put(SETTING_NUMBER_OF_SHARDS, shardsIdx2).put(SETTING_NUMBER_OF_REPLICAS, 0)));
    int docsTest2 = scaledRandomIntBetween(3 * shardsIdx2, 5 * shardsIdx2);
    for (int i = 0; i < docsTest2; i++) {
        client().prepareIndex("test2", "type", Integer.toString(i)).setSource("field", "value").execute().actionGet();
        if (rarely()) {
            refresh();
        }
    }
    assertThat(shardsIdx1 + shardsIdx2, equalTo(numAssignedShards("test1", "test2")));
    assertThat(numAssignedShards("test1", "test2"), greaterThanOrEqualTo(2));
    // THERE WILL BE AT LEAST 2 NODES HERE SO WE CAN WAIT FOR GREEN
    ensureGreen();
    refresh();
    int iters = scaledRandomIntBetween(100, 150);
    for (int i = 0; i < iters; i++) {
        SearchResponse searchResponse = internalCluster().coordOnlyNodeClient().prepareSearch().setQuery(QueryBuilders.termQuery("field", "value")).setStats("group1", "group2").highlighter(new HighlightBuilder().field("field")).addScriptField("script1", new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "_source.field", Collections.emptyMap())).setSize(100).execute().actionGet();
        assertHitCount(searchResponse, docsTest1 + docsTest2);
        assertAllSuccessful(searchResponse);
    }
    IndicesStatsResponse indicesStats = client().admin().indices().prepareStats().execute().actionGet();
    logger.debug("###### indices search stats: {}", indicesStats.getTotal().getSearch());
    assertThat(indicesStats.getTotal().getSearch().getTotal().getQueryCount(), greaterThan(0L));
    assertThat(indicesStats.getTotal().getSearch().getTotal().getQueryTimeInMillis(), greaterThan(0L));
    assertThat(indicesStats.getTotal().getSearch().getTotal().getFetchCount(), greaterThan(0L));
    assertThat(indicesStats.getTotal().getSearch().getTotal().getFetchTimeInMillis(), greaterThan(0L));
    assertThat(indicesStats.getTotal().getSearch().getGroupStats(), nullValue());
    indicesStats = client().admin().indices().prepareStats().setGroups("group1").execute().actionGet();
    assertThat(indicesStats.getTotal().getSearch().getGroupStats(), notNullValue());
    assertThat(indicesStats.getTotal().getSearch().getGroupStats().get("group1").getQueryCount(), greaterThan(0L));
    assertThat(indicesStats.getTotal().getSearch().getGroupStats().get("group1").getQueryTimeInMillis(), greaterThan(0L));
    assertThat(indicesStats.getTotal().getSearch().getGroupStats().get("group1").getFetchCount(), greaterThan(0L));
    assertThat(indicesStats.getTotal().getSearch().getGroupStats().get("group1").getFetchTimeInMillis(), greaterThan(0L));
    NodesStatsResponse nodeStats = client().admin().cluster().prepareNodesStats().execute().actionGet();
    Set<String> nodeIdsWithIndex = nodeIdsWithIndex("test1", "test2");
    int num = 0;
    for (NodeStats stat : nodeStats.getNodes()) {
        Stats total = stat.getIndices().getSearch().getTotal();
        if (nodeIdsWithIndex.contains(stat.getNode().getId())) {
            assertThat(total.getQueryCount(), greaterThan(0L));
            assertThat(total.getQueryTimeInMillis(), greaterThan(0L));
            num++;
        } else {
            assertThat(total.getQueryCount(), equalTo(0L));
            assertThat(total.getQueryTimeInMillis(), equalTo(0L));
        }
    }
    assertThat(num, greaterThan(0));
}
Also used : NodesStatsResponse(org.elasticsearch.action.admin.cluster.node.stats.NodesStatsResponse) Script(org.elasticsearch.script.Script) IndicesStatsResponse(org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse) NodeStats(org.elasticsearch.action.admin.cluster.node.stats.NodeStats) NodeStats(org.elasticsearch.action.admin.cluster.node.stats.NodeStats) Stats(org.elasticsearch.index.search.stats.SearchStats.Stats) HighlightBuilder(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 19 with HighlightBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder in project elasticsearch by elastic.

the class InnerHitsIT method testSimpleNested.

public void testSimpleNested() throws Exception {
    assertAcked(prepareCreate("articles").addMapping("article", jsonBuilder().startObject().startObject("article").startObject("properties").startObject("comments").field("type", "nested").startObject("properties").startObject("message").field("type", "text").field("fielddata", true).endObject().endObject().endObject().startObject("title").field("type", "text").endObject().endObject().endObject().endObject()));
    List<IndexRequestBuilder> requests = new ArrayList<>();
    requests.add(client().prepareIndex("articles", "article", "1").setSource(jsonBuilder().startObject().field("title", "quick brown fox").startArray("comments").startObject().field("message", "fox eat quick").endObject().startObject().field("message", "fox ate rabbit x y z").endObject().startObject().field("message", "rabbit got away").endObject().endArray().endObject()));
    requests.add(client().prepareIndex("articles", "article", "2").setSource(jsonBuilder().startObject().field("title", "big gray elephant").startArray("comments").startObject().field("message", "elephant captured").endObject().startObject().field("message", "mice squashed by elephant x").endObject().startObject().field("message", "elephant scared by mice x y").endObject().endArray().endObject()));
    indexRandom(true, requests);
    SearchResponse response = client().prepareSearch("articles").setQuery(nestedQuery("comments", matchQuery("comments.message", "fox"), ScoreMode.Avg).innerHit(new InnerHitBuilder().setName("comment"), false)).get();
    assertNoFailures(response);
    assertHitCount(response, 1);
    assertSearchHit(response, 1, hasId("1"));
    assertThat(response.getHits().getAt(0).getInnerHits().size(), equalTo(1));
    SearchHits innerHits = response.getHits().getAt(0).getInnerHits().get("comment");
    assertThat(innerHits.getTotalHits(), equalTo(2L));
    assertThat(innerHits.getHits().length, equalTo(2));
    assertThat(innerHits.getAt(0).getId(), equalTo("1"));
    assertThat(innerHits.getAt(0).getNestedIdentity().getField().string(), equalTo("comments"));
    assertThat(innerHits.getAt(0).getNestedIdentity().getOffset(), equalTo(0));
    assertThat(innerHits.getAt(1).getId(), equalTo("1"));
    assertThat(innerHits.getAt(1).getNestedIdentity().getField().string(), equalTo("comments"));
    assertThat(innerHits.getAt(1).getNestedIdentity().getOffset(), equalTo(1));
    response = client().prepareSearch("articles").setQuery(nestedQuery("comments", matchQuery("comments.message", "elephant"), ScoreMode.Avg).innerHit(new InnerHitBuilder().setName("comment"), false)).get();
    assertNoFailures(response);
    assertHitCount(response, 1);
    assertSearchHit(response, 1, hasId("2"));
    assertThat(response.getHits().getAt(0).getShard(), notNullValue());
    assertThat(response.getHits().getAt(0).getInnerHits().size(), equalTo(1));
    innerHits = response.getHits().getAt(0).getInnerHits().get("comment");
    assertThat(innerHits.getTotalHits(), equalTo(3L));
    assertThat(innerHits.getHits().length, equalTo(3));
    assertThat(innerHits.getAt(0).getId(), equalTo("2"));
    assertThat(innerHits.getAt(0).getNestedIdentity().getField().string(), equalTo("comments"));
    assertThat(innerHits.getAt(0).getNestedIdentity().getOffset(), equalTo(0));
    assertThat(innerHits.getAt(1).getId(), equalTo("2"));
    assertThat(innerHits.getAt(1).getNestedIdentity().getField().string(), equalTo("comments"));
    assertThat(innerHits.getAt(1).getNestedIdentity().getOffset(), equalTo(1));
    assertThat(innerHits.getAt(2).getId(), equalTo("2"));
    assertThat(innerHits.getAt(2).getNestedIdentity().getField().string(), equalTo("comments"));
    assertThat(innerHits.getAt(2).getNestedIdentity().getOffset(), equalTo(2));
    response = client().prepareSearch("articles").setQuery(nestedQuery("comments", matchQuery("comments.message", "fox"), ScoreMode.Avg).innerHit(new InnerHitBuilder().setHighlightBuilder(new HighlightBuilder().field("comments.message")).setExplain(true).addDocValueField("comments.message").addScriptField("script", new Script(ScriptType.INLINE, MockScriptEngine.NAME, "5", Collections.emptyMap())).setSize(1), false)).get();
    assertNoFailures(response);
    innerHits = response.getHits().getAt(0).getInnerHits().get("comments");
    assertThat(innerHits.getTotalHits(), equalTo(2L));
    assertThat(innerHits.getHits().length, equalTo(1));
    assertThat(innerHits.getAt(0).getHighlightFields().get("comments.message").getFragments()[0].string(), equalTo("<em>fox</em> eat quick"));
    assertThat(innerHits.getAt(0).getExplanation().toString(), containsString("weight(comments.message:fox in"));
    assertThat(innerHits.getAt(0).getFields().get("comments.message").getValue().toString(), equalTo("eat"));
    assertThat(innerHits.getAt(0).getFields().get("script").getValue().toString(), equalTo("5"));
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) Script(org.elasticsearch.script.Script) ArrayList(java.util.ArrayList) InnerHitBuilder(org.elasticsearch.index.query.InnerHitBuilder) SearchHits(org.elasticsearch.search.SearchHits) HighlightBuilder(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 20 with HighlightBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder in project elasticsearch by elastic.

the class CustomHighlighterSearchIT method testThatCustomHighlighterCanBeConfiguredPerField.

public void testThatCustomHighlighterCanBeConfiguredPerField() throws Exception {
    HighlightBuilder.Field highlightConfig = new HighlightBuilder.Field("name");
    highlightConfig.highlighterType("test-custom");
    Map<String, Object> options = new HashMap<>();
    options.put("myFieldOption", "someValue");
    highlightConfig.options(options);
    SearchResponse searchResponse = client().prepareSearch("test").setTypes("test").setQuery(QueryBuilders.matchAllQuery()).highlighter(new HighlightBuilder().field(highlightConfig)).execute().actionGet();
    assertHighlight(searchResponse, 0, "name", 0, equalTo("standard response for name at position 1"));
    assertHighlight(searchResponse, 0, "name", 1, equalTo("field:myFieldOption:someValue"));
}
Also used : HashMap(java.util.HashMap) HighlightBuilder(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Aggregations

HighlightBuilder (org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder)23 SearchResponse (org.elasticsearch.action.search.SearchResponse)16 SearchHits (org.elasticsearch.search.SearchHits)7 ArrayList (java.util.ArrayList)5 Script (org.elasticsearch.script.Script)5 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)5 Map (java.util.Map)4 InnerHitBuilder (org.elasticsearch.index.query.InnerHitBuilder)4 SearchHit (org.elasticsearch.search.SearchHit)4 HighlightField (org.elasticsearch.search.fetch.subphase.highlight.HighlightField)4 HashMap (java.util.HashMap)3 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)3 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)3 SearchHitField (org.elasticsearch.search.SearchHitField)3 IOException (java.io.IOException)2 Date (java.util.Date)2 List (java.util.List)2 ServiceResponse (net.yacy.grid.http.ServiceResponse)2 ElasticsearchClient (net.yacy.grid.io.index.ElasticsearchClient)2 Sort (net.yacy.grid.io.index.Sort)2