use of org.elasticsearch.search.SearchHit in project elasticsearch by elastic.
the class SearchScrollIT method testCloseAndReopenOrDeleteWithActiveScroll.
public void testCloseAndReopenOrDeleteWithActiveScroll() throws IOException {
createIndex("test");
for (int i = 0; i < 100; i++) {
client().prepareIndex("test", "type1", Integer.toString(i)).setSource(jsonBuilder().startObject().field("field", i).endObject()).execute().actionGet();
}
refresh();
SearchResponse searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setSize(35).setScroll(TimeValue.timeValueMinutes(2)).addSort("field", SortOrder.ASC).execute().actionGet();
long counter = 0;
assertThat(searchResponse.getHits().getTotalHits(), equalTo(100L));
assertThat(searchResponse.getHits().getHits().length, equalTo(35));
for (SearchHit hit : searchResponse.getHits()) {
assertThat(((Number) hit.getSortValues()[0]).longValue(), equalTo(counter++));
}
if (randomBoolean()) {
client().admin().indices().prepareClose("test").get();
client().admin().indices().prepareOpen("test").get();
ensureGreen("test");
} else {
client().admin().indices().prepareDelete("test").get();
}
}
use of org.elasticsearch.search.SearchHit in project jena by apache.
the class TextIndexES method query.
/**
* Query the ElasticSearch for the given Node, with the given query String and limit.
* @param property the node property to make a search for
* @param qs the query string
* @param limit limit on the number of records to return
* @return List of {@link TextHit}s containing the documents that have been found
*/
@Override
public List<TextHit> query(Node property, String qs, String graphURI, String lang, int limit) {
if (property != null) {
qs = parse(property.getLocalName(), qs, lang);
} else {
qs = parse(null, qs, lang);
}
LOGGER.debug("Querying ElasticSearch for QueryString: " + qs);
SearchResponse response = client.prepareSearch(indexName).setTypes(docDef.getEntityField()).setQuery(QueryBuilders.queryStringQuery(qs)).setFetchSource(false).setFrom(0).setSize(limit).get();
List<TextHit> results = new ArrayList<>();
for (SearchHit hit : response.getHits()) {
//It has been decided to return NULL literal values for now.
String entityField = hit.getId();
Node entityNode = TextQueryFuncs.stringToNode(entityField);
Float score = hit.getScore();
TextHit textHit = new TextHit(entityNode, score, null);
results.add(textHit);
}
return results;
}
use of org.elasticsearch.search.SearchHit in project pyramid by cheng-li.
the class ESIndex method getDocs.
/**
* use as an inverted index
* no score is computed
* @param term stemmed term
* @param ids
* @return
* @throws Exception
*/
public List<String> getDocs(String term, String[] ids) throws Exception {
StopWatch stopWatch = null;
if (logger.isDebugEnabled()) {
stopWatch = new StopWatch();
stopWatch.start();
}
/**
* setSize() has a huge impact on performance, the smaller the faster
*/
//todo reuse idsFilterBuilder
IdsFilterBuilder idsFilterBuilder = new IdsFilterBuilder(documentType);
idsFilterBuilder.addIds(ids);
TermFilterBuilder termFilterBuilder = new TermFilterBuilder(this.bodyField, term);
SearchResponse response = client.prepareSearch(indexName).setSize(ids.length).setHighlighterFilter(false).setTrackScores(false).setNoFields().setExplain(false).setFetchSource(false).setQuery(QueryBuilders.constantScoreQuery(FilterBuilders.andFilter(termFilterBuilder, idsFilterBuilder))).execute().actionGet();
List<String> list = new ArrayList<>(response.getHits().getHits().length);
for (SearchHit searchHit : response.getHits()) {
list.add(searchHit.getId());
}
if (logger.isDebugEnabled()) {
logger.debug("time spent on termFilter() for " + term + " = " + stopWatch + " There are " + list.size() + " matched docs");
}
return list;
}
use of org.elasticsearch.search.SearchHit in project pyramid by cheng-li.
the class ESIndex method getAllDocs.
public List<String> getAllDocs() {
SearchResponse response = client.prepareSearch(indexName).setSize(this.numDocs).setHighlighterFilter(false).setTrackScores(false).setNoFields().setExplain(false).setFetchSource(false).setQuery(QueryBuilders.matchAllQuery()).execute().actionGet();
List<String> list = new ArrayList<>(response.getHits().getHits().length);
for (SearchHit searchHit : response.getHits()) {
list.add(searchHit.getId());
}
return list;
}
use of org.elasticsearch.search.SearchHit in project pyramid by cheng-li.
the class ESIndex method termFilter.
/**
* use as an inverted index
* no score is computed
* @param term stemmed term
* @return
* @throws Exception
*/
public List<String> termFilter(String field, String term) throws Exception {
StopWatch stopWatch = null;
if (logger.isDebugEnabled()) {
stopWatch = new StopWatch();
stopWatch.start();
}
/**
* setSize() has a huge impact on performance, the smaller the faster
*/
TermFilterBuilder termFilterBuilder = new TermFilterBuilder(field, term);
SearchResponse response = client.prepareSearch(indexName).setSize(this.numDocs).setHighlighterFilter(false).setTrackScores(false).setNoFields().setExplain(false).setFetchSource(false).setQuery(QueryBuilders.constantScoreQuery(termFilterBuilder)).execute().actionGet();
List<String> list = new ArrayList<>(response.getHits().getHits().length);
for (SearchHit searchHit : response.getHits()) {
list.add(searchHit.getId());
}
if (logger.isDebugEnabled()) {
logger.debug("time spent on termFilter() for " + term + " = " + stopWatch + " There are " + list.size() + " matched docs");
}
return list;
}
Aggregations