From f537b8cceefa694e895dfaa43722ce6b4283948a Mon Sep 17 00:00:00 2001 From: Clinton Gormley Date: Fri, 14 Jun 2013 11:08:56 +0200 Subject: [PATCH] Change default operator to "or" for "low_freq_operator" and "high_freq_operator" parameters for "common" queries Closes #3178 --- .../index/query/CommonTermsQueryParser.java | 8 ++++---- .../integration/search/query/SimpleQueryTests.java | 12 +++++++++--- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/elasticsearch/index/query/CommonTermsQueryParser.java b/src/main/java/org/elasticsearch/index/query/CommonTermsQueryParser.java index 94334f36522..621f39aaf9c 100644 --- a/src/main/java/org/elasticsearch/index/query/CommonTermsQueryParser.java +++ b/src/main/java/org/elasticsearch/index/query/CommonTermsQueryParser.java @@ -51,9 +51,9 @@ public class CommonTermsQueryParser implements QueryParser { static final float DEFAULT_MAX_TERM_DOC_FREQ = 0.01f; - static final Occur DEFAULT_HIGH_FREQ_OCCUR = Occur.MUST; + static final Occur DEFAULT_HIGH_FREQ_OCCUR = Occur.SHOULD; - static final Occur DEFAULT_LOW_FREQ_OCCUR = Occur.MUST; + static final Occur DEFAULT_LOW_FREQ_OCCUR = Occur.SHOULD; static final boolean DEFAULT_DISABLE_COORDS = true; @@ -81,7 +81,7 @@ public class CommonTermsQueryParser implements QueryParser { String minimumShouldMatch = null; boolean disableCoords = DEFAULT_DISABLE_COORDS; Occur highFreqOccur = DEFAULT_HIGH_FREQ_OCCUR; - Occur lowFreqOccur = DEFAULT_HIGH_FREQ_OCCUR; + Occur lowFreqOccur = DEFAULT_LOW_FREQ_OCCUR; float maxTermFrequency = DEFAULT_MAX_TERM_DOC_FREQ; token = parser.nextToken(); if (token == XContentParser.Token.START_OBJECT) { @@ -202,4 +202,4 @@ public class CommonTermsQueryParser implements QueryParser { query.setMinimumNumberShouldMatch(minimumShouldMatch); return wrapSmartNameQuery(query, smartNameFieldMappers, parseContext); } -} \ No newline at end of file +} diff --git a/src/test/java/org/elasticsearch/test/integration/search/query/SimpleQueryTests.java b/src/test/java/org/elasticsearch/test/integration/search/query/SimpleQueryTests.java index 13eec4906c7..0f306cbfe7a 100644 --- a/src/test/java/org/elasticsearch/test/integration/search/query/SimpleQueryTests.java +++ b/src/test/java/org/elasticsearch/test/integration/search/query/SimpleQueryTests.java @@ -127,13 +127,19 @@ public class SimpleQueryTests extends AbstractSharedClusterTest { client().prepareIndex("test", "type1", "2").setSource("field1", "the quick lazy huge brown fox jumps over the tree").execute().actionGet(); client().prepareIndex("test", "type1", "3").setSource("field1", "quick lazy huge brown", "field2", "the quick lazy huge brown fox jumps over the tree").setRefresh(true).execute().actionGet(); - SearchResponse searchResponse = client().prepareSearch().setQuery(QueryBuilders.commonTerms("field1", "the quick brown").cutoffFrequency(3)).execute().actionGet(); + SearchResponse searchResponse = client().prepareSearch().setQuery(QueryBuilders.commonTerms("field1", "the quick brown").cutoffFrequency(3).lowFreqOperator(Operator.OR)).execute().actionGet(); + assertThat(searchResponse.getHits().totalHits(), equalTo(3l)); + assertThat(searchResponse.getHits().getHits()[0].getId(), equalTo("1")); + assertThat(searchResponse.getHits().getHits()[1].getId(), equalTo("2")); + assertThat(searchResponse.getHits().getHits()[2].getId(), equalTo("3")); + + searchResponse = client().prepareSearch().setQuery(QueryBuilders.commonTerms("field1", "the quick brown").cutoffFrequency(3).lowFreqOperator(Operator.AND)).execute().actionGet(); assertThat(searchResponse.getHits().totalHits(), equalTo(2l)); assertThat(searchResponse.getHits().getHits()[0].getId(), equalTo("1")); assertThat(searchResponse.getHits().getHits()[1].getId(), equalTo("2")); - - searchResponse = client().prepareSearch().setQuery(QueryBuilders.commonTerms("field1", "the quick brown").cutoffFrequency(3).lowFreqOperator(Operator.OR)).execute().actionGet(); + // Default + searchResponse = client().prepareSearch().setQuery(QueryBuilders.commonTerms("field1", "the quick brown").cutoffFrequency(3)).execute().actionGet(); assertThat(searchResponse.getHits().totalHits(), equalTo(3l)); assertThat(searchResponse.getHits().getHits()[0].getId(), equalTo("1")); assertThat(searchResponse.getHits().getHits()[1].getId(), equalTo("2"));