use of org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder in project elasticsearch by elastic.
the class PercolatorQuerySearchIT method testPercolatorQueryWithHighlighting.
public void testPercolatorQueryWithHighlighting() throws Exception {
StringBuilder fieldMapping = new StringBuilder("type=text").append(",store=").append(randomBoolean());
if (randomBoolean()) {
fieldMapping.append(",term_vector=with_positions_offsets");
} else if (randomBoolean()) {
fieldMapping.append(",index_options=offsets");
}
createIndex("test", client().admin().indices().prepareCreate("test").addMapping("type", "field1", fieldMapping).addMapping("queries", "query", "type=percolator"));
client().prepareIndex("test", "queries", "1").setSource(jsonBuilder().startObject().field("query", matchQuery("field1", "brown fox")).endObject()).execute().actionGet();
client().prepareIndex("test", "queries", "2").setSource(jsonBuilder().startObject().field("query", matchQuery("field1", "lazy dog")).endObject()).execute().actionGet();
client().prepareIndex("test", "queries", "3").setSource(jsonBuilder().startObject().field("query", termQuery("field1", "jumps")).endObject()).execute().actionGet();
client().prepareIndex("test", "queries", "4").setSource(jsonBuilder().startObject().field("query", termQuery("field1", "dog")).endObject()).execute().actionGet();
client().prepareIndex("test", "queries", "5").setSource(jsonBuilder().startObject().field("query", termQuery("field1", "fox")).endObject()).execute().actionGet();
client().admin().indices().prepareRefresh().get();
BytesReference document = jsonBuilder().startObject().field("field1", "The quick brown fox jumps over the lazy dog").endObject().bytes();
SearchResponse searchResponse = client().prepareSearch().setQuery(new PercolateQueryBuilder("query", "type", document, XContentType.JSON)).highlighter(new HighlightBuilder().field("field1")).addSort("_uid", SortOrder.ASC).get();
assertHitCount(searchResponse, 5);
assertThat(searchResponse.getHits().getAt(0).getHighlightFields().get("field1").fragments()[0].string(), equalTo("The quick <em>brown</em> <em>fox</em> jumps over the lazy dog"));
assertThat(searchResponse.getHits().getAt(1).getHighlightFields().get("field1").fragments()[0].string(), equalTo("The quick brown fox jumps over the <em>lazy</em> <em>dog</em>"));
assertThat(searchResponse.getHits().getAt(2).getHighlightFields().get("field1").fragments()[0].string(), equalTo("The quick brown fox <em>jumps</em> over the lazy dog"));
assertThat(searchResponse.getHits().getAt(3).getHighlightFields().get("field1").fragments()[0].string(), equalTo("The quick brown fox jumps over the lazy <em>dog</em>"));
assertThat(searchResponse.getHits().getAt(4).getHighlightFields().get("field1").fragments()[0].string(), equalTo("The quick brown <em>fox</em> jumps over the lazy dog"));
}
use of org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder in project elasticsearch by elastic.
the class ExternalValuesMapperIntegrationIT method testHighlightingOnCustomString.
public void testHighlightingOnCustomString() throws Exception {
prepareCreate("test-idx").addMapping("type", XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("field").field("type", FakeStringFieldMapper.CONTENT_TYPE).endObject().endObject().endObject().endObject()).execute().get();
index("test-idx", "type", "1", XContentFactory.jsonBuilder().startObject().field("field", "Every day is exactly the same").endObject());
refresh();
SearchResponse response;
// test if the highlighting is excluded when we use wildcards
response = client().prepareSearch("test-idx").setQuery(QueryBuilders.matchQuery("field", "exactly the same")).highlighter(new HighlightBuilder().field("*")).execute().actionGet();
assertSearchResponse(response);
assertThat(response.getHits().getTotalHits(), equalTo(1L));
assertThat(response.getHits().getAt(0).getHighlightFields().size(), equalTo(0));
// make sure it is not excluded when we explicitly provide the fieldname
response = client().prepareSearch("test-idx").setQuery(QueryBuilders.matchQuery("field", "exactly the same")).highlighter(new HighlightBuilder().field("field")).execute().actionGet();
assertSearchResponse(response);
assertThat(response.getHits().getTotalHits(), equalTo(1L));
assertThat(response.getHits().getAt(0).getHighlightFields().size(), equalTo(1));
assertThat(response.getHits().getAt(0).getHighlightFields().get("field").fragments()[0].string(), equalTo("Every day is " + "<em>exactly</em> <em>the</em> <em>same</em>"));
// make sure it is not excluded when we explicitly provide the fieldname and a wildcard
response = client().prepareSearch("test-idx").setQuery(QueryBuilders.matchQuery("field", "exactly the same")).highlighter(new HighlightBuilder().field("*").field("field")).execute().actionGet();
assertSearchResponse(response);
assertThat(response.getHits().getTotalHits(), equalTo(1L));
assertThat(response.getHits().getAt(0).getHighlightFields().size(), equalTo(1));
assertThat(response.getHits().getAt(0).getHighlightFields().get("field").fragments()[0].string(), equalTo("Every day is " + "<em>exactly</em> <em>the</em> <em>same</em>"));
}
use of 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>"));
}
}
}
use of 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"));
}
}
use of 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));
}
Aggregations