Skip to content

Commit b5b1aba

Browse files
committed
Add example of fuzzy search usage in Elastic module
1 parent 53c0c58 commit b5b1aba

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

elasticsearch/src/test/java/by/andd3dfx/elasticsearch/ElasticSearchManualIT.java

+21-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
import java.util.Arrays;
1414
import java.util.Date;
1515
import java.util.List;
16-
import java.util.stream.Collectors;
16+
import java.util.logging.Logger;
17+
1718
import org.elasticsearch.action.DocWriteResponse.Result;
1819
import org.elasticsearch.action.index.IndexResponse;
1920
import org.elasticsearch.action.search.SearchResponse;
@@ -33,6 +34,8 @@
3334
*/
3435
public class ElasticSearchManualIT {
3536

37+
private static final Logger log = Logger.getLogger(ElasticSearchManualIT.class.getName());
38+
3639
private static List<Person> listOfPersons = new ArrayList<>() {{
3740
add(new Person(8, "John Woodcraft", new Date()));
3841
add(new Person(10, "John Doe", new Date()));
@@ -89,11 +92,26 @@ public void filterByAgeSearch() {
8992
checkSearchResponse(response, 2);
9093
}
9194

92-
private void checkSearchResponse(SearchResponse searchResponse, int expectedSize) {
95+
/**
96+
* Trying to search with error in searched name
97+
*/
98+
@Test
99+
public void fuzzySearch() {
100+
SearchResponse response = client
101+
.prepareSearch()
102+
.setQuery(QueryBuilders.fuzzyQuery("fullName", "Sevenson"))
103+
.get();
104+
105+
var result = checkSearchResponse(response, 1);
106+
log.info("Found next persons: " + result);
107+
}
108+
109+
private List<Person> checkSearchResponse(SearchResponse searchResponse, int expectedSize) {
93110
SearchHit[] searchHits = searchResponse.getHits().getHits();
94111
List<Person> result = Arrays.stream(searchHits)
95112
.map(hit -> JSON.parseObject(hit.getSourceAsString(), Person.class))
96-
.collect(Collectors.toList());
113+
.toList();
97114
assertThat("Wrong result items count", result.size(), is(expectedSize));
115+
return result;
98116
}
99117
}

0 commit comments

Comments
 (0)