Query: Add `index.query.default_field` allowing to control the default field used to search on, closes #1657.

This commit is contained in:
Shay Banon 2012-02-02 00:49:34 +02:00
parent 8b9c75cd7e
commit 5a1cbe0c83
6 changed files with 22 additions and 7 deletions

View File

@ -41,6 +41,7 @@ import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.analysis.*;
import org.elasticsearch.index.mapper.FieldMapper;
import org.elasticsearch.index.mapper.internal.AllFieldMapper;
import org.elasticsearch.index.service.IndexService;
import org.elasticsearch.indices.IndicesService;
import org.elasticsearch.indices.analysis.IndicesAnalysisService;
@ -130,7 +131,11 @@ public class TransportAnalyzeAction extends TransportSingleCustomOperationAction
}
}
if (field == null) {
field = "_all";
if (indexService != null) {
field = indexService.queryParserService().defaultField();
} else {
field = AllFieldMapper.NAME;
}
}
if (analyzer == null && request.analyzer() != null) {
if (indexService == null) {

View File

@ -25,7 +25,6 @@ import org.apache.lucene.search.FuzzyLikeThisQuery;
import org.apache.lucene.search.Query;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.mapper.internal.AllFieldMapper;
import java.io.IOException;
import java.util.List;
@ -114,7 +113,7 @@ public class FuzzyLikeThisQueryParser implements QueryParser {
FuzzyLikeThisQuery query = new FuzzyLikeThisQuery(maxNumTerms, analyzer);
if (fields == null) {
// add the default _all field
query.addTerms(likeText, AllFieldMapper.NAME, minSimilarity, prefixLength);
query.addTerms(likeText, parseContext.defaultField(), minSimilarity, prefixLength);
} else {
for (String field : fields) {
query.addTerms(likeText, field, minSimilarity, prefixLength);

View File

@ -35,6 +35,7 @@ import org.elasticsearch.index.analysis.AnalysisService;
import org.elasticsearch.index.cache.IndexCache;
import org.elasticsearch.index.engine.IndexEngine;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.index.mapper.internal.AllFieldMapper;
import org.elasticsearch.index.settings.IndexSettings;
import org.elasticsearch.index.similarity.SimilarityService;
import org.elasticsearch.indices.query.IndicesQueriesRegistry;
@ -81,6 +82,8 @@ public class IndexQueryParserService extends AbstractIndexComponent {
private final Map<String, FilterParser> filterParsers;
private String defaultField;
@Inject
public IndexQueryParserService(Index index, @IndexSettings Settings indexSettings,
IndicesQueriesRegistry indicesQueriesRegistry,
@ -97,6 +100,8 @@ public class IndexQueryParserService extends AbstractIndexComponent {
this.indexCache = indexCache;
this.indexEngine = indexEngine;
this.defaultField = indexSettings.get("index.query.default_field", AllFieldMapper.NAME);
List<QueryParser> queryParsers = newArrayList();
if (namedQueryParsers != null) {
Map<String, Settings> queryParserGroups = indexSettings.getGroups(IndexQueryParserService.Defaults.QUERY_PREFIX);
@ -148,6 +153,10 @@ public class IndexQueryParserService extends AbstractIndexComponent {
cache.remove();
}
public String defaultField() {
return this.defaultField;
}
public QueryParser queryParser(String name) {
return queryParsers.get(name);
}

View File

@ -26,7 +26,6 @@ import org.apache.lucene.search.Query;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.lucene.search.MoreLikeThisQuery;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.mapper.internal.AllFieldMapper;
import java.io.IOException;
import java.util.List;
@ -53,7 +52,7 @@ public class MoreLikeThisQueryParser implements QueryParser {
XContentParser parser = parseContext.parser();
MoreLikeThisQuery mltQuery = new MoreLikeThisQuery();
mltQuery.setMoreLikeFields(new String[]{AllFieldMapper.NAME});
mltQuery.setMoreLikeFields(new String[]{parseContext.defaultField()});
mltQuery.setSimilarity(parseContext.searchSimilarity());
Analyzer analyzer = null;

View File

@ -130,6 +130,10 @@ public class QueryParseContext {
return indexQueryParser.indexCache;
}
public String defaultField() {
return indexQueryParser.defaultField();
}
public MapperQueryParser singleQueryParser(QueryParserSettings settings) {
queryParser.reset(settings);
return queryParser;

View File

@ -34,7 +34,6 @@ import org.elasticsearch.common.lucene.search.Queries;
import org.elasticsearch.common.regex.Regex;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.mapper.internal.AllFieldMapper;
import org.elasticsearch.index.query.support.QueryParsers;
import java.io.IOException;
@ -68,7 +67,7 @@ public class QueryStringQueryParser implements QueryParser {
XContentParser parser = parseContext.parser();
MultiFieldQueryParserSettings qpSettings = new MultiFieldQueryParserSettings();
qpSettings.defaultField(AllFieldMapper.NAME);
qpSettings.defaultField(parseContext.defaultField());
qpSettings.analyzeWildcard(defaultAnalyzeWildcard);
qpSettings.allowLeadingWildcard(defaultAllowLeadingWildcard);