From 64bf849cb14c6c378206ae1702d931137a3cf365 Mon Sep 17 00:00:00 2001 From: kimchy Date: Wed, 15 Jun 2011 13:43:10 +0300 Subject: [PATCH] Analyzers for FLT / MLT, closes #1031. --- .../index/query/FuzzyLikeThisFieldQueryBuilder.java | 12 ++++++++++++ .../index/query/FuzzyLikeThisFieldQueryParser.java | 8 ++++++-- .../index/query/FuzzyLikeThisQueryBuilder.java | 12 ++++++++++++ .../index/query/FuzzyLikeThisQueryParser.java | 10 +++++++++- .../index/query/MoreLikeThisFieldQueryBuilder.java | 12 ++++++++++++ .../index/query/MoreLikeThisFieldQueryParser.java | 13 ++++++++++--- .../index/query/MoreLikeThisQueryBuilder.java | 12 ++++++++++++ .../index/query/MoreLikeThisQueryParser.java | 10 +++++++++- 8 files changed, 82 insertions(+), 7 deletions(-) diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/FuzzyLikeThisFieldQueryBuilder.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/FuzzyLikeThisFieldQueryBuilder.java index 60986fe6c66..7c43297ccae 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/FuzzyLikeThisFieldQueryBuilder.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/FuzzyLikeThisFieldQueryBuilder.java @@ -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(); } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/FuzzyLikeThisFieldQueryParser.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/FuzzyLikeThisFieldQueryParser.java index 6acbae5b0c9..5358edb2912 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/FuzzyLikeThisFieldQueryParser.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/FuzzyLikeThisFieldQueryParser.java @@ -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) { diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/FuzzyLikeThisQueryBuilder.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/FuzzyLikeThisQueryBuilder.java index c4e165f4c20..9c4a7cc7cd5 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/FuzzyLikeThisQueryBuilder.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/FuzzyLikeThisQueryBuilder.java @@ -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(); } } \ No newline at end of file diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/FuzzyLikeThisQueryParser.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/FuzzyLikeThisQueryParser.java index b554a8e34a7..ad9ae0dc8cd 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/FuzzyLikeThisQueryParser.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/FuzzyLikeThisQueryParser.java @@ -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); diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/MoreLikeThisFieldQueryBuilder.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/MoreLikeThisFieldQueryBuilder.java index 0ff288cb05c..f7982910f87 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/MoreLikeThisFieldQueryBuilder.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/MoreLikeThisFieldQueryBuilder.java @@ -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(); } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/MoreLikeThisFieldQueryParser.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/MoreLikeThisFieldQueryParser.java index 14af675a260..f5056e49ecd 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/MoreLikeThisFieldQueryParser.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/MoreLikeThisFieldQueryParser.java @@ -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); } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilder.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilder.java index dc989b3bb32..fd138b5e3f1 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilder.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilder.java @@ -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(); } } \ No newline at end of file diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/MoreLikeThisQueryParser.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/MoreLikeThisQueryParser.java index 946efbfa74a..f62747da69b 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/MoreLikeThisQueryParser.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/MoreLikeThisQueryParser.java @@ -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; } } \ No newline at end of file