Search in sources :

Example 36 with SearchRequestBuilder

use of org.elasticsearch.action.search.SearchRequestBuilder in project stash-codesearch-plugin by palantir.

the class SearchUpdateJobImpl method doReindex.

@Override
public void doReindex(Client client, GitScm gitScm, GlobalSettings globalSettings) {
    if (!globalSettings.getIndexingEnabled()) {
        return;
    }
    deleteLatestIndexedNote(client);
    while (true) {
        try {
            SearchRequestBuilder esReq = client.prepareSearch(ES_UPDATEALIAS).setSize(1000).setFetchSource(false).setRouting(getRepoDesc()).setQuery(filteredQuery(matchAllQuery(), andFilter(sfu.projectRepositoryFilter(repository.getProject().getKey(), repository.getSlug()), sfu.exactRefFilter(ref))));
            BulkRequestBuilder bulkDelete = client.prepareBulk().setRefresh(true);
            for (SearchHit hit : esReq.get().getHits().getHits()) {
                bulkDelete.add(buildDeleteFromRef(client, hit.getType(), hit.getId()));
            }
            if (bulkDelete.numberOfActions() == 0) {
                break;
            }
            bulkDelete.get();
        } catch (Exception e) {
            log.error("Could not delete documents for {}, aborting", toString(), e);
            return;
        }
    }
    doUpdate(client, gitScm, globalSettings);
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder)

Example 37 with SearchRequestBuilder

use of org.elasticsearch.action.search.SearchRequestBuilder in project elasticsearch-opennlp-plugin by spinscale.

the class OpenNlpPluginIntegrationTest method testThatFacetingIsWorking.

@Test
public void testThatFacetingIsWorking() throws Exception {
    putMapping("/test-mapping-analyzers.json");
    String sampleText = copyToStringFromClasspath("/sample-text.txt");
    IndexResponse indexResponse = indexElement(sampleText);
    SearchResponse searchResponse = new SearchRequestBuilder(node.client()).setIndices(index).setTypes(type).setQuery(QueryBuilders.matchAllQuery()).addFacet(new TermsFacetBuilder("names").field("someFieldAnalyzed.name").order(TermsFacet.ComparatorType.TERM)).execute().actionGet();
    assertThat(searchResponse.getHits().totalHits(), is(1L));
    assertThat(searchResponse.getHits().getAt(0).id(), is(indexResponse.getId()));
    TermsFacet termsFacet = searchResponse.getFacets().facet(TermsFacet.class, "names");
    assertThat(termsFacet.getTotalCount(), is(2L));
    assertThat(termsFacet.getEntries().get(0).getTerm().string(), is("Jack Nicholson"));
    assertThat(termsFacet.getEntries().get(1).getTerm().string(), is("Kobe Bryant"));
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) TermsFacetBuilder(org.elasticsearch.search.facet.terms.TermsFacetBuilder) CreateIndexResponse(org.elasticsearch.action.admin.indices.create.CreateIndexResponse) IndexResponse(org.elasticsearch.action.index.IndexResponse) TermsFacet(org.elasticsearch.search.facet.terms.TermsFacet) SearchResponse(org.elasticsearch.action.search.SearchResponse) Test(org.junit.Test)

Example 38 with SearchRequestBuilder

use of org.elasticsearch.action.search.SearchRequestBuilder in project titan by thinkaurelius.

the class ElasticSearchIndex method query.

@Override
public Iterable<RawQuery.Result<String>> query(RawQuery query, KeyInformation.IndexRetriever informations, BaseTransaction tx) throws BackendException {
    SearchRequestBuilder srb = client.prepareSearch(indexName);
    srb.setTypes(query.getStore());
    srb.setQuery(QueryBuilders.queryStringQuery(query.getQuery()));
    srb.setFrom(query.getOffset());
    if (query.hasLimit())
        srb.setSize(query.getLimit());
    else
        srb.setSize(maxResultsSize);
    srb.setNoFields();
    //srb.setExplain(true);
    SearchResponse response = srb.execute().actionGet();
    log.debug("Executed query [{}] in {} ms", query.getQuery(), response.getTookInMillis());
    SearchHits hits = response.getHits();
    if (!query.hasLimit() && hits.totalHits() >= maxResultsSize)
        log.warn("Query result set truncated to first [{}] elements for query: {}", maxResultsSize, query);
    List<RawQuery.Result<String>> result = new ArrayList<RawQuery.Result<String>>(hits.hits().length);
    for (SearchHit hit : hits) {
        result.add(new RawQuery.Result<String>(hit.id(), hit.getScore()));
    }
    return result;
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) SearchHits(org.elasticsearch.search.SearchHits) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 39 with SearchRequestBuilder

use of org.elasticsearch.action.search.SearchRequestBuilder in project metacat by Netflix.

the class ElasticSearchUtilImpl method getIdsByQualifiedName.

/**
     * List of names.
     *
     * @param type          type
     * @param qualifiedName name
     * @return list of names
     */
public List<String> getIdsByQualifiedName(final String type, final QualifiedName qualifiedName) {
    List<String> result = Lists.newArrayList();
    // Run the query and get the response.
    final QueryBuilder queryBuilder = QueryBuilders.boolQuery().must(QueryBuilders.termQuery("name.qualifiedName.tree", qualifiedName)).must(QueryBuilders.termQuery("deleted_", false));
    final SearchRequestBuilder request = client.prepareSearch(esIndex).setTypes(type).setSearchType(SearchType.QUERY_THEN_FETCH).setQuery(queryBuilder).setSize(Integer.MAX_VALUE).setNoFields();
    final SearchResponse response = request.execute().actionGet();
    if (response.getHits().hits().length != 0) {
        result = getIds(response);
    }
    return result;
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 40 with SearchRequestBuilder

use of org.elasticsearch.action.search.SearchRequestBuilder in project metacat by Netflix.

the class ElasticSearchUtilImpl method getTableIdsByUri.

/**
     * List table names by uri.
     *
     * @param type    doc type
     * @param dataUri uri
     * @return list of table names
     */
public List<String> getTableIdsByUri(final String type, final String dataUri) {
    List<String> ids = Lists.newArrayList();
    if (dataUri != null) {
        //
        // Run the query and get the response.
        final SearchRequestBuilder request = client.prepareSearch(esIndex).setTypes(type).setSearchType(SearchType.QUERY_THEN_FETCH).setQuery(QueryBuilders.termQuery("serde.uri", dataUri)).setSize(Integer.MAX_VALUE).setNoFields();
        final SearchResponse response = request.execute().actionGet();
        if (response.getHits().hits().length != 0) {
            ids = getIds(response);
        }
    }
    return ids;
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Aggregations

SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)110 SearchResponse (org.elasticsearch.action.search.SearchResponse)66 SearchHit (org.elasticsearch.search.SearchHit)23 SearchHits (org.elasticsearch.search.SearchHits)18 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)17 ArrayList (java.util.ArrayList)14 Client (org.elasticsearch.client.Client)13 HashMap (java.util.HashMap)12 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)12 Test (org.junit.Test)12 SearchRequest (org.elasticsearch.action.search.SearchRequest)11 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)10 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)9 FilterAggregationBuilder (org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder)9 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)9 SearchPhaseExecutionException (org.elasticsearch.action.search.SearchPhaseExecutionException)8 Filter (org.elasticsearch.search.aggregations.bucket.filter.Filter)8 Map (java.util.Map)7 BulkRequestBuilder (org.elasticsearch.action.bulk.BulkRequestBuilder)7 MultiSearchResponse (org.elasticsearch.action.search.MultiSearchResponse)7