Search in sources :

Example 1 with FacetResults

use of com.rbmhtechnology.vind.api.result.FacetResults in project vind by RBMHTechnology.

the class SolrSearchServer method doExecute.

@Override
protected SearchResult doExecute(FulltextSearch search, DocumentFactory factory) {
    final SolrQuery query = buildSolrQuery(search, factory);
    // query
    try {
        solrClientLogger.debug(">>> query({})", query.toString());
        final QueryResponse response = solrClient.query(query, REQUEST_METHOD);
        if (response != null) {
            final SolrDocumentList responseResults = response.getResults();
            if (search.isSpellcheck() && responseResults.getNumFound() <= 0 && response.getSpellCheckResponse() != null && !response.getSpellCheckResponse().isCorrectlySpelled() && CollectionUtils.isNotEmpty(response.getSpellCheckResponse().getCollatedResults())) {
                final FulltextSearch spellcheckSearch = search.copy().spellcheck(false).text(response.getSpellCheckResponse().getCollatedResult());
                return execute(spellcheckSearch, factory);
            }
            final Map<String, Integer> childCounts = SolrUtils.getChildCounts(response);
            final List<Document> documents = SolrUtils.Result.buildResultList(responseResults, childCounts, factory, search.getSearchContext());
            final FacetResults facetResults = SolrUtils.Result.buildFacetResult(response, factory, search.getChildrenFactory(), search.getFacets(), search.getSearchContext());
            switch(search.getResultSet().getType()) {
                case page:
                    {
                        return new PageResult(responseResults.getNumFound(), response.getQTime(), documents, search, facetResults, this, factory).setElapsedTime(response.getElapsedTime());
                    }
                case slice:
                    {
                        return new SliceResult(responseResults.getNumFound(), response.getQTime(), documents, search, facetResults, this, factory).setElapsedTime(response.getElapsedTime());
                    }
                default:
                    return new PageResult(responseResults.getNumFound(), response.getQTime(), documents, search, facetResults, this, factory).setElapsedTime(response.getElapsedTime());
            }
        } else {
            throw new SolrServerException("Null result from SolrClient");
        }
    } catch (SolrServerException | IOException e) {
        throw new SearchServerException("Cannot issue query", e);
    }
}
Also used : SliceResult(com.rbmhtechnology.vind.api.result.SliceResult) SolrServerException(org.apache.solr.client.solrj.SolrServerException) SolrDocumentList(org.apache.solr.common.SolrDocumentList) IOException(java.io.IOException) SolrInputDocument(org.apache.solr.common.SolrInputDocument) Document(com.rbmhtechnology.vind.api.Document) SolrDocument(org.apache.solr.common.SolrDocument) SolrQuery(org.apache.solr.client.solrj.SolrQuery) PageResult(com.rbmhtechnology.vind.api.result.PageResult) FacetResults(com.rbmhtechnology.vind.api.result.FacetResults) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SearchServerException(com.rbmhtechnology.vind.SearchServerException) FulltextSearch(com.rbmhtechnology.vind.api.query.FulltextSearch)

Example 2 with FacetResults

use of com.rbmhtechnology.vind.api.result.FacetResults in project vind by RBMHTechnology.

the class ElasticSearchServer method doExecute.

@Override
protected SearchResult doExecute(FulltextSearch search, DocumentFactory factory) {
    final StopWatch elapsedtime = StopWatch.createStarted();
    // query
    try {
        final String searchString = search.isEscapeCharacter() ? escapeSpecialCharacters(search.getSearchString()) : search.getSearchString();
        final ValidateQueryResponse validateQueryResponse = elasticSearchClient.validateQuery(searchString);
        if (validateQueryResponse.isValid()) {
            search.text(searchString);
        }
        final SearchSourceBuilder query = ElasticQueryBuilder.buildQuery(search, factory, !validateQueryResponse.isValid(), currentFootprint, elasticSearchClient);
        FulltextSearch usedSearch = search.copy();
        elasticClientLogger.debug(">>> query({})", query.toString());
        SearchResponse response = elasticSearchClient.query(query);
        if (Objects.nonNull(response) && Objects.nonNull(response.getHits()) && Objects.nonNull(response.getHits().getHits())) {
            final boolean isCursorSearch = search.getResultSet().getType().equals(cursor);
            final List<Document> documents = Arrays.stream(response.getHits().getHits()).map(hit -> DocumentUtil.buildVindDoc(hit, factory, search.getSearchContext(), isCursorSearch)).collect(Collectors.toList());
            long totalHits = response.getHits().getTotalHits().value;
            long queryTime = response.getTook().getMillis();
            if (search.isSpellcheck() && CollectionUtils.isEmpty(documents)) {
                // if no results, try spellchecker (if defined and if spellchecked query differs from original)
                final List<String> spellCheckedQuery = ElasticQueryBuilder.getSpellCheckedQuery(search.getSearchString(), response);
                // query with checked query
                if (CollectionUtils.isNotEmpty(spellCheckedQuery)) {
                    final Iterator<String> iterator = spellCheckedQuery.iterator();
                    while (iterator.hasNext()) {
                        final String text = iterator.next();
                        final FulltextSearch spellcheckSearch = search.copy().text(text).spellcheck(false);
                        final SearchSourceBuilder spellcheckQuery = ElasticQueryBuilder.buildQuery(spellcheckSearch, factory, currentFootprint, elasticSearchClient);
                        final SearchResponse spellcheckResponse = elasticSearchClient.query(spellcheckQuery);
                        queryTime = queryTime + spellcheckResponse.getTook().getMillis();
                        if (spellcheckResponse.getHits().getTotalHits().value > 0) {
                            totalHits = spellcheckResponse.getHits().getTotalHits().value;
                            response = spellcheckResponse;
                            usedSearch = spellcheckSearch.copy();
                            documents.addAll(Arrays.stream(spellcheckResponse.getHits().getHits()).map(hit -> DocumentUtil.buildVindDoc(hit, factory, search.getSearchContext())).collect(Collectors.toList()));
                            break;
                        }
                    }
                }
            }
            // Building Vind Facet Results
            final FacetResults facetResults = ResultUtils.buildFacetResults(response.getAggregations(), factory, search.getFacets(), search.getSearchContext());
            elapsedtime.stop();
            switch(search.getResultSet().getType()) {
                case page:
                    {
                        return new PageResult(totalHits, queryTime, documents, usedSearch, facetResults, this, factory).setElapsedTime(elapsedtime.getTime());
                    }
                case slice:
                    {
                        return new SliceResult(totalHits, queryTime, documents, usedSearch, facetResults, this, factory).setElapsedTime(elapsedtime.getTime());
                    }
                case cursor:
                    {
                        if (!documents.isEmpty()) {
                            // Get cursor from last document result
                            final String searchAfterCursor = documents.get(documents.size() - 1).getSearchAfterCursor().get();
                            // Update used search with next cursor
                            ((Cursor) usedSearch.getResultSet()).setSearchAfter(searchAfterCursor);
                        }
                        // Build result with used search
                        return new CursorResult(totalHits, queryTime, documents, usedSearch, facetResults, this, factory).setElapsedTime(elapsedtime.getTime());
                    }
                default:
                    return new PageResult(totalHits, queryTime, documents, usedSearch, facetResults, this, factory).setElapsedTime(elapsedtime.getTime());
            }
        } else {
            throw new ElasticsearchException("Empty result from ElasticClient");
        }
    } catch (ElasticsearchException | IOException e) {
        throw new SearchServerException(String.format("Cannot issue query: %s", e.getMessage()), e);
    }
}
Also used : ElasticsearchException(org.elasticsearch.ElasticsearchException) PageResult(com.rbmhtechnology.vind.api.result.PageResult) Arrays(java.util.Arrays) Delete(com.rbmhtechnology.vind.api.query.delete.Delete) InverseSearchSliceResult(com.rbmhtechnology.vind.api.result.InverseSearchSliceResult) FieldUtil(com.rbmhtechnology.vind.elasticsearch.backend.util.FieldUtil) SearchServerException(com.rbmhtechnology.vind.SearchServerException) LoggerFactory(org.slf4j.LoggerFactory) ObjectInputStream(java.io.ObjectInputStream) Asserts(org.apache.http.util.Asserts) QueryBuilders(org.elasticsearch.index.query.QueryBuilders) DocumentUtil(com.rbmhtechnology.vind.elasticsearch.backend.util.DocumentUtil) ByteBuffer(java.nio.ByteBuffer) ByteArrayInputStream(java.io.ByteArrayInputStream) FulltextSearch(com.rbmhtechnology.vind.api.query.FulltextSearch) UpdateResponse(org.elasticsearch.action.update.UpdateResponse) Map(java.util.Map) SearchResponse(org.elasticsearch.action.search.SearchResponse) DocumentUtil.createEmptyDocument(com.rbmhtechnology.vind.elasticsearch.backend.util.DocumentUtil.createEmptyDocument) DeleteResponse(org.elasticsearch.action.delete.DeleteResponse) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Failure(org.elasticsearch.action.bulk.BulkItemResponse.Failure) ElasticQueryBuilder(com.rbmhtechnology.vind.elasticsearch.backend.util.ElasticQueryBuilder) InverseSearch(com.rbmhtechnology.vind.api.query.inverseSearch.InverseSearch) InverseSearchQueryFactory(com.rbmhtechnology.vind.api.query.inverseSearch.InverseSearchQueryFactory) SearchResult(com.rbmhtechnology.vind.api.result.SearchResult) MultiGetResponse(org.elasticsearch.action.get.MultiGetResponse) AnnotationUtil(com.rbmhtechnology.vind.annotations.AnnotationUtil) BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) Update(com.rbmhtechnology.vind.api.query.update.Update) Collection(java.util.Collection) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) ServiceLoader(java.util.ServiceLoader) StopWatch(org.apache.commons.lang3.time.StopWatch) GetResult(com.rbmhtechnology.vind.api.result.GetResult) Collectors(java.util.stream.Collectors) SuggestionResult(com.rbmhtechnology.vind.api.result.SuggestionResult) Objects(java.util.Objects) RealTimeGet(com.rbmhtechnology.vind.api.query.get.RealTimeGet) CursorResult(com.rbmhtechnology.vind.api.result.CursorResult) List(java.util.List) Stream(java.util.stream.Stream) InverseSearchResult(com.rbmhtechnology.vind.api.result.InverseSearchResult) ValidateQueryResponse(org.elasticsearch.action.admin.indices.validate.query.ValidateQueryResponse) ServiceProvider(com.rbmhtechnology.vind.api.ServiceProvider) ElasticVindClient(com.rbmhtechnology.vind.elasticsearch.backend.client.ElasticVindClient) BeanGetResult(com.rbmhtechnology.vind.api.result.BeanGetResult) NotImplementedException(org.apache.commons.lang3.NotImplementedException) SpecialCharacterEscaping.escapeSpecialCharacters(com.rbmhtechnology.vind.utils.SpecialCharacterEscaping.escapeSpecialCharacters) ExecutableSuggestionSearch(com.rbmhtechnology.vind.api.query.suggestion.ExecutableSuggestionSearch) IndexResult(com.rbmhtechnology.vind.api.result.IndexResult) FieldDescriptor(com.rbmhtechnology.vind.model.FieldDescriptor) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SliceResult(com.rbmhtechnology.vind.api.result.SliceResult) CollectionUtils(org.apache.commons.collections.CollectionUtils) PainlessScript(com.rbmhtechnology.vind.elasticsearch.backend.util.PainlessScript) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) StatusResult(com.rbmhtechnology.vind.api.result.StatusResult) SmartSearchServerBase(com.rbmhtechnology.vind.api.SmartSearchServerBase) BeanSearchResult(com.rbmhtechnology.vind.api.result.BeanSearchResult) BulkByScrollResponse(org.elasticsearch.index.reindex.BulkByScrollResponse) SearchServerInstantiateException(com.rbmhtechnology.vind.SearchServerInstantiateException) TermFacetResult(com.rbmhtechnology.vind.api.result.facet.TermFacetResult) SearchConfiguration(com.rbmhtechnology.vind.configure.SearchConfiguration) InverseSearchQuery(com.rbmhtechnology.vind.model.InverseSearchQuery) DeleteResult(com.rbmhtechnology.vind.api.result.DeleteResult) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) FacetResults(com.rbmhtechnology.vind.api.result.FacetResults) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) DivisionType.cursor(com.rbmhtechnology.vind.api.query.division.ResultSubset.DivisionType.cursor) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) Document(com.rbmhtechnology.vind.api.Document) InverseSearchPageResult(com.rbmhtechnology.vind.api.result.InverseSearchPageResult) ElasticMappingUtils(com.rbmhtechnology.vind.elasticsearch.backend.util.ElasticMappingUtils) TimeUnit(java.util.concurrent.TimeUnit) ResultUtils(com.rbmhtechnology.vind.elasticsearch.backend.util.ResultUtils) DocumentFactory(com.rbmhtechnology.vind.model.DocumentFactory) SearchServerProviderLoaderException(com.rbmhtechnology.vind.SearchServerProviderLoaderException) Cursor(com.rbmhtechnology.vind.api.query.division.Cursor) Filter(com.rbmhtechnology.vind.api.query.filter.Filter) Collections(java.util.Collections) InverseSearchSliceResult(com.rbmhtechnology.vind.api.result.InverseSearchSliceResult) SliceResult(com.rbmhtechnology.vind.api.result.SliceResult) CursorResult(com.rbmhtechnology.vind.api.result.CursorResult) ValidateQueryResponse(org.elasticsearch.action.admin.indices.validate.query.ValidateQueryResponse) ElasticsearchException(org.elasticsearch.ElasticsearchException) IOException(java.io.IOException) DocumentUtil.createEmptyDocument(com.rbmhtechnology.vind.elasticsearch.backend.util.DocumentUtil.createEmptyDocument) Document(com.rbmhtechnology.vind.api.Document) PageResult(com.rbmhtechnology.vind.api.result.PageResult) InverseSearchPageResult(com.rbmhtechnology.vind.api.result.InverseSearchPageResult) StopWatch(org.apache.commons.lang3.time.StopWatch) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) FacetResults(com.rbmhtechnology.vind.api.result.FacetResults) SearchServerException(com.rbmhtechnology.vind.SearchServerException) FulltextSearch(com.rbmhtechnology.vind.api.query.FulltextSearch)

Aggregations

SearchServerException (com.rbmhtechnology.vind.SearchServerException)2 Document (com.rbmhtechnology.vind.api.Document)2 FulltextSearch (com.rbmhtechnology.vind.api.query.FulltextSearch)2 FacetResults (com.rbmhtechnology.vind.api.result.FacetResults)2 PageResult (com.rbmhtechnology.vind.api.result.PageResult)2 SliceResult (com.rbmhtechnology.vind.api.result.SliceResult)2 IOException (java.io.IOException)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 SearchServerInstantiateException (com.rbmhtechnology.vind.SearchServerInstantiateException)1 SearchServerProviderLoaderException (com.rbmhtechnology.vind.SearchServerProviderLoaderException)1 AnnotationUtil (com.rbmhtechnology.vind.annotations.AnnotationUtil)1 ServiceProvider (com.rbmhtechnology.vind.api.ServiceProvider)1 SmartSearchServerBase (com.rbmhtechnology.vind.api.SmartSearchServerBase)1 Delete (com.rbmhtechnology.vind.api.query.delete.Delete)1 Cursor (com.rbmhtechnology.vind.api.query.division.Cursor)1 DivisionType.cursor (com.rbmhtechnology.vind.api.query.division.ResultSubset.DivisionType.cursor)1 Filter (com.rbmhtechnology.vind.api.query.filter.Filter)1 RealTimeGet (com.rbmhtechnology.vind.api.query.get.RealTimeGet)1 InverseSearch (com.rbmhtechnology.vind.api.query.inverseSearch.InverseSearch)1