Skip to content

Commit 4bddde0

Browse files
committed
added further tests for full text queries
1 parent 243d9d8 commit 4bddde0

File tree

5 files changed

+116
-24
lines changed

5 files changed

+116
-24
lines changed

server/src/internalClusterTest/java/org/elasticsearch/search/fetch/subphase/highlight/HighlighterSearchIT.java

Lines changed: 77 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
import static org.elasticsearch.index.query.QueryBuilders.existsQuery;
7878
import static org.elasticsearch.index.query.QueryBuilders.functionScoreQuery;
7979
import static org.elasticsearch.index.query.QueryBuilders.fuzzyQuery;
80+
import static org.elasticsearch.index.query.QueryBuilders.matchBoolPrefixQuery;
8081
import static org.elasticsearch.index.query.QueryBuilders.matchPhrasePrefixQuery;
8182
import static org.elasticsearch.index.query.QueryBuilders.matchPhraseQuery;
8283
import static org.elasticsearch.index.query.QueryBuilders.matchQuery;
@@ -3644,7 +3645,6 @@ public void testConstantKeywordFieldNoHighlightingWildcard() throws IOException
36443645
String index = "test";
36453646
String constantKeywordFieldName = "test_constant_keyword_field";
36463647
String constantValue = "constant_value";
3647-
String partialConstantValueWithWildCard = "foobar*";
36483648

36493649
XContentBuilder mappings = prepareConstantKeywordMappings(constantKeywordFieldName, constantValue);
36503650
assertAcked(prepareCreate(index).setMapping(mappings));
@@ -3775,6 +3775,25 @@ public void testConstantKeywordFieldAndOtherFieldsMatchHighlighted() throws IOEx
37753775

37763776
}
37773777

3778+
public void testConstantKeywordMultiMatchQuery() throws IOException {
3779+
String index = "test";
3780+
String constantKeywordFieldName = "test_constant_keyword_field";
3781+
String constantValue = "bar";
3782+
3783+
XContentBuilder mappings = prepareConstantKeywordMappings(constantKeywordFieldName, constantValue);
3784+
assertAcked(prepareCreate(index).setMapping(mappings));
3785+
3786+
XContentBuilder document = jsonBuilder().startObject().field("foo", constantValue).endObject();
3787+
saveDocumentIntoIndex(index, "1", document);
3788+
3789+
SearchResponse search = prepareConstantKeywordSearch(multiMatchQuery(constantValue, constantKeywordFieldName, "foo"));
3790+
3791+
assertNoFailures(search);
3792+
assertHighlight(search, 0, constantKeywordFieldName, 0, 1, equalTo("<em>bar</em>"));
3793+
assertHighlight(search, 0, "foo", 0, 1, equalTo("<em>bar</em>"));
3794+
3795+
}
3796+
37783797
public void testConstantKeywordMultipleHitsHighlighted() throws IOException {
37793798
String index = "test";
37803799
String constantKeywordFieldName = "test_constant_keyword_field";
@@ -3824,26 +3843,13 @@ public void testDoubleConstantKeywordJustOneHighlighted() throws IOException {
38243843
String firstConstantValue = "constant_value_1";
38253844
String secondConstantValue = "constant_value_2";
38263845

3827-
XContentBuilder mappings = jsonBuilder();
3828-
mappings.startObject();
3829-
mappings.startObject("_doc")
3830-
.startObject("properties")
3831-
.startObject(firstConstantKeywordFieldName)
3832-
.field("type", "constant_keyword")
3833-
.field("value", firstConstantValue)
3834-
.endObject()
3835-
.startObject(secondConstantKeywordFieldName)
3836-
.field("type", "constant_keyword")
3837-
.field("value", secondConstantValue)
3838-
.endObject()
3839-
.startObject("foo")
3840-
.field("type", "text")
3841-
.endObject()
3842-
.endObject()
3843-
.endObject();
3844-
mappings.endObject();
3845-
3846-
assertAcked(prepareCreate(index).setMapping(mappings));
3846+
XContentBuilder mappings = prepareDoubleConstantKeywordMappings(
3847+
firstConstantKeywordFieldName,
3848+
firstConstantValue,
3849+
secondConstantKeywordFieldName,
3850+
secondConstantValue
3851+
);
3852+
prepareCreate(index).setMapping(mappings).get();
38473853

38483854
XContentBuilder document = jsonBuilder().startObject().field("foo", "bar").endObject();
38493855
saveDocumentIntoIndex(index, "1", document);
@@ -3855,7 +3861,55 @@ public void testDoubleConstantKeywordJustOneHighlighted() throws IOException {
38553861
assertHighlight(search, 0, firstConstantKeywordFieldName, 0, 1, equalTo("<em>constant_value_1</em>"));
38563862
}
38573863

3858-
public void testConstantKeywordNestedShouldQuery() throws Exception {
3864+
public void testConstantKeywordBoolPrefixQueryJustOneHighlighted() throws IOException {
3865+
String index = "test";
3866+
String firstConstantKeywordFieldName = "test_constant_keyword_field_1";
3867+
String firstConstantValue = "constant_value_1";
3868+
String secondConstantKeywordFieldName = "test_constant_keyword_field_2";
3869+
String secondConstantValue = "constant_value_2";
3870+
3871+
XContentBuilder mappings = prepareDoubleConstantKeywordMappings(
3872+
firstConstantKeywordFieldName,
3873+
firstConstantValue,
3874+
secondConstantKeywordFieldName,
3875+
secondConstantValue
3876+
);
3877+
assertAcked(prepareCreate(index).setMapping(mappings));
3878+
3879+
XContentBuilder document = jsonBuilder().startObject()
3880+
.field("foo", "bar")
3881+
.field(firstConstantKeywordFieldName, firstConstantValue)
3882+
.endObject();
3883+
saveDocumentIntoIndex(index, "1", document);
3884+
3885+
SearchResponse search = prepareConstantKeywordSearch(matchBoolPrefixQuery(firstConstantKeywordFieldName, "constant_value_1"));
3886+
3887+
assertNoFailures(search);
3888+
assertHighlight(search, 0, firstConstantKeywordFieldName, 0, 1, equalTo("<em>constant_value_1</em>"));
3889+
assertNotHighlighted(search, 0, secondConstantKeywordFieldName);
3890+
}
3891+
3892+
public void testConstantKeywordFieldMatchPhraseQuery() throws IOException {
3893+
String index = "test";
3894+
String constantKeywordFieldName = "test_constant_keyword_field";
3895+
String constantValue = "constant_value";
3896+
3897+
XContentBuilder mappings = prepareConstantKeywordMappings(constantKeywordFieldName, constantValue);
3898+
assertAcked(prepareCreate(index).setMapping(mappings));
3899+
3900+
XContentBuilder document = jsonBuilder().startObject()
3901+
.field("foo", "bar")
3902+
.field(constantKeywordFieldName, constantValue)
3903+
.endObject();
3904+
saveDocumentIntoIndex(index, "1", document);
3905+
3906+
SearchResponse search = prepareConstantKeywordSearch(matchPhraseQuery(constantKeywordFieldName, constantValue));
3907+
3908+
assertNoFailures(search);
3909+
assertHighlight(search, 0, constantKeywordFieldName, 0, 1, equalTo("<em>constant_value</em>"));
3910+
}
3911+
3912+
public void testConstantKeywordNestedShouldBoolQuery() throws Exception {
38593913
String index = "test";
38603914
String constantKeywordFieldName = "test_constant_keyword_field";
38613915
String constantValue = "constant_value";
@@ -3872,7 +3926,7 @@ public void testConstantKeywordNestedShouldQuery() throws Exception {
38723926
assertHighlight(search, 0, constantKeywordFieldName, 0, 1, equalTo("<em>constant_value</em>"));
38733927
}
38743928

3875-
public void testConstantKeywordNestedMustQuery() throws Exception {
3929+
public void testConstantKeywordNestedMustBoolQuery() throws Exception {
38763930
String index = "test";
38773931
String constantKeywordFieldName = "test_constant_keyword_field";
38783932
String constantValue = "constant_value";

server/src/main/java/org/elasticsearch/TransportVersion.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@
1616

1717
import java.io.IOException;
1818
import java.lang.reflect.Field;
19-
import java.util.*;
19+
import java.util.Collection;
20+
import java.util.Collections;
21+
import java.util.HashMap;
22+
import java.util.Map;
23+
import java.util.NavigableMap;
24+
import java.util.Set;
25+
import java.util.TreeMap;
2026

2127
/**
2228
* Represents the version of the wire protocol used to communicate between a pair of ES nodes.

server/src/main/java/org/elasticsearch/index/query/MatchBoolPrefixQueryBuilder.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88

99
package org.elasticsearch.index.query;
1010

11+
import org.apache.lucene.index.Term;
1112
import org.apache.lucene.search.FuzzyQuery;
1213
import org.apache.lucene.search.Query;
14+
import org.apache.lucene.search.TermQuery;
1315
import org.elasticsearch.TransportVersion;
1416
import org.elasticsearch.common.ParsingException;
1517
import org.elasticsearch.common.Strings;
@@ -407,4 +409,12 @@ public String getWriteableName() {
407409
public TransportVersion getMinimalSupportedVersion() {
408410
return TransportVersion.V_7_2_0;
409411
}
412+
413+
@Override
414+
public Query toHighlightQuery(String fieldName) {
415+
if (this.fieldName.equals(fieldName)) {
416+
return new TermQuery(new Term(fieldName, maybeConvertToString(value).toString()));
417+
}
418+
return super.toHighlightQuery(fieldName);
419+
}
410420
}

server/src/main/java/org/elasticsearch/index/query/MatchPhraseQueryBuilder.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
package org.elasticsearch.index.query;
1010

1111
import org.apache.lucene.analysis.core.KeywordAnalyzer;
12+
import org.apache.lucene.index.Term;
1213
import org.apache.lucene.search.Query;
14+
import org.apache.lucene.search.TermQuery;
1315
import org.elasticsearch.TransportVersion;
1416
import org.elasticsearch.common.ParsingException;
1517
import org.elasticsearch.common.Strings;
@@ -288,4 +290,12 @@ public static MatchPhraseQueryBuilder fromXContent(XContentParser parser) throws
288290
public TransportVersion getMinimalSupportedVersion() {
289291
return TransportVersion.ZERO;
290292
}
293+
294+
@Override
295+
public Query toHighlightQuery(String fieldName) {
296+
if (this.fieldName.equals(fieldName)) {
297+
return new TermQuery(new Term(fieldName, maybeConvertToString(value).toString()));
298+
}
299+
return super.toHighlightQuery(fieldName);
300+
}
291301
}

server/src/main/java/org/elasticsearch/index/query/MultiMatchQueryBuilder.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88

99
package org.elasticsearch.index.query;
1010

11+
import org.apache.lucene.index.Term;
1112
import org.apache.lucene.search.FuzzyQuery;
1213
import org.apache.lucene.search.Query;
14+
import org.apache.lucene.search.TermQuery;
1315
import org.elasticsearch.ElasticsearchParseException;
1416
import org.elasticsearch.TransportVersion;
1517
import org.elasticsearch.common.ParsingException;
@@ -829,4 +831,14 @@ protected boolean doEquals(MultiMatchQueryBuilder other) {
829831
public TransportVersion getMinimalSupportedVersion() {
830832
return TransportVersion.ZERO;
831833
}
834+
835+
@Override
836+
public Query toHighlightQuery(String fieldName) {
837+
return fields().keySet()
838+
.stream()
839+
.filter(fn -> fn.equals(fieldName))
840+
.map(fn -> new TermQuery(new Term(fieldName, maybeConvertToString(value).toString())))
841+
.findFirst()
842+
.orElse(null);
843+
}
832844
}

0 commit comments

Comments
 (0)