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()));
}
}
use of org.elasticsearch.search.SearchHit in project elasticsearch by elastic.
the class InnerHitsIT method testRandomNested.
public void testRandomNested() throws Exception {
assertAcked(prepareCreate("idx").addMapping("type", "field1", "type=nested", "field2", "type=nested"));
int numDocs = scaledRandomIntBetween(25, 100);
List<IndexRequestBuilder> requestBuilders = new ArrayList<>();
int[] field1InnerObjects = new int[numDocs];
int[] field2InnerObjects = new int[numDocs];
for (int i = 0; i < numDocs; i++) {
int numInnerObjects = field1InnerObjects[i] = scaledRandomIntBetween(1, numDocs);
XContentBuilder source = jsonBuilder().startObject().startArray("field1");
for (int j = 0; j < numInnerObjects; j++) {
source.startObject().field("x", "y").endObject();
}
numInnerObjects = field2InnerObjects[i] = scaledRandomIntBetween(1, numDocs);
source.endArray().startArray("field2");
for (int j = 0; j < numInnerObjects; j++) {
source.startObject().field("x", "y").endObject();
}
source.endArray().endObject();
requestBuilders.add(client().prepareIndex("idx", "type", String.format(Locale.ENGLISH, "%03d", i)).setSource(source));
}
indexRandom(true, requestBuilders);
int size = randomIntBetween(0, numDocs);
BoolQueryBuilder boolQuery = new BoolQueryBuilder();
boolQuery.should(nestedQuery("field1", matchAllQuery(), ScoreMode.Avg).innerHit(new InnerHitBuilder().setName("a").setSize(size).addSort(new FieldSortBuilder("_doc").order(SortOrder.DESC)), false));
boolQuery.should(nestedQuery("field2", matchAllQuery(), ScoreMode.Avg).innerHit(new InnerHitBuilder().setName("b").addSort(new FieldSortBuilder("_doc").order(SortOrder.DESC)).setSize(size), false));
SearchResponse searchResponse = client().prepareSearch("idx").setQuery(boolQuery).setSize(numDocs).addSort("_uid", SortOrder.ASC).get();
assertNoFailures(searchResponse);
assertHitCount(searchResponse, numDocs);
assertThat(searchResponse.getHits().getHits().length, equalTo(numDocs));
for (int i = 0; i < numDocs; i++) {
SearchHit searchHit = searchResponse.getHits().getAt(i);
assertThat(searchHit.getShard(), notNullValue());
SearchHits inner = searchHit.getInnerHits().get("a");
assertThat(inner.getTotalHits(), equalTo((long) field1InnerObjects[i]));
for (int j = 0; j < field1InnerObjects[i] && j < size; j++) {
SearchHit innerHit = inner.getAt(j);
assertThat(innerHit.getNestedIdentity().getField().string(), equalTo("field1"));
assertThat(innerHit.getNestedIdentity().getOffset(), equalTo(j));
assertThat(innerHit.getNestedIdentity().getChild(), nullValue());
}
inner = searchHit.getInnerHits().get("b");
assertThat(inner.getTotalHits(), equalTo((long) field2InnerObjects[i]));
for (int j = 0; j < field2InnerObjects[i] && j < size; j++) {
SearchHit innerHit = inner.getAt(j);
assertThat(innerHit.getNestedIdentity().getField().string(), equalTo("field2"));
assertThat(innerHit.getNestedIdentity().getOffset(), equalTo(j));
assertThat(innerHit.getNestedIdentity().getChild(), nullValue());
}
}
}
use of org.elasticsearch.search.SearchHit in project elasticsearch by elastic.
the class MatchedQueriesIT method testFuzzyQuerySupportsName.
public void testFuzzyQuerySupportsName() {
createIndex("test1");
ensureGreen();
client().prepareIndex("test1", "type1", "1").setSource("title", "title1").get();
refresh();
SearchResponse searchResponse = client().prepareSearch().setQuery(QueryBuilders.fuzzyQuery("title", "titel1").queryName("fuzzy")).get();
assertHitCount(searchResponse, 1L);
for (SearchHit hit : searchResponse.getHits()) {
if (hit.getId().equals("1")) {
assertThat(hit.getMatchedQueries().length, equalTo(1));
assertThat(hit.getMatchedQueries(), hasItemInArray("fuzzy"));
} else {
fail("Unexpected document returned with id " + hit.getId());
}
}
}
use of org.elasticsearch.search.SearchHit in project elasticsearch by elastic.
the class MatchedQueriesIT method testRegExpQuerySupportsName.
public void testRegExpQuerySupportsName() {
createIndex("test1");
ensureGreen();
client().prepareIndex("test1", "type1", "1").setSource("title", "title1").get();
refresh();
SearchResponse searchResponse = client().prepareSearch().setQuery(QueryBuilders.regexpQuery("title", "title1").queryName("regex")).get();
assertHitCount(searchResponse, 1L);
for (SearchHit hit : searchResponse.getHits()) {
if (hit.getId().equals("1")) {
assertThat(hit.getMatchedQueries().length, equalTo(1));
assertThat(hit.getMatchedQueries(), hasItemInArray("regex"));
} else {
fail("Unexpected document returned with id " + hit.getId());
}
}
}
use of org.elasticsearch.search.SearchHit in project elasticsearch by elastic.
the class MatchedQueriesIT method testPrefixQuerySupportsName.
public void testPrefixQuerySupportsName() {
createIndex("test1");
ensureGreen();
client().prepareIndex("test1", "type1", "1").setSource("title", "title1").get();
refresh();
SearchResponse searchResponse = client().prepareSearch().setQuery(QueryBuilders.prefixQuery("title", "title").queryName("prefix")).get();
assertHitCount(searchResponse, 1L);
for (SearchHit hit : searchResponse.getHits()) {
if (hit.getId().equals("1")) {
assertThat(hit.getMatchedQueries().length, equalTo(1));
assertThat(hit.getMatchedQueries(), hasItemInArray("prefix"));
} else {
fail("Unexpected document returned with id " + hit.getId());
}
}
}
Aggregations