Skip to content

Commit

Permalink
Query: Add index.query.default_field allowing to control the defaul…
Browse files Browse the repository at this point in the history
…t field used to search on, closes #1657.
  • Loading branch information
kimchy committed Feb 1, 2012
1 parent 8b9c75c commit 5a1cbe0
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 7 deletions.
Expand Up @@ -41,6 +41,7 @@
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;
Expand Down Expand Up @@ -130,7 +131,11 @@ protected AnalyzeResponse shardOperation(AnalyzeRequest request, int shardId) th
}
}
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) {
Expand Down
Expand Up @@ -25,7 +25,6 @@
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;
Expand Down Expand Up @@ -114,7 +113,7 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars
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);
Expand Down
Expand Up @@ -35,6 +35,7 @@
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;
Expand Down Expand Up @@ -81,6 +82,8 @@ protected QueryParseContext initialValue() {

private final Map<String, FilterParser> filterParsers;

private String defaultField;

@Inject
public IndexQueryParserService(Index index, @IndexSettings Settings indexSettings,
IndicesQueriesRegistry indicesQueriesRegistry,
Expand All @@ -97,6 +100,8 @@ public IndexQueryParserService(Index index, @IndexSettings Settings indexSetting
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);
Expand Down Expand Up @@ -148,6 +153,10 @@ public void close() {
cache.remove();
}

public String defaultField() {
return this.defaultField;
}

public QueryParser queryParser(String name) {
return queryParsers.get(name);
}
Expand Down
Expand Up @@ -26,7 +26,6 @@
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;
Expand All @@ -53,7 +52,7 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars
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;

Expand Down
Expand Up @@ -130,6 +130,10 @@ public IndexCache indexCache() {
return indexQueryParser.indexCache;
}

public String defaultField() {
return indexQueryParser.defaultField();
}

public MapperQueryParser singleQueryParser(QueryParserSettings settings) {
queryParser.reset(settings);
return queryParser;
Expand Down
Expand Up @@ -34,7 +34,6 @@
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;
Expand Down Expand Up @@ -68,7 +67,7 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars
XContentParser parser = parseContext.parser();

MultiFieldQueryParserSettings qpSettings = new MultiFieldQueryParserSettings();
qpSettings.defaultField(AllFieldMapper.NAME);
qpSettings.defaultField(parseContext.defaultField());
qpSettings.analyzeWildcard(defaultAnalyzeWildcard);
qpSettings.allowLeadingWildcard(defaultAllowLeadingWildcard);

Expand Down

0 comments on commit 5a1cbe0

Please sign in to comment.