-
Notifications
You must be signed in to change notification settings - Fork 25k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use index-prefix fields for terms of length min_chars - 1 #36703
Changes from 2 commits
d83e34b
2eda0b2
475a87b
76c67aa
e964ced
f4d6105
274cebd
86480e7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,7 @@ | |
import org.apache.lucene.search.PhraseQuery; | ||
import org.apache.lucene.search.Query; | ||
import org.apache.lucene.search.TermQuery; | ||
import org.apache.lucene.search.WildcardQuery; | ||
import org.apache.lucene.search.intervals.IntervalsSource; | ||
import org.elasticsearch.Version; | ||
import org.elasticsearch.common.collect.Iterators; | ||
|
@@ -360,7 +361,7 @@ PrefixFieldType setAnalyzer(NamedAnalyzer delegate) { | |
} | ||
|
||
boolean accept(int length) { | ||
return length >= minChars && length <= maxChars; | ||
return length >= minChars - 1 && length <= maxChars; | ||
} | ||
|
||
void doXContent(XContentBuilder builder) throws IOException { | ||
|
@@ -370,6 +371,17 @@ void doXContent(XContentBuilder builder) throws IOException { | |
builder.endObject(); | ||
} | ||
|
||
public Query termQuery(Object value, MultiTermQuery.RewriteMethod method, QueryShardContext context) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we should rename this method, now that it is not a pure term query (for example There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I made this just override |
||
assert value instanceof String; | ||
String strValue = (String) value; | ||
if (strValue.length() >= minChars) { | ||
return super.termQuery(value, context); | ||
} | ||
WildcardQuery query = new WildcardQuery(new Term(name(), value + "?")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it'd be safer to create an automaton manually and then instantiate an AutomatonQuery, otherwise there could be surprises if |
||
query.setRewriteMethod(method); | ||
return query; | ||
} | ||
|
||
@Override | ||
public PrefixFieldType clone() { | ||
return new PrefixFieldType(name(), minChars, maxChars); | ||
|
@@ -402,7 +414,6 @@ public boolean equals(Object o) { | |
|
||
@Override | ||
public int hashCode() { | ||
|
||
return Objects.hash(super.hashCode(), minChars, maxChars); | ||
} | ||
} | ||
|
@@ -564,7 +575,7 @@ public Query prefixQuery(String value, MultiTermQuery.RewriteMethod method, Quer | |
if (prefixFieldType == null || prefixFieldType.accept(value.length()) == false) { | ||
return super.prefixQuery(value, method, context); | ||
} | ||
Query tq = prefixFieldType.termQuery(value, context); | ||
Query tq = prefixFieldType.termQuery(value, method, context); | ||
if (method == null || method == MultiTermQuery.CONSTANT_SCORE_REWRITE | ||
|| method == MultiTermQuery.CONSTANT_SCORE_BOOLEAN_REWRITE) { | ||
return new ConstantScoreQuery(tq); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,7 @@ | |
import org.apache.lucene.search.PrefixQuery; | ||
import org.apache.lucene.search.Query; | ||
import org.apache.lucene.search.TermQuery; | ||
import org.apache.lucene.search.WildcardQuery; | ||
import org.apache.lucene.util.BytesRef; | ||
import org.elasticsearch.Version; | ||
import org.elasticsearch.action.index.IndexRequest; | ||
|
@@ -828,7 +829,7 @@ public void testIndexPrefixMapping() throws IOException { | |
.field("type", "text") | ||
.field("analyzer", "standard") | ||
.startObject("index_prefixes") | ||
.field("min_chars", 1) | ||
.field("min_chars", 2) | ||
.field("max_chars", 10) | ||
.endObject() | ||
.endObject().endObject() | ||
|
@@ -837,17 +838,20 @@ public void testIndexPrefixMapping() throws IOException { | |
DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping)); | ||
assertEquals(mapping, mapper.mappingSource().toString()); | ||
|
||
assertThat(mapper.mappers().getMapper("field._index_prefix").toString(), containsString("prefixChars=1:10")); | ||
assertThat(mapper.mappers().getMapper("field._index_prefix").toString(), containsString("prefixChars=2:10")); | ||
|
||
FieldMapper fieldMapper = (FieldMapper) mapper.mappers().getMapper("field"); | ||
MappedFieldType fieldType = fieldMapper.fieldType; | ||
|
||
Query q = fieldType.prefixQuery("goin", CONSTANT_SCORE_REWRITE, queryShardContext); | ||
|
||
assertEquals(new ConstantScoreQuery(new TermQuery(new Term("field._index_prefix", "goin"))), q); | ||
|
||
q = fieldType.prefixQuery("internationalisatio", CONSTANT_SCORE_REWRITE, queryShardContext); | ||
assertEquals(new PrefixQuery(new Term("field", "internationalisatio")), q); | ||
|
||
q = fieldType.prefixQuery("g", CONSTANT_SCORE_REWRITE, queryShardContext); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems like these detailed query construction tests would fit better in a unit test like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ++ |
||
assertEquals(new ConstantScoreQuery(new WildcardQuery(new Term("field._index_prefix", "g?"))), q); | ||
|
||
ParsedDocument doc = mapper.parse(SourceToParse.source("test", "type", "1", BytesReference | ||
.bytes(XContentFactory.jsonBuilder() | ||
.startObject() | ||
|
@@ -874,7 +878,8 @@ public void testIndexPrefixMapping() throws IOException { | |
MappedFieldType fieldType = fieldMapper.fieldType; | ||
|
||
Query q1 = fieldType.prefixQuery("g", CONSTANT_SCORE_REWRITE, queryShardContext); | ||
assertThat(q1, instanceOf(PrefixQuery.class)); | ||
assertThat(q1, instanceOf(ConstantScoreQuery.class)); | ||
assertThat(((ConstantScoreQuery)q1).getQuery(), instanceOf(WildcardQuery.class)); | ||
Query q2 = fieldType.prefixQuery("go", CONSTANT_SCORE_REWRITE, queryShardContext); | ||
assertThat(q2, instanceOf(ConstantScoreQuery.class)); | ||
Query q5 = fieldType.prefixQuery("going", CONSTANT_SCORE_REWRITE, queryShardContext); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's go even further, change this to just
length <= maxChars
, and then appendminChars - prefixTerm.length
wildcards to the automaton that is used for querying?