use of org.elasticsearch.search.SearchHit in project pyramid by cheng-li.
the class ESIndexTest method test18.
static void test18() throws Exception {
ESIndex index = new ESIndex.Builder().setClientType("node").setIndexName("ohsumed_20000").build();
// String q = "{\"term\": {\"id\": 1}}";
// String q = "{" +
// " \"match_all\": {}" +
// " }";
// String q = "{" +
// " \"filtered\": {" +
// " \"query\": {" +
// " \"match_all\": {}" +
// " }," +
// " \"filter\": {" +
// " \"term\": {" +
// " \"split\": \"train\"" +
// " }" +
// " }" +
// " }" +
// " }";
String q = "{\n" + " \"bool\": {\n" + " \"should\": [\n" + " {\n" + " \"constant_score\": {\n" + " \"query\": {\n" + " \"match\": {\n" + " \"body\": \"repeated\"\n" + " }\n" + " }\n" + " }\n" + " },\n" + " {\n" + " \"constant_score\": {\n" + " \"query\": {\n" + " \"match\": {\n" + " \"body\": \"cyclophosphamide\"\n" + " }\n" + " }\n" + " }\n" + " },\n" + " {\n" + " \"constant_score\": {\n" + " \"query\": {\n" + " \"match\": {\n" + " \"body\": \"cycles\"\n" + " }\n" + " }\n" + " }\n" + " },\n" + " {\n" + " \"constant_score\": {\n" + " \"query\": {\n" + " \"match\": {\n" + " \"body\": \"study\"\n" + " }\n" + " }\n" + " }\n" + " }\n" + " ],\n" + " \"minimum_should_match\": \"70%\"\n" + " }\n" + " }";
SearchResponse response = index.submitQuery(q);
for (SearchHit searchHit : response.getHits()) {
System.out.println(searchHit.getId() + " " + searchHit.getScore());
}
index.close();
}
use of org.elasticsearch.search.SearchHit in project pyramid by cheng-li.
the class ESIndexTest method test21.
// static void test20() throws Exception{
// try(ESIndex index = new ESIndex.Builder().setClientType("node").setIndexName("ohsumed_20000")
// .build()){
// List<String> terms = new ArrayList<>();
// terms.add("repeated");
// terms.add("cyclophosphamide");
// terms.add("cycles");
// terms.add("study");
// String[] ids = {"AVYcLfPVDpWfZwAC_rp3", "AVYcLfbpDpWfZwAC_rt_"};
// SearchResponse response = index.minimumShouldMatch(terms, "body", 70, ids);
// System.out.println(response.getHits().getTotalHits());
// for (SearchHit searchHit : response.getHits()) {
// System.out.println(searchHit.getId()+" "+searchHit.getScore());
// }
// double a = 0/0;
// }
//
// }
static void test21() throws Exception {
try (ESIndex index = new ESIndex.Builder().setClientType("node").setIndexName("imdb").build()) {
Ngram ngram1 = new Ngram();
ngram1.setInOrder(true);
ngram1.setNgram("really nice");
ngram1.setField("body");
ngram1.setSlop(0);
String filterQuery = "{\"filtered\":{\"query\":{\"match_all\":{}},\"filter\":{\"term\":{\"split\":\"train\"}}}}";
SearchResponse response = index.spanNear(ngram1, filterQuery, 10);
for (SearchHit searchHit : response.getHits()) {
System.out.println(searchHit.getId() + " " + searchHit.getScore());
}
}
}
use of org.elasticsearch.search.SearchHit in project play2-elasticsearch by cleverage.
the class IndexQuery method toSearchResults.
private IndexResults<T> toSearchResults(SearchResponse searchResponse) {
// Get Total Records Found
long count = searchResponse.getHits().totalHits();
// Get Aggregations
Aggregations aggregationsResponse = searchResponse.getAggregations();
// Get List results
List<T> results = new ArrayList<T>();
// Loop on each one
for (SearchHit h : searchResponse.getHits()) {
// Get Data Map
Map<String, Object> map = h.sourceAsMap();
// Create a new Indexable Object for the return
T objectIndexable = IndexUtils.getInstanceIndex(clazz);
T t = (T) objectIndexable.fromIndex(map);
t.id = h.getId();
t.searchHit = h;
results.add(t);
}
if (Logger.isDebugEnabled()) {
Logger.debug("ElasticSearch : Results -> " + results.toString());
}
// pagination
long pageSize = 10;
if (size > -1) {
pageSize = size;
}
long pageCurrent = 1;
if (from > 0) {
pageCurrent = ((int) (from / pageSize)) + 1;
}
long pageNb;
if (pageSize == 0) {
pageNb = 1;
} else {
pageNb = (long) Math.ceil(new BigDecimal(count).divide(new BigDecimal(pageSize), 2, RoundingMode.HALF_UP).doubleValue());
}
// Return Results
return new IndexResults<T>(count, pageSize, pageCurrent, pageNb, results, aggregationsResponse);
}
use of org.elasticsearch.search.SearchHit in project fess by codelibs.
the class FessEsClient method deleteByQuery.
public int deleteByQuery(final String index, final String type, final QueryBuilder queryBuilder) {
final FessConfig fessConfig = ComponentUtil.getFessConfig();
SearchResponse response = client.prepareSearch(index).setTypes(type).setScroll(scrollForDelete).setSize(sizeForDelete).setFetchSource(new String[] { fessConfig.getIndexFieldId() }, null).setQuery(queryBuilder).setPreference(Constants.SEARCH_PREFERENCE_PRIMARY).execute().actionGet(fessConfig.getIndexScrollSearchTimeoutTimeout());
int count = 0;
String scrollId = response.getScrollId();
while (scrollId != null) {
final SearchHits searchHits = response.getHits();
final SearchHit[] hits = searchHits.getHits();
if (hits.length == 0) {
scrollId = null;
break;
}
final BulkRequestBuilder bulkRequest = client.prepareBulk();
for (final SearchHit hit : hits) {
bulkRequest.add(client.prepareDelete(index, type, hit.getId()));
}
count += hits.length;
final BulkResponse bulkResponse = bulkRequest.execute().actionGet(fessConfig.getIndexBulkTimeout());
if (bulkResponse.hasFailures()) {
throw new IllegalBehaviorStateException(bulkResponse.buildFailureMessage());
}
response = client.prepareSearchScroll(scrollId).setScroll(scrollForDelete).execute().actionGet(fessConfig.getIndexBulkTimeout());
scrollId = response.getScrollId();
}
return count;
}
use of org.elasticsearch.search.SearchHit in project fess by codelibs.
the class EsAbstractBehavior method delegateQueryDelete.
@Override
protected int delegateQueryDelete(final ConditionBean cb, final DeleteOption<? extends ConditionBean> option) {
SearchResponse response = null;
int count = 0;
while (true) {
if (response == null) {
final SearchRequestBuilder builder = client.prepareSearch(asEsIndex()).setTypes(asEsIndexType()).setScroll(scrollForDelete).setSize(sizeForDelete);
final EsAbstractConditionBean esCb = (EsAbstractConditionBean) cb;
if (esCb.getPreference() != null) {
esCb.setPreference(esCb.getPreference());
}
esCb.request().build(builder);
response = esCb.build(builder).execute().actionGet(scrollSearchTimeout);
} else {
final String scrollId = response.getScrollId();
response = client.prepareSearchScroll(scrollId).setScroll(scrollForDelete).execute().actionGet(scrollSearchTimeout);
}
final SearchHits searchHits = response.getHits();
final SearchHit[] hits = searchHits.getHits();
if (hits.length == 0) {
break;
}
final BulkRequestBuilder bulkRequest = client.prepareBulk();
for (final SearchHit hit : hits) {
bulkRequest.add(client.prepareDelete(asEsIndex(), asEsIndexType(), hit.getId()));
}
count += hits.length;
final BulkResponse bulkResponse = bulkRequest.execute().actionGet(bulkTimeout);
if (bulkResponse.hasFailures()) {
throw new IllegalBehaviorStateException(bulkResponse.buildFailureMessage());
}
}
return count;
}
Aggregations