From 86cd95aee21971b80349e24c66643d82fac2f67b Mon Sep 17 00:00:00 2001 From: Shay Banon Date: Tue, 22 May 2012 16:46:58 +0200 Subject: [PATCH] Query DSL: text query to support minimum_should_match, closes #1971. --- .../elasticsearch/index/query/TextQueryBuilder.java | 10 ++++++++++ .../org/elasticsearch/index/query/TextQueryParser.java | 10 ++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/main/java/org/elasticsearch/index/query/TextQueryBuilder.java b/src/main/java/org/elasticsearch/index/query/TextQueryBuilder.java index 50160f5968d..23f0c4dcfbb 100644 --- a/src/main/java/org/elasticsearch/index/query/TextQueryBuilder.java +++ b/src/main/java/org/elasticsearch/index/query/TextQueryBuilder.java @@ -69,6 +69,8 @@ public class TextQueryBuilder extends BaseQueryBuilder { private Integer maxExpansions; + private String minimumShouldMatch; + /** * Constructs a new text query. */ @@ -140,6 +142,11 @@ public class TextQueryBuilder extends BaseQueryBuilder { return this; } + public TextQueryBuilder minimumShouldMatch(String minimumShouldMatch) { + this.minimumShouldMatch = minimumShouldMatch; + return this; + } + @Override public void doXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(TextQueryParser.NAME); @@ -170,6 +177,9 @@ public class TextQueryBuilder extends BaseQueryBuilder { if (maxExpansions != null) { builder.field("max_expansions", maxExpansions); } + if (minimumShouldMatch != null) { + builder.field("minimum_should_match", minimumShouldMatch); + } builder.endObject(); builder.endObject(); diff --git a/src/main/java/org/elasticsearch/index/query/TextQueryParser.java b/src/main/java/org/elasticsearch/index/query/TextQueryParser.java index fa636db93e0..b6b21a04255 100644 --- a/src/main/java/org/elasticsearch/index/query/TextQueryParser.java +++ b/src/main/java/org/elasticsearch/index/query/TextQueryParser.java @@ -20,9 +20,11 @@ package org.elasticsearch.index.query; import org.apache.lucene.search.BooleanClause; +import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.FuzzyQuery; import org.apache.lucene.search.Query; import org.elasticsearch.common.inject.Inject; +import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.common.xcontent.XContentParser; import java.io.IOException; @@ -68,6 +70,7 @@ public class TextQueryParser implements QueryParser { int prefixLength = FuzzyQuery.defaultPrefixLength; int maxExpansions = FuzzyQuery.defaultMaxExpansions; BooleanClause.Occur occur = BooleanClause.Occur.SHOULD; + String minimumShouldMatch = null; token = parser.nextToken(); if (token == XContentParser.Token.START_OBJECT) { @@ -111,6 +114,8 @@ public class TextQueryParser implements QueryParser { } else { throw new QueryParsingException(parseContext.index(), "text query requires operator to be either 'and' or 'or', not [" + op + "]"); } + } else if ("minimum_should_match".equals(currentFieldName) || "minimumShouldMatch".equals(currentFieldName)) { + minimumShouldMatch = parser.textOrNull(); } else { throw new QueryParsingException(parseContext.index(), "[text] query does not support [" + currentFieldName + "]"); } @@ -136,6 +141,11 @@ public class TextQueryParser implements QueryParser { tQP.setOccur(occur); Query query = tQP.parse(type); + + if (query instanceof BooleanQuery) { + Queries.applyMinimumShouldMatch((BooleanQuery) query, minimumShouldMatch); + } + query.setBoost(boost); return query; }