From d6b613ac8c6e3ddee6d8e1604db208721f03cb75 Mon Sep 17 00:00:00 2001 From: Simon Willnauer Date: Fri, 18 Jan 2013 20:30:31 +0100 Subject: [PATCH] Respect lowercase_expanded_terms in MappingQueryParser Fixes #2566 --- .../classic/MapperQueryParser.java | 13 +++++++++ .../search/query/SimpleQueryTests.java | 28 +++++++++++++++++++ .../search/simple/SimpleSearchTests.java | 2 +- 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/apache/lucene/queryparser/classic/MapperQueryParser.java b/src/main/java/org/apache/lucene/queryparser/classic/MapperQueryParser.java index e24c86cc5cc..57bc33b1628 100644 --- a/src/main/java/org/apache/lucene/queryparser/classic/MapperQueryParser.java +++ b/src/main/java/org/apache/lucene/queryparser/classic/MapperQueryParser.java @@ -294,6 +294,10 @@ public class MapperQueryParser extends QueryParser { if ("*".equals(part2)) { part2 = null; } + if (lowercaseExpandedTerms) { + part1 = part1==null ? null : part1.toLowerCase(locale); + part2 = part2==null ? null : part2.toLowerCase(locale); + } Collection fields = extractMultiFields(field); if (fields != null) { if (fields.size() == 1) { @@ -354,6 +358,9 @@ public class MapperQueryParser extends QueryParser { @Override protected Query getFuzzyQuery(String field, String termStr, float minSimilarity) throws ParseException { + if (lowercaseExpandedTerms) { + termStr = termStr.toLowerCase(locale); + } Collection fields = extractMultiFields(field); if (fields != null) { if (fields.size() == 1) { @@ -421,6 +428,9 @@ public class MapperQueryParser extends QueryParser { @Override protected Query getPrefixQuery(String field, String termStr) throws ParseException { + if (lowercaseExpandedTerms) { + termStr = termStr.toLowerCase(locale); + } Collection fields = extractMultiFields(field); if (fields != null) { if (fields.size() == 1) { @@ -569,6 +579,9 @@ public class MapperQueryParser extends QueryParser { return fieldQueryExtensions.get(ExistsFieldQueryExtension.NAME).query(parseContext, actualField); } } + if (lowercaseExpandedTerms) { + termStr = termStr.toLowerCase(locale); + } Collection fields = extractMultiFields(field); if (fields != null) { if (fields.size() == 1) { 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 3b4f83cf951..f6cb0e8d5b6 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 @@ -160,6 +160,34 @@ public class SimpleQueryTests extends AbstractNodesTests { searchResponse = client.prepareSearch().setQuery(queryString("v?l*e?1").analyzeWildcard(true)).execute().actionGet(); assertThat(searchResponse.hits().totalHits(), equalTo(1l)); } + + @Test + public void testLowercaseExpandedTerms() { + try { + client.admin().indices().prepareDelete("test").execute().actionGet(); + } catch (Exception e) { + // ignore + } + + client.admin().indices().prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("number_of_shards", 1)).execute().actionGet(); + + client.prepareIndex("test", "type1", "1").setSource("field1", "value_1", "field2", "value_2").execute().actionGet(); + + client.admin().indices().prepareRefresh().execute().actionGet(); + + SearchResponse searchResponse = client.prepareSearch().setQuery(queryString("VALUE_3~1").lowercaseExpandedTerms(true)).execute().actionGet(); + assertThat(searchResponse.hits().totalHits(), equalTo(1l)); + searchResponse = client.prepareSearch().setQuery(queryString("VALUE_3~1").lowercaseExpandedTerms(false)).execute().actionGet(); + assertThat(searchResponse.hits().totalHits(), equalTo(0l)); + searchResponse = client.prepareSearch().setQuery(queryString("ValUE_*").lowercaseExpandedTerms(true)).execute().actionGet(); + assertThat(searchResponse.hits().totalHits(), equalTo(1l)); + searchResponse = client.prepareSearch().setQuery(queryString("vAl*E_1")).execute().actionGet(); + assertThat(searchResponse.hits().totalHits(), equalTo(1l)); + searchResponse = client.prepareSearch().setQuery(queryString("[VALUE_1 TO VALUE_3]")).execute().actionGet(); + assertThat(searchResponse.hits().totalHits(), equalTo(1l)); + searchResponse = client.prepareSearch().setQuery(queryString("[VALUE_1 TO VALUE_3]").lowercaseExpandedTerms(false)).execute().actionGet(); + assertThat(searchResponse.hits().totalHits(), equalTo(0l)); + } @Test public void typeFilterTypeIndexedTests() throws Exception { diff --git a/src/test/java/org/elasticsearch/test/integration/search/simple/SimpleSearchTests.java b/src/test/java/org/elasticsearch/test/integration/search/simple/SimpleSearchTests.java index 30cb84a3b48..619786d3fa6 100644 --- a/src/test/java/org/elasticsearch/test/integration/search/simple/SimpleSearchTests.java +++ b/src/test/java/org/elasticsearch/test/integration/search/simple/SimpleSearchTests.java @@ -93,7 +93,7 @@ public class SimpleSearchTests extends AbstractNodesTests { searchResponse = client.prepareSearch().setQuery(QueryBuilders.prefixQuery("_id", "XXX")).execute().actionGet(); assertThat(searchResponse.hits().totalHits(), equalTo(1l)); - searchResponse = client.prepareSearch().setQuery(QueryBuilders.queryString("_id:XXX*")).execute().actionGet(); + searchResponse = client.prepareSearch().setQuery(QueryBuilders.queryString("_id:XXX*").lowercaseExpandedTerms(false)).execute().actionGet(); assertThat(searchResponse.hits().totalHits(), equalTo(1l)); }