Skip to content
This repository has been archived by the owner on Jun 1, 2021. It is now read-only.

Commit

Permalink
Merge pull request #208 from KWatzal/#207-spellcheck-queries-always-r…
Browse files Browse the repository at this point in the history
…eturn-0-groups

Construct FacetResults with response from spellcheck queries
  • Loading branch information
tkurz authored Mar 30, 2021
2 parents 1f449a7 + df3739e commit 3a143aa
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -355,10 +355,10 @@ protected SearchResult doExecute(FulltextSearch search, DocumentFactory factory)
ElasticQueryBuilder.buildQuery(search, factory, !validateQueryResponse.isValid(), currentFootprint, elasticSearchClient);
elasticClientLogger.debug(">>> query({})", query.toString());

final SearchResponse response = elasticSearchClient.query(query);
SearchResponse response = elasticSearchClient.query(query);
if(Objects.nonNull(response)
&& Objects.nonNull(response.getHits())
&& Objects.nonNull(response.getHits().getHits())){
&& Objects.nonNull(response.getHits().getHits())) {

final boolean isCursorSearch = search.getResultSet().getType().equals(cursor);
final List<Document> documents = Arrays.stream(response.getHits().getHits())
Expand All @@ -368,14 +368,13 @@ protected SearchResult doExecute(FulltextSearch search, DocumentFactory factory)
long totalHits = response.getHits().getTotalHits().value;
long queryTime = response.getTook().getMillis();

if ( search.isSpellcheck()
&& CollectionUtils.isEmpty(documents)) {
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 (spellCheckedQuery != null && CollectionUtils.isNotEmpty(spellCheckedQuery)) {
if (CollectionUtils.isNotEmpty(spellCheckedQuery)) {
final Iterator<String> iterator = spellCheckedQuery.iterator();
while (iterator.hasNext()) {
final String text = iterator.next();
Expand All @@ -386,6 +385,7 @@ protected SearchResult doExecute(FulltextSearch search, DocumentFactory factory)
queryTime = queryTime + spellcheckResponse.getTook().getMillis();
if(spellcheckResponse.getHits().getTotalHits().value > 0) {
totalHits = spellcheckResponse.getHits().getTotalHits().value;
response = spellcheckResponse;
documents.addAll(Arrays.stream(spellcheckResponse.getHits().getHits())
.map(hit -> DocumentUtil.buildVindDoc(hit, factory, search.getSearchContext()))
.collect(Collectors.toList()));
Expand Down
47 changes: 47 additions & 0 deletions test/src/test/java/com/rbmhtechnology/vind/test/ServerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
Expand Down Expand Up @@ -3804,4 +3805,50 @@ public void testSingleQuotesSearch() {
result = server.execute(Search.fulltext("rubik`s").page(1,15).spellcheck(true),assets);
assertEquals(14, result.getResults().size());
}

@Test
@RunWithBackend({Elastic})
public void testSpellcheckResultHasGroups() {

SingleValueFieldDescriptor.DateFieldDescriptor<ZonedDateTime> created = new FieldDescriptorBuilder()
.setFacet(true)
.buildDateField("created");

FieldDescriptor<String> textField = new FieldDescriptorBuilder()
.setFullText(true)
.setSuggest(true)
.setFacet(true)
.buildTextField("textField");


DocumentFactory assets = new DocumentFactoryBuilder("asset")
.addField(textField)
.addField(created)
.build();

Document d1 = assets.createDoc("1")
.setValue(textField, "Stark")
.setValue(created, ZonedDateTime.now());
Document d2 = assets.createDoc("2")
.setValue(textField, "Stark")
.setValue(created, ZonedDateTime.now());
Document d3 = assets.createDoc("3")
.setValue(textField, "Widow")
.setValue(created, ZonedDateTime.now());

SearchServer server = testBackend.getSearchServer();

server.index(d1);
server.index(d2);
server.index(d3);
server.commit();

SearchResult result = server.execute(
Search.fulltext("Sark").spellcheck(true).facet(created),
assets
);

assertEquals(2, result.getResults().size());
assertEquals(2, result.getFacetResults().getTermFacet(created).getValues().size());
}
}

0 comments on commit 3a143aa

Please sign in to comment.