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);
}
}
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);
}
}
Aggregations