SimpleQueryParser should call MappedFieldType.termQuery when appropriate. #17678

This will allow to use it on virtual fields like `_index` or `_id` which do not
exist in the index.
This commit is contained in:
Colin Goodheart-Smithe 2016-04-12 10:32:20 +01:00 committed by Adrien Grand
parent 2ed7769ddc
commit f5bade1a0d
4 changed files with 37 additions and 3 deletions

View File

@ -30,6 +30,7 @@ import org.apache.lucene.search.FuzzyQuery;
import org.apache.lucene.search.PrefixQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.index.mapper.MappedFieldType;
import java.io.IOException;
import java.util.Locale;
@ -43,11 +44,13 @@ import java.util.Objects;
public class SimpleQueryParser extends org.apache.lucene.queryparser.simple.SimpleQueryParser {
private final Settings settings;
private QueryShardContext context;
/** Creates a new parser with custom flags used to enable/disable certain features. */
public SimpleQueryParser(Analyzer analyzer, Map<String, Float> weights, int flags, Settings settings) {
public SimpleQueryParser(Analyzer analyzer, Map<String, Float> weights, int flags, Settings settings, QueryShardContext context) {
super(analyzer, weights, flags);
this.settings = settings;
this.context = context;
}
/**
@ -60,6 +63,15 @@ public class SimpleQueryParser extends org.apache.lucene.queryparser.simple.Simp
throw e;
}
@Override
protected Query newTermQuery(Term term) {
MappedFieldType currentFieldType = context.fieldMapper(term.field());
if (currentFieldType == null || currentFieldType.tokenized()) {
return super.newTermQuery(term);
}
return currentFieldType.termQuery(term.bytes(), context);
}
@Override
public Query newDefaultQuery(String text) {
BooleanQuery.Builder bq = new BooleanQuery.Builder();

View File

@ -363,7 +363,7 @@ public class SimpleQueryStringBuilder extends AbstractQueryBuilder<SimpleQuerySt
}
SimpleQueryParser sqp = new SimpleQueryParser(luceneAnalyzer, resolvedFieldsAndWeights, flags, settings);
SimpleQueryParser sqp = new SimpleQueryParser(luceneAnalyzer, resolvedFieldsAndWeights, flags, settings, context);
sqp.setDefaultOperator(defaultOperator.toBooleanClauseOccur());
Query query = sqp.parse(queryText);

View File

@ -23,10 +23,10 @@ import org.apache.lucene.index.Term;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.BoostQuery;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.MatchNoDocsQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;
import org.elasticsearch.Version;
import org.elasticsearch.cluster.metadata.MetaData;
import java.io.IOException;
@ -396,4 +396,15 @@ public class SimpleQueryStringBuilderTests extends AbstractQueryTestCase<SimpleQ
assertEquals(expectedMinimumShouldMatch, boolQuery.getMinimumNumberShouldMatch());
}
}
public void testIndexMetaField() throws IOException {
QueryShardContext shardContext = createShardContext();
SimpleQueryStringBuilder simpleQueryStringBuilder = new SimpleQueryStringBuilder(getIndex().getName());
simpleQueryStringBuilder.field("_index");
Query query = simpleQueryStringBuilder.toQuery(shardContext);
assertThat(query, notNullValue());
if (getCurrentTypes().length > 0) {
assertThat(query, instanceOf(MatchAllDocsQuery.class));
}
}
}

View File

@ -359,4 +359,15 @@ public class SimpleQueryStringIT extends ESIntegTestCase {
assertHitCount(searchResponse, 1L);
assertSearchHits(searchResponse, "1");
}
public void testSimpleQueryStringOnIndexMetaField() throws Exception {
client().prepareIndex("test", "type1", "1").setSource("foo", 123, "bar", "abc").get();
client().prepareIndex("test", "type1", "2").setSource("foo", 234, "bar", "bcd").get();
refresh();
SearchResponse searchResponse = client().prepareSearch().setQuery(simpleQueryStringQuery("test").field("_index")).get();
assertHitCount(searchResponse, 2L);
assertSearchHits(searchResponse, "1", "2");
}
}