Search in sources :

Example 76 with SearchHit

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();
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 77 with SearchHit

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());
        }
    }
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) Ngram(edu.neu.ccs.pyramid.feature.Ngram) SpanNotNgram(edu.neu.ccs.pyramid.feature.SpanNotNgram) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 78 with SearchHit

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);
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) Aggregations(org.elasticsearch.search.aggregations.Aggregations) ArrayList(java.util.ArrayList) BigDecimal(java.math.BigDecimal)

Example 79 with SearchHit

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;
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) IllegalBehaviorStateException(org.dbflute.exception.IllegalBehaviorStateException) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) SearchHits(org.elasticsearch.search.SearchHits) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) MultiSearchResponse(org.elasticsearch.action.search.MultiSearchResponse) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 80 with SearchHit

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;
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) IllegalBehaviorStateException(org.dbflute.exception.IllegalBehaviorStateException) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) SearchHits(org.elasticsearch.search.SearchHits) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Aggregations

SearchHit (org.elasticsearch.search.SearchHit)166 SearchResponse (org.elasticsearch.action.search.SearchResponse)114 SearchHits (org.elasticsearch.search.SearchHits)52 ArrayList (java.util.ArrayList)31 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)25 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)22 IOException (java.io.IOException)20 Matchers.containsString (org.hamcrest.Matchers.containsString)17 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)16 HashMap (java.util.HashMap)15 ScoreDoc (org.apache.lucene.search.ScoreDoc)14 SearchHitField (org.elasticsearch.search.SearchHitField)14 HashSet (java.util.HashSet)13 Map (java.util.Map)12 Test (org.junit.Test)12 AtomicReference (java.util.concurrent.atomic.AtomicReference)10 TopDocs (org.apache.lucene.search.TopDocs)10 Text (org.elasticsearch.common.text.Text)10 BulkRequestBuilder (org.elasticsearch.action.bulk.BulkRequestBuilder)9 BulkResponse (org.elasticsearch.action.bulk.BulkResponse)9