Skip to content

Commit

Permalink
Fixed NPE in multi_match query when using lenient and field weight
Browse files Browse the repository at this point in the history
Closes #3797
  • Loading branch information
eltu authored and javanna committed Oct 24, 2013
1 parent cc4bc7d commit 0e9c049
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
22 changes: 7 additions & 15 deletions src/main/java/org/elasticsearch/index/search/MultiMatchQuery.java
Expand Up @@ -46,35 +46,30 @@ public MultiMatchQuery(QueryParseContext parseContext) {
super(parseContext);
}

private Query parseAndApply(Type type, String fieldName, Object value, String minimumShouldMatch) throws IOException {
private Query parseAndApply(Type type, String fieldName, Object value, String minimumShouldMatch, Float boostValue) throws IOException {
Query query = parse(type, fieldName, value);
if (query instanceof BooleanQuery) {
Queries.applyMinimumShouldMatch((BooleanQuery) query, minimumShouldMatch);
}
if (boostValue != null && query != null) {
query.setBoost(boostValue);
}
return query;
}

public Query parse(Type type, Map<String, Float> fieldNames, Object value, String minimumShouldMatch) throws IOException {
if (fieldNames.size() == 1) {
Map.Entry<String, Float> fieldBoost = fieldNames.entrySet().iterator().next();
Float boostValue = fieldBoost.getValue();
final Query query = parseAndApply(type, fieldBoost.getKey(), value, minimumShouldMatch);
if (boostValue != null) {
query.setBoost(boostValue);
}
return query;
return parseAndApply(type, fieldBoost.getKey(), value, minimumShouldMatch, boostValue);
}

if (useDisMax) {
DisjunctionMaxQuery disMaxQuery = new DisjunctionMaxQuery(tieBreaker);
boolean clauseAdded = false;
for (String fieldName : fieldNames.keySet()) {
Query query = parseAndApply(type, fieldName, value, minimumShouldMatch);
Float boostValue = fieldNames.get(fieldName);
if (boostValue != null) {
query.setBoost(boostValue);
}

Query query = parseAndApply(type, fieldName, value, minimumShouldMatch, boostValue);
if (query != null) {
clauseAdded = true;
disMaxQuery.add(query);
Expand All @@ -84,11 +79,8 @@ public Query parse(Type type, Map<String, Float> fieldNames, Object value, Strin
} else {
BooleanQuery booleanQuery = new BooleanQuery();
for (String fieldName : fieldNames.keySet()) {
Query query = parseAndApply(type, fieldName, value, minimumShouldMatch);
Float boostValue = fieldNames.get(fieldName);
if (boostValue != null) {
query.setBoost(boostValue);
}
Query query = parseAndApply(type, fieldName, value, minimumShouldMatch, boostValue);
if (query != null) {
booleanQuery.add(query, BooleanClause.Occur.SHOULD);
}
Expand Down
Expand Up @@ -60,7 +60,6 @@
*/
public class SimpleQueryTests extends AbstractIntegrationTest {


@Test // see https://github.com/elasticsearch/elasticsearch/issues/3177
public void testIssue3177() {
run(prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 1)));
Expand Down Expand Up @@ -1794,4 +1793,24 @@ public void testCustomWordDelimiterQueryString() {

assertHitCount(response, 1l);
}

@Test // see https://github.com/elasticsearch/elasticsearch/issues/3797
public void testMultiMatchLenientIssue3797() {
createIndex("test");
ensureGreen();
client().prepareIndex("test", "type1", "1").setSource("field1", 123, "field2", "value2").get();
refresh();

SearchResponse searchResponse = client().prepareSearch("test")
.setQuery(QueryBuilders.multiMatchQuery("value2", "field1^2", "field2").lenient(true).useDisMax(false)).get();
assertHitCount(searchResponse, 1l);

searchResponse = client().prepareSearch("test")
.setQuery(QueryBuilders.multiMatchQuery("value2", "field1^2", "field2").lenient(true).useDisMax(true)).get();
assertHitCount(searchResponse, 1l);

searchResponse = client().prepareSearch("test")
.setQuery(QueryBuilders.multiMatchQuery("value2", "field2^2").lenient(true)).get();
assertHitCount(searchResponse, 1l);
}
}

0 comments on commit 0e9c049

Please sign in to comment.