Skip to content

Commit

Permalink
Correctly apply boosts in query string.
Browse files Browse the repository at this point in the history
This applies boosts to phrase queries generated by query string queries
both in boolean and dismax mode.
  • Loading branch information
nik9000 authored and s1monw committed Aug 14, 2013
1 parent ddad4fe commit becbbf5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ protected Query getFieldQuery(String field, String queryText, int slop) throws P
Query q = super.getFieldQuery(mField, queryText, slop);
if (q != null) {
added = true;
applyBoost(field, q);
applyBoost(mField, q);
applySlop(q, slop);
disMaxQuery.add(q);
}
Expand All @@ -297,7 +297,7 @@ protected Query getFieldQuery(String field, String queryText, int slop) throws P
for (String mField : fields) {
Query q = super.getFieldQuery(mField, queryText, slop);
if (q != null) {
applyBoost(field, q);
applyBoost(mField, q);
applySlop(q, slop);
clauses.add(new BooleanClause(q, BooleanClause.Occur.SHOULD));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@
import org.junit.Test;

import java.io.IOException;
import java.util.concurrent.ExecutionException;

import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.index.query.FilterBuilders.*;
import static org.elasticsearch.index.query.QueryBuilders.*;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.*;
import static org.hamcrest.Matchers.anyOf;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.*;

/**
*
Expand Down Expand Up @@ -745,6 +745,34 @@ public void testFuzzyQueryString() {
assertThat(searchResponse.getHits().totalHits(), equalTo(1l));
}

@Test
public void testQuotedQueryStringWithBoost() throws InterruptedException, ExecutionException {
float boost = 10.0f;
client().admin().indices().prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 1)).execute().actionGet();
indexRandom("test", true,
client().prepareIndex("test", "type1", "1").setSource("important", "phrase match", "less_important", "nothing important"),
client().prepareIndex("test", "type1", "2").setSource("important", "nothing important", "less_important", "phrase match")
);

SearchResponse searchResponse = client().prepareSearch()
.setQuery(queryString("\"phrase match\"").field("important", boost).field("less_important"))
.execute().actionGet();
assertNoFailures(searchResponse);
assertThat(searchResponse.getHits().totalHits(), equalTo(2l));
assertThat(searchResponse.getHits().getAt(0).id(), equalTo("1"));
assertThat(searchResponse.getHits().getAt(1).id(), equalTo("2"));
assertThat((double)searchResponse.getHits().getAt(0).score(), closeTo(boost * searchResponse.getHits().getAt(1).score(), .1));

searchResponse = client().prepareSearch()
.setQuery(queryString("\"phrase match\"").field("important", boost).field("less_important").useDisMax(false))
.execute().actionGet();
assertNoFailures(searchResponse);
assertThat(searchResponse.getHits().totalHits(), equalTo(2l));
assertThat(searchResponse.getHits().getAt(0).id(), equalTo("1"));
assertThat(searchResponse.getHits().getAt(1).id(), equalTo("2"));
assertThat((double)searchResponse.getHits().getAt(0).score(), closeTo(boost * searchResponse.getHits().getAt(1).score(), .1));
}

@Test
public void testSpecialRangeSyntaxInQueryString() {
client().admin().indices().prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 1)).execute().actionGet();
Expand Down

0 comments on commit becbbf5

Please sign in to comment.