Analyzers for FLT / MLT, closes #1031.

This commit is contained in:
kimchy 2011-06-15 13:43:10 +03:00
parent 11f19ff6da
commit 64bf849cb1
8 changed files with 82 additions and 7 deletions

View File

@ -37,6 +37,7 @@ public class FuzzyLikeThisFieldQueryBuilder extends BaseQueryBuilder {
private Integer prefixLength;
private Integer maxQueryTerms;
private Boolean ignoreTF;
private String analyzer;
/**
* A fuzzy more like this query on the provided field.
@ -75,6 +76,14 @@ public class FuzzyLikeThisFieldQueryBuilder extends BaseQueryBuilder {
return this;
}
/**
* The analyzer that will be used to analyze the text. Defaults to the analyzer associated with the field.
*/
public FuzzyLikeThisFieldQueryBuilder analyzer(String analyzer) {
this.analyzer = analyzer;
return this;
}
public FuzzyLikeThisFieldQueryBuilder boost(float boost) {
this.boost = boost;
return this;
@ -102,6 +111,9 @@ public class FuzzyLikeThisFieldQueryBuilder extends BaseQueryBuilder {
if (boost != null) {
builder.field("boost", boost);
}
if (analyzer != null) {
builder.field("analyzer", analyzer);
}
builder.endObject();
builder.endObject();
}

View File

@ -65,6 +65,7 @@ public class FuzzyLikeThisFieldQueryParser implements QueryParser {
float minSimilarity = 0.5f;
int prefixLength = 0;
boolean ignoreTF = false;
Analyzer analyzer = null;
XContentParser.Token token = parser.nextToken();
assert token == XContentParser.Token.FIELD_NAME;
@ -92,6 +93,8 @@ public class FuzzyLikeThisFieldQueryParser implements QueryParser {
minSimilarity = parser.floatValue();
} else if ("prefix_length".equals(currentFieldName) || "prefixLength".equals(currentFieldName)) {
prefixLength = parser.intValue();
} else if ("analyzer".equals(currentFieldName)) {
analyzer = parseContext.analysisService().analyzer(parser.text());
}
}
}
@ -100,12 +103,13 @@ public class FuzzyLikeThisFieldQueryParser implements QueryParser {
throw new QueryParsingException(parseContext.index(), "fuzzy_like_This_field requires 'like_text' to be specified");
}
Analyzer analyzer = null;
MapperService.SmartNameFieldMappers smartNameFieldMappers = parseContext.smartFieldMappers(fieldName);
if (smartNameFieldMappers != null) {
if (smartNameFieldMappers.hasMapper()) {
fieldName = smartNameFieldMappers.mapper().names().indexName();
analyzer = smartNameFieldMappers.mapper().searchAnalyzer();
if (analyzer == null) {
analyzer = smartNameFieldMappers.mapper().searchAnalyzer();
}
}
}
if (analyzer == null) {

View File

@ -37,6 +37,7 @@ public class FuzzyLikeThisQueryBuilder extends BaseQueryBuilder {
private Integer prefixLength;
private Integer maxQueryTerms;
private Boolean ignoreTF;
private String analyzer;
/**
* Constructs a new fuzzy like this query which uses the "_all" field.
@ -82,6 +83,14 @@ public class FuzzyLikeThisQueryBuilder extends BaseQueryBuilder {
return this;
}
/**
* The analyzer that will be used to analyze the text. Defaults to the analyzer associated with the fied.
*/
public FuzzyLikeThisQueryBuilder analyzer(String analyzer) {
this.analyzer = analyzer;
return this;
}
public FuzzyLikeThisQueryBuilder boost(float boost) {
this.boost = boost;
return this;
@ -115,6 +124,9 @@ public class FuzzyLikeThisQueryBuilder extends BaseQueryBuilder {
if (boost != null) {
builder.field("boost", boost);
}
if (analyzer != null) {
builder.field("analyzer", analyzer);
}
builder.endObject();
}
}

View File

@ -19,6 +19,7 @@
package org.elasticsearch.index.query;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.search.FuzzyLikeThisQuery;
import org.apache.lucene.search.Query;
import org.elasticsearch.common.collect.Lists;
@ -64,6 +65,7 @@ public class FuzzyLikeThisQueryParser implements QueryParser {
float minSimilarity = 0.5f;
int prefixLength = 0;
boolean ignoreTF = false;
Analyzer analyzer = null;
XContentParser.Token token;
String currentFieldName = null;
@ -83,6 +85,8 @@ public class FuzzyLikeThisQueryParser implements QueryParser {
minSimilarity = parser.floatValue();
} else if ("prefix_length".equals(currentFieldName) || "prefixLength".equals(currentFieldName)) {
prefixLength = parser.intValue();
} else if ("analyzer".equals(currentFieldName)) {
analyzer = parseContext.analysisService().analyzer(parser.text());
}
} else if (token == XContentParser.Token.START_ARRAY) {
if ("fields".equals(currentFieldName)) {
@ -98,7 +102,11 @@ public class FuzzyLikeThisQueryParser implements QueryParser {
throw new QueryParsingException(parseContext.index(), "fuzzy_like_this requires 'like_text' to be specified");
}
FuzzyLikeThisQuery query = new FuzzyLikeThisQuery(maxNumTerms, parseContext.mapperService().searchAnalyzer());
if (analyzer == null) {
analyzer = parseContext.mapperService().searchAnalyzer();
}
FuzzyLikeThisQuery query = new FuzzyLikeThisQuery(maxNumTerms, analyzer);
if (fields == null) {
// add the default _all field
query.addTerms(likeText, AllFieldMapper.NAME, minSimilarity, prefixLength);

View File

@ -43,6 +43,7 @@ public class MoreLikeThisFieldQueryBuilder extends BaseQueryBuilder {
private int maxWordLen = -1;
private float boostTerms = -1;
private float boost = -1;
private String analyzer;
/**
* A more like this query that runs against a specific field.
@ -143,6 +144,14 @@ public class MoreLikeThisFieldQueryBuilder extends BaseQueryBuilder {
return this;
}
/**
* The analyzer that will be used to analyze the text. Defaults to the analyzer associated with the fied.
*/
public MoreLikeThisFieldQueryBuilder analyzer(String analyzer) {
this.analyzer = analyzer;
return this;
}
public MoreLikeThisFieldQueryBuilder boost(float boost) {
this.boost = boost;
return this;
@ -189,6 +198,9 @@ public class MoreLikeThisFieldQueryBuilder extends BaseQueryBuilder {
if (boost != -1) {
builder.field("boost", boost);
}
if (analyzer != null) {
builder.field("analyzer", analyzer);
}
builder.endObject();
builder.endObject();
}

View File

@ -19,6 +19,7 @@
package org.elasticsearch.index.query;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.search.Query;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.collect.Sets;
@ -60,6 +61,7 @@ public class MoreLikeThisFieldQueryParser implements QueryParser {
MoreLikeThisQuery mltQuery = new MoreLikeThisQuery();
mltQuery.setSimilarity(parseContext.searchSimilarity());
Analyzer analyzer = null;
String currentFieldName = null;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
@ -85,6 +87,8 @@ public class MoreLikeThisFieldQueryParser implements QueryParser {
mltQuery.setBoostTermsFactor(parser.floatValue());
} else if ("percent_terms_to_match".equals(currentFieldName) || "percentTermsToMatch".equals(currentFieldName)) {
mltQuery.setPercentTermsToMatch(parser.floatValue());
} else if ("analyzer".equals(currentFieldName)) {
analyzer = parseContext.analysisService().analyzer(parser.text());
}
} else if (token == XContentParser.Token.START_ARRAY) {
if ("stop_words".equals(currentFieldName) || "stopWords".equals(currentFieldName)) {
@ -109,12 +113,15 @@ public class MoreLikeThisFieldQueryParser implements QueryParser {
if (smartNameFieldMappers != null) {
if (smartNameFieldMappers.hasMapper()) {
fieldName = smartNameFieldMappers.mapper().names().indexName();
mltQuery.setAnalyzer(smartNameFieldMappers.mapper().searchAnalyzer());
if (analyzer == null) {
analyzer = smartNameFieldMappers.mapper().searchAnalyzer();
}
}
}
if (mltQuery.getAnalyzer() == null) {
mltQuery.setAnalyzer(parseContext.mapperService().searchAnalyzer());
if (analyzer == null) {
analyzer = parseContext.mapperService().searchAnalyzer();
}
mltQuery.setAnalyzer(analyzer);
mltQuery.setMoreLikeFields(new String[]{fieldName});
return wrapSmartNameQuery(mltQuery, smartNameFieldMappers, parseContext);
}

View File

@ -44,6 +44,7 @@ public class MoreLikeThisQueryBuilder extends BaseQueryBuilder {
private int maxWordLen = -1;
private float boostTerms = -1;
private float boost = -1;
private String analyzer;
/**
* Constructs a new more like this query which uses the "_all" field.
@ -151,6 +152,14 @@ public class MoreLikeThisQueryBuilder extends BaseQueryBuilder {
return this;
}
/**
* The analyzer that will be used to analyze the text. Defaults to the analyzer associated with the fied.
*/
public MoreLikeThisQueryBuilder analyzer(String analyzer) {
this.analyzer = analyzer;
return this;
}
public MoreLikeThisQueryBuilder boost(float boost) {
this.boost = boost;
return this;
@ -203,6 +212,9 @@ public class MoreLikeThisQueryBuilder extends BaseQueryBuilder {
if (boost != -1) {
builder.field("boost", boost);
}
if (analyzer != null) {
builder.field("analyzer", analyzer);
}
builder.endObject();
}
}

View File

@ -19,6 +19,7 @@
package org.elasticsearch.index.query;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.search.Query;
import org.elasticsearch.common.collect.Lists;
import org.elasticsearch.common.collect.Sets;
@ -51,6 +52,7 @@ public class MoreLikeThisQueryParser implements QueryParser {
MoreLikeThisQuery mltQuery = new MoreLikeThisQuery();
mltQuery.setMoreLikeFields(new String[]{AllFieldMapper.NAME});
mltQuery.setSimilarity(parseContext.searchSimilarity());
Analyzer analyzer = null;
XContentParser.Token token;
String currentFieldName = null;
@ -77,6 +79,8 @@ public class MoreLikeThisQueryParser implements QueryParser {
mltQuery.setBoostTermsFactor(parser.floatValue());
} else if ("percent_terms_to_match".equals(currentFieldName) || "percentTermsToMatch".equals(currentFieldName)) {
mltQuery.setPercentTermsToMatch(parser.floatValue());
} else if ("analyzer".equals(currentFieldName)) {
analyzer = parseContext.analysisService().analyzer(parser.text());
}
} else if (token == XContentParser.Token.START_ARRAY) {
if ("stop_words".equals(currentFieldName) || "stopWords".equals(currentFieldName)) {
@ -102,7 +106,11 @@ public class MoreLikeThisQueryParser implements QueryParser {
throw new QueryParsingException(parseContext.index(), "more_like_this requires 'fields' to be specified");
}
mltQuery.setAnalyzer(parseContext.mapperService().searchAnalyzer());
if (analyzer == null) {
analyzer = parseContext.mapperService().searchAnalyzer();
}
mltQuery.setAnalyzer(analyzer);
return mltQuery;
}
}