44
44
import java .util .Collection ;
45
45
import java .util .Collections ;
46
46
import java .util .HashMap ;
47
+ import java .util .HashSet ;
47
48
import java .util .List ;
48
49
import java .util .Map ;
50
+ import java .util .Set ;
49
51
50
52
import static java .util .Collections .emptyMap ;
51
53
import static org .elasticsearch .index .query .QueryBuilders .multiMatchQuery ;
52
54
import static org .hamcrest .Matchers .equalTo ;
55
+ import static org .hamcrest .Matchers .instanceOf ;
53
56
54
57
public class MultiMatchQueryParserTests extends ESSingleNodeTestCase {
55
58
@@ -73,15 +76,27 @@ public void setup() throws IOException {
73
76
"properties": {
74
77
"first": {
75
78
"type": "text",
76
- "analyzer": "standard"
79
+ "analyzer": "standard",
80
+ "index_prefixes": {
81
+ "min_chars" : 1,
82
+ "max_chars" : 19
83
+ }
77
84
},
78
85
"last": {
79
86
"type": "text",
80
- "analyzer": "standard"
87
+ "analyzer": "standard",
88
+ "index_prefixes": {
89
+ "min_chars" : 1,
90
+ "max_chars" : 19
91
+ }
81
92
},
82
93
"nickname": {
83
94
"type": "text",
84
- "analyzer": "whitespace"
95
+ "analyzer": "whitespace",
96
+ "index_prefixes": {
97
+ "min_chars" : 1,
98
+ "max_chars" : 19
99
+ }
85
100
}
86
101
}
87
102
}
@@ -93,6 +108,33 @@ public void setup() throws IOException {
93
108
this .indexService = indexService ;
94
109
}
95
110
111
+ public void testToQueryPhrasePrefix () throws IOException {
112
+ SearchExecutionContext searchExecutionContext = indexService .newSearchExecutionContext (randomInt (20 ), 0 , null , () -> {
113
+ throw new UnsupportedOperationException ();
114
+ }, null , emptyMap ());
115
+ searchExecutionContext .setAllowUnmappedFields (true );
116
+ MultiMatchQueryBuilder multiMatchQueryBuilder = new MultiMatchQueryBuilder ("Har" , "name.first" , "name.last" , "name.nickname" );
117
+ multiMatchQueryBuilder .type (MultiMatchQueryBuilder .Type .PHRASE_PREFIX );
118
+ Query query = multiMatchQueryBuilder .toQuery (searchExecutionContext );
119
+ assertThat (query , instanceOf (DisjunctionMaxQuery .class ));
120
+ DisjunctionMaxQuery disjunctionMaxQuery = (DisjunctionMaxQuery ) query ;
121
+ Set <Term > expectedTerms = new HashSet <>(
122
+ Arrays .asList (
123
+ new Term ("name.first._index_prefix" , "har" ),
124
+ new Term ("name.last._index_prefix" , "har" ),
125
+ new Term ("name.nickname._index_prefix" , "Har" )
126
+ )
127
+ );
128
+ for (Query disjunct : disjunctionMaxQuery .getDisjuncts ()) {
129
+ assertThat (disjunct , instanceOf (SynonymQuery .class ));
130
+ SynonymQuery synonymQuery = (SynonymQuery ) disjunct ;
131
+ assertEquals (1 , synonymQuery .getTerms ().size ());
132
+ Term term = synonymQuery .getTerms ().get (0 );
133
+ assertTrue ("Unexpected term " + term , expectedTerms .remove (term ));
134
+ }
135
+ assertEquals ("Expected terms not found in the query: " + expectedTerms , 0 , expectedTerms .size ());
136
+ }
137
+
96
138
public void testCrossFieldMultiMatchQuery () throws IOException {
97
139
SearchExecutionContext searchExecutionContext = indexService .newSearchExecutionContext (randomInt (20 ), 0 , null , () -> {
98
140
throw new UnsupportedOperationException ();
0 commit comments