Skip to content

Commit b811359

Browse files
yrodiereSanne
authored andcommitted
HSEARCH-2281 Omit the "fuzziness" attribute on queries when not required
1 parent 52970de commit b811359

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

elasticsearch/src/main/java/org/hibernate/search/elasticsearch/impl/ToElasticsearch.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public class ToElasticsearch {
5151
private static final Log LOG = LoggerFactory.make( Log.class );
5252

5353
private static final int DEFAULT_SLOP = 0;
54-
54+
private static final int DEFAULT_MAX_EDIT_DISTANCE = 0;
5555
private static final float DEFAULT_BOOST = 1.0f;
5656

5757
private ToElasticsearch() {
@@ -347,7 +347,7 @@ private static JsonObject convertRemoteMatchQuery(RemoteMatchQuery query) {
347347
JsonBuilder.object()
348348
.addProperty( "query", query.getSearchTerms() )
349349
.addProperty( "analyzer", query.getAnalyzerReference().getAnalyzer().getName( query.getField() ) )
350-
.addProperty( "fuzziness", query.getMaxEditDistance() )
350+
.append( fuzzinessAppender( query.getMaxEditDistance() ) )
351351
.append( boostAppender( query ) )
352352
)
353353
).build();
@@ -484,6 +484,23 @@ public void append(JsonBuilder.Object object) {
484484
}
485485
}
486486

487+
/**
488+
* Appender that adds a "fuzziness" property if necessary.
489+
*/
490+
private static JsonBuilder.JsonAppender<? super JsonBuilder.Object> fuzzinessAppender(final int maxEditDistance) {
491+
if ( maxEditDistance != DEFAULT_MAX_EDIT_DISTANCE ) {
492+
return new JsonBuilder.JsonAppender<JsonBuilder.Object>() {
493+
@Override
494+
public void append(JsonBuilder.Object object) {
495+
object.addProperty( "fuzziness", maxEditDistance );
496+
}
497+
};
498+
}
499+
else {
500+
return NOOP_APPENDER;
501+
}
502+
}
503+
487504
/**
488505
* Appender that adds a "boost" property if necessary.
489506
*/

elasticsearch/src/test/java/org/hibernate/search/elasticsearch/test/ElasticsearchDSLIT.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,27 @@ public void testDSLKeyword() throws Exception {
9999

100100
FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery( query, Letter.class );
101101
String queryString = fullTextQuery.getQueryString();
102-
assertJsonEquals( "{'query':{'match':{'message':{'query':'A very important matter','analyzer':'english','fuzziness':0}}}}", queryString );
102+
assertJsonEquals( "{'query':{'match':{'message':{'query':'A very important matter','analyzer':'english'}}}}", queryString );
103+
}
104+
}
105+
106+
@Test
107+
public void testDSLKeywordWithFuzziness() throws Exception {
108+
try ( Session session = openSession() ) {
109+
FullTextSession fullTextSession = Search.getFullTextSession( session );
110+
final QueryBuilder queryBuilder = queryBuilder( fullTextSession );
111+
112+
Query query = queryBuilder
113+
.keyword()
114+
.fuzzy()
115+
.withEditDistanceUpTo( 2 )
116+
.onField( "message" )
117+
.matching( "A very important matter" )
118+
.createQuery();
119+
120+
FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery( query, Letter.class );
121+
String queryString = fullTextQuery.getQueryString();
122+
assertJsonEquals( "{'query':{'match':{'message':{'query':'A very important matter','analyzer':'english','fuzziness':2}}}}", queryString );
103123
}
104124
}
105125

@@ -118,7 +138,7 @@ public void testDSLKeywordWithBoost() throws Exception {
118138

119139
FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery( query, Letter.class );
120140
String queryString = fullTextQuery.getQueryString();
121-
assertJsonEquals( "{'query':{'match':{'message':{'query':'A very important matter','analyzer':'english','fuzziness':0,'boost':2.0}}}}", queryString );
141+
assertJsonEquals( "{'query':{'match':{'message':{'query':'A very important matter','analyzer':'english','boost':2.0}}}}", queryString );
122142
}
123143
}
124144

0 commit comments

Comments
 (0)