Search in sources :

Example 66 with SearchHit

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

Example 67 with SearchHit

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

Example 68 with SearchHit

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

Example 69 with SearchHit

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

Example 70 with SearchHit

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;
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) StopWatch(org.apache.commons.lang3.time.StopWatch) 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