query parser should take into account using all, since all creates a specialized "term query" which boosts based on the boost level associated with a term. Generalized it so mappers can control when query is used for term query.

This commit is contained in:
kimchy 2010-03-18 00:53:36 +02:00
parent a8be04b334
commit 1107df9ab6
4 changed files with 55 additions and 13 deletions

View File

@ -22,6 +22,7 @@ package org.elasticsearch.index.mapper;
import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Field; import org.apache.lucene.document.Field;
import org.apache.lucene.document.Fieldable; import org.apache.lucene.document.Fieldable;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.Filter; import org.apache.lucene.search.Filter;
import org.apache.lucene.search.Query; import org.apache.lucene.search.Query;
import org.apache.lucene.util.StringHelper; import org.apache.lucene.util.StringHelper;
@ -143,8 +144,16 @@ public interface FieldMapper<T> {
*/ */
boolean useFieldQueryWithQueryString(); boolean useFieldQueryWithQueryString();
/**
* A field query for the specified value.
*/
Query fieldQuery(String value); Query fieldQuery(String value);
/**
* A term query to use when parsing a query string. Can return <tt>null</tt>.
*/
Query queryStringTermQuery(Term term);
Filter fieldFilter(String value); Filter fieldFilter(String value);
/** /**

View File

@ -23,6 +23,8 @@ import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.document.Field; import org.apache.lucene.document.Field;
import org.apache.lucene.document.Fieldable; import org.apache.lucene.document.Fieldable;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.Query;
import org.elasticsearch.index.analysis.NamedAnalyzer; import org.elasticsearch.index.analysis.NamedAnalyzer;
import org.elasticsearch.index.mapper.AllFieldMapper; import org.elasticsearch.index.mapper.AllFieldMapper;
import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.DocumentMapper;
@ -30,6 +32,7 @@ import org.elasticsearch.index.mapper.MergeMappingException;
import org.elasticsearch.util.json.JsonBuilder; import org.elasticsearch.util.json.JsonBuilder;
import org.elasticsearch.util.lucene.Lucene; import org.elasticsearch.util.lucene.Lucene;
import org.elasticsearch.util.lucene.all.AllAnalyzer; import org.elasticsearch.util.lucene.all.AllAnalyzer;
import org.elasticsearch.util.lucene.all.AllTermQuery;
import java.io.IOException; import java.io.IOException;
@ -106,6 +109,10 @@ public class JsonAllFieldMapper extends JsonFieldMapper<Void> implements AllFiel
return this.enabled; return this.enabled;
} }
@Override public Query queryStringTermQuery(Term term) {
return new AllTermQuery(term);
}
@Override protected Field parseCreateField(JsonParseContext jsonContext) throws IOException { @Override protected Field parseCreateField(JsonParseContext jsonContext) throws IOException {
if (!enabled) { if (!enabled) {
return null; return null;

View File

@ -308,6 +308,10 @@ public abstract class JsonFieldMapper<T> implements FieldMapper<T>, JsonMapper {
return new TermQuery(new Term(names.indexName(), indexedValue(value))); return new TermQuery(new Term(names.indexName(), indexedValue(value)));
} }
@Override public Query queryStringTermQuery(Term term) {
return null;
}
@Override public Filter fieldFilter(String value) { @Override public Filter fieldFilter(String value) {
return new TermFilter(new Term(names.indexName(), indexedValue(value))); return new TermFilter(new Term(names.indexName(), indexedValue(value)));
} }

View File

@ -20,6 +20,7 @@
package org.elasticsearch.index.query.support; package org.elasticsearch.index.query.support;
import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.ParseException; import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser; import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.BooleanClause; import org.apache.lucene.search.BooleanClause;
@ -52,6 +53,8 @@ public class MapperQueryParser extends QueryParser {
private final FilterCache filterCache; private final FilterCache filterCache;
private FieldMapper currentMapper;
public MapperQueryParser(String defaultField, Analyzer analyzer, public MapperQueryParser(String defaultField, Analyzer analyzer,
@Nullable MapperService mapperService, @Nullable MapperService mapperService,
@Nullable FilterCache filterCache) { @Nullable FilterCache filterCache) {
@ -61,17 +64,28 @@ public class MapperQueryParser extends QueryParser {
setMultiTermRewriteMethod(MultiTermQuery.CONSTANT_SCORE_AUTO_REWRITE_DEFAULT); setMultiTermRewriteMethod(MultiTermQuery.CONSTANT_SCORE_AUTO_REWRITE_DEFAULT);
} }
@Override protected Query newTermQuery(Term term) {
if (currentMapper != null) {
Query termQuery = currentMapper.queryStringTermQuery(term);
if (termQuery != null) {
return termQuery;
}
}
return super.newTermQuery(term);
}
@Override public Query getFieldQuery(String field, String queryText) throws ParseException { @Override public Query getFieldQuery(String field, String queryText) throws ParseException {
currentMapper = null;
if (mapperService != null) { if (mapperService != null) {
MapperService.SmartNameFieldMappers fieldMappers = mapperService.smartName(field); MapperService.SmartNameFieldMappers fieldMappers = mapperService.smartName(field);
if (fieldMappers != null) { if (fieldMappers != null) {
FieldMapper mapper = fieldMappers.fieldMappers().mapper(); currentMapper = fieldMappers.fieldMappers().mapper();
if (mapper != null) { if (currentMapper != null) {
Query query; Query query;
if (mapper.useFieldQueryWithQueryString()) { if (currentMapper.useFieldQueryWithQueryString()) {
query = fieldMappers.fieldMappers().mapper().fieldQuery(queryText); query = currentMapper.fieldQuery(queryText);
} else { } else {
query = super.getFieldQuery(mapper.names().indexName(), queryText); query = super.getFieldQuery(currentMapper.names().indexName(), queryText);
} }
return wrapSmartNameQuery(query, fieldMappers, filterCache); return wrapSmartNameQuery(query, fieldMappers, filterCache);
} }
@ -87,11 +101,13 @@ public class MapperQueryParser extends QueryParser {
if ("*".equals(part2)) { if ("*".equals(part2)) {
part2 = null; part2 = null;
} }
currentMapper = null;
if (mapperService != null) { if (mapperService != null) {
MapperService.SmartNameFieldMappers fieldMappers = mapperService.smartName(field); MapperService.SmartNameFieldMappers fieldMappers = mapperService.smartName(field);
if (fieldMappers != null) { if (fieldMappers != null) {
if (fieldMappers.fieldMappers().mapper() != null) { currentMapper = fieldMappers.fieldMappers().mapper();
Query rangeQuery = fieldMappers.fieldMappers().mapper().rangeQuery(part1, part2, inclusive, inclusive); if (currentMapper != null) {
Query rangeQuery = currentMapper.rangeQuery(part1, part2, inclusive, inclusive);
return wrapSmartNameQuery(rangeQuery, fieldMappers, filterCache); return wrapSmartNameQuery(rangeQuery, fieldMappers, filterCache);
} }
} }
@ -101,11 +117,13 @@ public class MapperQueryParser extends QueryParser {
@Override protected Query getPrefixQuery(String field, String termStr) throws ParseException { @Override protected Query getPrefixQuery(String field, String termStr) throws ParseException {
String indexedNameField = field; String indexedNameField = field;
currentMapper = null;
if (mapperService != null) { if (mapperService != null) {
MapperService.SmartNameFieldMappers fieldMappers = mapperService.smartName(field); MapperService.SmartNameFieldMappers fieldMappers = mapperService.smartName(field);
if (fieldMappers != null) { if (fieldMappers != null) {
if (fieldMappers.fieldMappers().mapper() != null) { currentMapper = fieldMappers.fieldMappers().mapper();
indexedNameField = fieldMappers.fieldMappers().mapper().names().indexName(); if (currentMapper != null) {
indexedNameField = currentMapper.names().indexName();
} }
return wrapSmartNameQuery(super.getPrefixQuery(indexedNameField, termStr), fieldMappers, filterCache); return wrapSmartNameQuery(super.getPrefixQuery(indexedNameField, termStr), fieldMappers, filterCache);
} }
@ -115,11 +133,13 @@ public class MapperQueryParser extends QueryParser {
@Override protected Query getFuzzyQuery(String field, String termStr, float minSimilarity) throws ParseException { @Override protected Query getFuzzyQuery(String field, String termStr, float minSimilarity) throws ParseException {
String indexedNameField = field; String indexedNameField = field;
currentMapper = null;
if (mapperService != null) { if (mapperService != null) {
MapperService.SmartNameFieldMappers fieldMappers = mapperService.smartName(field); MapperService.SmartNameFieldMappers fieldMappers = mapperService.smartName(field);
if (fieldMappers != null) { if (fieldMappers != null) {
if (fieldMappers.fieldMappers().mapper() != null) { currentMapper = fieldMappers.fieldMappers().mapper();
indexedNameField = fieldMappers.fieldMappers().mapper().names().indexName(); if (currentMapper != null) {
indexedNameField = currentMapper.names().indexName();
} }
return wrapSmartNameQuery(super.getFuzzyQuery(indexedNameField, termStr, minSimilarity), fieldMappers, filterCache); return wrapSmartNameQuery(super.getFuzzyQuery(indexedNameField, termStr, minSimilarity), fieldMappers, filterCache);
} }
@ -129,11 +149,13 @@ public class MapperQueryParser extends QueryParser {
@Override protected Query getWildcardQuery(String field, String termStr) throws ParseException { @Override protected Query getWildcardQuery(String field, String termStr) throws ParseException {
String indexedNameField = field; String indexedNameField = field;
currentMapper = null;
if (mapperService != null) { if (mapperService != null) {
MapperService.SmartNameFieldMappers fieldMappers = mapperService.smartName(field); MapperService.SmartNameFieldMappers fieldMappers = mapperService.smartName(field);
if (fieldMappers != null) { if (fieldMappers != null) {
if (fieldMappers.fieldMappers().mapper() != null) { currentMapper = fieldMappers.fieldMappers().mapper();
indexedNameField = fieldMappers.fieldMappers().mapper().names().indexName(); if (currentMapper != null) {
indexedNameField = currentMapper.names().indexName();
} }
return wrapSmartNameQuery(super.getWildcardQuery(indexedNameField, termStr), fieldMappers, filterCache); return wrapSmartNameQuery(super.getWildcardQuery(indexedNameField, termStr), fieldMappers, filterCache);
} }