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"))));
}
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)));
}
}
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");
}
}
}
}
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> ."));
}
}
use of org.elasticsearch.search.SearchHit in project elasticsearch by elastic.
the class HighlighterSearchIT method testFastVectorHighlighterManyDocs.
public void testFastVectorHighlighterManyDocs() throws Exception {
assertAcked(prepareCreate("test").addMapping("type1", type1TermVectorMapping()));
ensureGreen();
int COUNT = between(20, 100);
IndexRequestBuilder[] indexRequestBuilders = new IndexRequestBuilder[COUNT];
for (int i = 0; i < COUNT; i++) {
indexRequestBuilders[i] = client().prepareIndex("test", "type1", Integer.toString(i)).setSource("field1", "test " + i);
}
logger.info("--> indexing docs");
indexRandom(true, indexRequestBuilders);
logger.info("--> searching explicitly on field1 and highlighting on it");
SearchResponse searchResponse = client().prepareSearch().setSize(COUNT).setQuery(termQuery("field1", "test")).highlighter(new HighlightBuilder().field("field1", 100, 0)).get();
for (int i = 0; i < COUNT; i++) {
SearchHit hit = searchResponse.getHits().getHits()[i];
// LUCENE 3.1 UPGRADE: Caused adding the space at the end...
assertHighlight(searchResponse, i, "field1", 0, 1, equalTo("<em>test</em> " + hit.getId()));
}
}
Aggregations