Respect lowercase_expanded_terms in MappingQueryParser

Fixes #2566
This commit is contained in:
Simon Willnauer 2013-01-18 20:30:31 +01:00
parent 31fd521fd1
commit d6b613ac8c
3 changed files with 42 additions and 1 deletions

View File

@ -294,6 +294,10 @@ public class MapperQueryParser extends QueryParser {
if ("*".equals(part2)) { if ("*".equals(part2)) {
part2 = null; part2 = null;
} }
if (lowercaseExpandedTerms) {
part1 = part1==null ? null : part1.toLowerCase(locale);
part2 = part2==null ? null : part2.toLowerCase(locale);
}
Collection<String> fields = extractMultiFields(field); Collection<String> fields = extractMultiFields(field);
if (fields != null) { if (fields != null) {
if (fields.size() == 1) { if (fields.size() == 1) {
@ -354,6 +358,9 @@ public class MapperQueryParser extends QueryParser {
@Override @Override
protected Query getFuzzyQuery(String field, String termStr, float minSimilarity) throws ParseException { protected Query getFuzzyQuery(String field, String termStr, float minSimilarity) throws ParseException {
if (lowercaseExpandedTerms) {
termStr = termStr.toLowerCase(locale);
}
Collection<String> fields = extractMultiFields(field); Collection<String> fields = extractMultiFields(field);
if (fields != null) { if (fields != null) {
if (fields.size() == 1) { if (fields.size() == 1) {
@ -421,6 +428,9 @@ public class MapperQueryParser extends QueryParser {
@Override @Override
protected Query getPrefixQuery(String field, String termStr) throws ParseException { protected Query getPrefixQuery(String field, String termStr) throws ParseException {
if (lowercaseExpandedTerms) {
termStr = termStr.toLowerCase(locale);
}
Collection<String> fields = extractMultiFields(field); Collection<String> fields = extractMultiFields(field);
if (fields != null) { if (fields != null) {
if (fields.size() == 1) { if (fields.size() == 1) {
@ -569,6 +579,9 @@ public class MapperQueryParser extends QueryParser {
return fieldQueryExtensions.get(ExistsFieldQueryExtension.NAME).query(parseContext, actualField); return fieldQueryExtensions.get(ExistsFieldQueryExtension.NAME).query(parseContext, actualField);
} }
} }
if (lowercaseExpandedTerms) {
termStr = termStr.toLowerCase(locale);
}
Collection<String> fields = extractMultiFields(field); Collection<String> fields = extractMultiFields(field);
if (fields != null) { if (fields != null) {
if (fields.size() == 1) { if (fields.size() == 1) {

View File

@ -160,6 +160,34 @@ public class SimpleQueryTests extends AbstractNodesTests {
searchResponse = client.prepareSearch().setQuery(queryString("v?l*e?1").analyzeWildcard(true)).execute().actionGet(); searchResponse = client.prepareSearch().setQuery(queryString("v?l*e?1").analyzeWildcard(true)).execute().actionGet();
assertThat(searchResponse.hits().totalHits(), equalTo(1l)); 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 @Test
public void typeFilterTypeIndexedTests() throws Exception { public void typeFilterTypeIndexedTests() throws Exception {

View File

@ -93,7 +93,7 @@ public class SimpleSearchTests extends AbstractNodesTests {
searchResponse = client.prepareSearch().setQuery(QueryBuilders.prefixQuery("_id", "XXX")).execute().actionGet(); searchResponse = client.prepareSearch().setQuery(QueryBuilders.prefixQuery("_id", "XXX")).execute().actionGet();
assertThat(searchResponse.hits().totalHits(), equalTo(1l)); 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)); assertThat(searchResponse.hits().totalHits(), equalTo(1l));
} }