diff --git a/src/main/java/org/springframework/data/solr/core/DefaultQueryParser.java b/src/main/java/org/springframework/data/solr/core/DefaultQueryParser.java index 219d5bbef..1d351de09 100644 --- a/src/main/java/org/springframework/data/solr/core/DefaultQueryParser.java +++ b/src/main/java/org/springframework/data/solr/core/DefaultQueryParser.java @@ -15,11 +15,21 @@ */ package org.springframework.data.solr.core; +import static org.apache.solr.common.params.CommonParams.DF; +import static org.apache.solr.common.params.DisMaxParams.ALTQ; +import static org.apache.solr.common.params.DisMaxParams.BF; +import static org.apache.solr.common.params.DisMaxParams.BQ; +import static org.apache.solr.common.params.DisMaxParams.MM; +import static org.apache.solr.common.params.DisMaxParams.PF; +import static org.apache.solr.common.params.DisMaxParams.PS; +import static org.apache.solr.common.params.DisMaxParams.QS; +import static org.apache.solr.common.params.DisMaxParams.TIE; +import static org.apache.solr.common.params.SimpleParams.QF; + import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Map.Entry; - import org.apache.commons.lang3.StringUtils; import org.apache.solr.client.solrj.SolrQuery; import org.apache.solr.client.solrj.SolrQuery.ORDER; @@ -35,16 +45,31 @@ import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort.Order; import org.springframework.data.mapping.context.MappingContext; -import org.springframework.data.solr.core.query.*; -import org.springframework.data.solr.core.query.Criteria.Predicate; +import org.springframework.data.solr.core.query.Criteria; +import org.springframework.data.solr.core.query.DisMaxOptions; +import org.springframework.data.solr.core.query.DisMaxQuery; +import org.springframework.data.solr.core.query.FacetOptions; import org.springframework.data.solr.core.query.FacetOptions.FacetParameter; import org.springframework.data.solr.core.query.FacetOptions.FieldWithDateRangeParameters; import org.springframework.data.solr.core.query.FacetOptions.FieldWithFacetParameters; import org.springframework.data.solr.core.query.FacetOptions.FieldWithNumericRangeParameters; import org.springframework.data.solr.core.query.FacetOptions.FieldWithRangeParameters; +import org.springframework.data.solr.core.query.FacetQuery; +import org.springframework.data.solr.core.query.Field; +import org.springframework.data.solr.core.query.FilterQuery; +import org.springframework.data.solr.core.query.Function; import org.springframework.data.solr.core.query.Function.Context.Target; +import org.springframework.data.solr.core.query.GroupOptions; +import org.springframework.data.solr.core.query.HighlightOptions; import org.springframework.data.solr.core.query.HighlightOptions.FieldWithHighlightParameters; import org.springframework.data.solr.core.query.HighlightOptions.HighlightParameter; +import org.springframework.data.solr.core.query.HighlightQuery; +import org.springframework.data.solr.core.query.PivotField; +import org.springframework.data.solr.core.query.Query; +import org.springframework.data.solr.core.query.QueryParameter; +import org.springframework.data.solr.core.query.SolrDataQuery; +import org.springframework.data.solr.core.query.SpellcheckOptions; +import org.springframework.data.solr.core.query.StatsOptions; import org.springframework.lang.Nullable; import org.springframework.util.Assert; import org.springframework.util.CollectionUtils; @@ -65,6 +90,7 @@ * @author Joachim Uhrlaß * @author Petar Tahchiev * @author Juan Manuel de Blas + * @author Matthew Hall */ public class DefaultQueryParser extends QueryParserBase { @@ -109,6 +135,10 @@ public final SolrQuery doConstructSolrQuery(SolrDataQuery query, @Nullable Class processHighlightOptions(solrQuery, (HighlightQuery) query, domainType); } + if (query instanceof DisMaxQuery) { + processDisMaxOptions(solrQuery, (DisMaxQuery) query); + } + return solrQuery; } @@ -132,6 +162,38 @@ private void processQueryOptions(SolrQuery solrQuery, Query query, @Nullable Cla LOGGER.debug("Constructed SolrQuery:\r\n {}", solrQuery); } + protected void processDisMaxOptions(SolrQuery solrQuery, DisMaxQuery disMaxQuery) { + + if (disMaxQuery == null || disMaxQuery.getDisMaxOptions() == null) { + return; + } + + DisMaxOptions disMaxOptions = disMaxQuery.getDisMaxOptions(); + + solrQuery.set("defType", "dismax"); + + setSolrParamIfPresent(solrQuery, DF, disMaxOptions.getDefaultField()); + + setSolrParamIfPresent(solrQuery, ALTQ, disMaxOptions.getAltQuery()); + setSolrParamIfPresent(solrQuery, QF, disMaxOptions.getQueryFunction()); + setSolrParamIfPresent(solrQuery, MM, disMaxOptions.getMinimumMatch()); + + setSolrParamIfPresent(solrQuery, BQ, disMaxOptions.getBoostQuery()); + setSolrParamIfPresent(solrQuery, BF, disMaxOptions.getBoostFunction()); + setSolrParamIfPresent(solrQuery, PF, disMaxOptions.getPhraseFunction()); + + setSolrParamIfPresent(solrQuery, PS, disMaxOptions.getPhraseSlop() == null ? null : + String.valueOf(disMaxOptions.getPhraseSlop())); + setSolrParamIfPresent(solrQuery, QS, disMaxOptions.getQuerySlop() == null ? null : String.valueOf(disMaxOptions.getQuerySlop())); + setSolrParamIfPresent(solrQuery, TIE, disMaxOptions.getTie() == null ? null : String.valueOf(disMaxOptions.getTie())); + } + + private static void setSolrParamIfPresent(SolrQuery solrQuery, String param, String value) { + if (!org.springframework.util.StringUtils.isEmpty(value)) { + solrQuery.setParam(param, value); + } + } + private void processFacetOptions(SolrQuery solrQuery, FacetQuery query, @Nullable Class domainType) { if (enableFaceting(solrQuery, query)) { diff --git a/src/main/java/org/springframework/data/solr/core/QueryParserBase.java b/src/main/java/org/springframework/data/solr/core/QueryParserBase.java index 48766ade5..b5d6f36f3 100644 --- a/src/main/java/org/springframework/data/solr/core/QueryParserBase.java +++ b/src/main/java/org/springframework/data/solr/core/QueryParserBase.java @@ -62,6 +62,7 @@ * @author Radek Mensik * @author David Webb * @author Michael Rocke + * @author Matthew Hall */ public abstract class QueryParserBase implements QueryParser { @@ -1064,6 +1065,31 @@ public Map getNamesAssociation() { } } + /** + * @author Matthew Hall + * @since 4.1 + */ + static class NamedObjectsDisMaxQuery extends AbstractDisMaxQueryDecorator implements NamedObjects { + + private Map namesAssociation = new HashMap<>(); + + public NamedObjectsDisMaxQuery(DisMaxQuery query) { + super(query); + } + + @Override + public void setName(Object object, String name) { + setObjectName(namesAssociation, object, name); + } + + @Override + public Map getNamesAssociation() { + return Collections.unmodifiableMap(namesAssociation); + } + + } + + /** * Create new new {@link Context} for rendering {@link Function functions}. * diff --git a/src/main/java/org/springframework/data/solr/core/SolrOperations.java b/src/main/java/org/springframework/data/solr/core/SolrOperations.java index e9a148067..70b8e5901 100644 --- a/src/main/java/org/springframework/data/solr/core/SolrOperations.java +++ b/src/main/java/org/springframework/data/solr/core/SolrOperations.java @@ -18,13 +18,13 @@ import java.time.Duration; import java.util.Collection; import java.util.Optional; - import org.apache.solr.client.solrj.SolrClient; import org.apache.solr.client.solrj.response.SolrPingResponse; import org.apache.solr.client.solrj.response.UpdateResponse; import org.apache.solr.common.SolrInputDocument; import org.springframework.data.domain.Page; import org.springframework.data.solr.core.convert.SolrConverter; +import org.springframework.data.solr.core.query.DisMaxQuery; import org.springframework.data.solr.core.query.FacetAndHighlightQuery; import org.springframework.data.solr.core.query.FacetQuery; import org.springframework.data.solr.core.query.HighlightQuery; @@ -37,6 +37,7 @@ import org.springframework.data.solr.core.query.result.GroupPage; import org.springframework.data.solr.core.query.result.HighlightPage; import org.springframework.data.solr.core.query.result.ScoredPage; +import org.springframework.data.solr.core.query.result.SolrResultPage; import org.springframework.data.solr.core.query.result.StatsPage; import org.springframework.data.solr.core.query.result.TermsPage; import org.springframework.data.solr.core.schema.SchemaOperations; @@ -50,6 +51,7 @@ * @author Francisco Spaeth * @author Shiradwade Sateesh Krishna * @author David Webb + * @author Matthew Hall */ public interface SolrOperations { @@ -455,6 +457,29 @@ FacetAndHighlightPage queryForFacetAndHighlightPage(String collection, Fa */ StatsPage queryForStatsPage(String collection, Query query, Class clazz, RequestMethod method); + /** + * Execute the query against Solr and return result as {@link StatsPage}. + * + * @param collection must not be {@literal null}. + * @param query must not be {@literal null}. + * @param clazz must not be {@literal null}. + * @return never {@literal null}. + * @since 4.1 + */ + SolrResultPage queryForDisMaxPage(String collection, DisMaxQuery query, Class clazz); + + /** + * Execute the query against Solr and return result as {@link StatsPage}. + * + * @param collection must not be {@literal null}. + * @param query must not be {@literal null}. + * @param clazz must not be {@literal null}. + * @param requestMethod must not be {@literal null}. + * @return never {@literal null}. + * @since 4.1 + */ + public SolrResultPage queryForDisMaxPage(String collection, DisMaxQuery query, Class clazz, + @Nullable RequestMethod requestMethod); /** * Execute the query against Solr and return result as page. * diff --git a/src/main/java/org/springframework/data/solr/core/SolrTemplate.java b/src/main/java/org/springframework/data/solr/core/SolrTemplate.java index 536b954e8..3295ef86a 100644 --- a/src/main/java/org/springframework/data/solr/core/SolrTemplate.java +++ b/src/main/java/org/springframework/data/solr/core/SolrTemplate.java @@ -27,7 +27,6 @@ import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; - import org.apache.solr.client.solrj.SolrClient; import org.apache.solr.client.solrj.SolrQuery; import org.apache.solr.client.solrj.SolrRequest; @@ -52,6 +51,7 @@ import org.springframework.data.domain.Pageable; import org.springframework.data.mapping.context.MappingContext; import org.springframework.data.solr.UncategorizedSolrException; +import org.springframework.data.solr.core.QueryParserBase.NamedObjectsDisMaxQuery; import org.springframework.data.solr.core.QueryParserBase.NamedObjectsFacetAndHighlightQuery; import org.springframework.data.solr.core.QueryParserBase.NamedObjectsFacetQuery; import org.springframework.data.solr.core.QueryParserBase.NamedObjectsHighlightQuery; @@ -62,14 +62,25 @@ import org.springframework.data.solr.core.mapping.SolrPersistentEntity; import org.springframework.data.solr.core.mapping.SolrPersistentProperty; import org.springframework.data.solr.core.query.AbstractQueryDecorator; +import org.springframework.data.solr.core.query.DisMaxQuery; import org.springframework.data.solr.core.query.FacetAndHighlightQuery; import org.springframework.data.solr.core.query.FacetQuery; import org.springframework.data.solr.core.query.HighlightQuery; import org.springframework.data.solr.core.query.Query; import org.springframework.data.solr.core.query.SolrDataQuery; import org.springframework.data.solr.core.query.TermsQuery; -import org.springframework.data.solr.core.query.result.*; +import org.springframework.data.solr.core.query.result.Cursor; +import org.springframework.data.solr.core.query.result.DelegatingCursor; +import org.springframework.data.solr.core.query.result.FacetAndHighlightPage; +import org.springframework.data.solr.core.query.result.FacetPage; +import org.springframework.data.solr.core.query.result.GroupPage; +import org.springframework.data.solr.core.query.result.HighlightPage; +import org.springframework.data.solr.core.query.result.ScoredPage; +import org.springframework.data.solr.core.query.result.SolrResultPage; import org.springframework.data.solr.core.query.result.SpellcheckQueryResult.Alternative; +import org.springframework.data.solr.core.query.result.StatsPage; +import org.springframework.data.solr.core.query.result.TermsPage; +import org.springframework.data.solr.core.query.result.TermsResultPage; import org.springframework.data.solr.core.schema.DefaultSchemaOperations; import org.springframework.data.solr.core.schema.SchemaOperations; import org.springframework.data.solr.core.schema.SolrPersistentEntitySchemaCreator; @@ -91,6 +102,7 @@ * @author Petar Tahchiev * @author Mark Paluch * @author Juan Manuel de Blas + * @author Matthew Hall */ public class SolrTemplate implements SolrOperations, InitializingBean, ApplicationContextAware { @@ -431,6 +443,24 @@ public FacetAndHighlightPage queryForFacetAndHighlightPage(String collect return createSolrResultPage(query, clazz, response, objectsName); } + @Override + public SolrResultPage queryForDisMaxPage(String collection, DisMaxQuery query, Class clazz) { + return queryForDisMaxPage(collection, query, clazz, null); + } + + @Override + public SolrResultPage queryForDisMaxPage(String collection, DisMaxQuery query, Class clazz, + @Nullable RequestMethod requestMethod) { + + QueryResponse response; + NamedObjectsDisMaxQuery namedObjectsQuery = new NamedObjectsDisMaxQuery(query); + response = querySolr(collection, namedObjectsQuery, clazz, + requestMethod != null ? requestMethod : getDefaultRequestMethod()); + Map objectsName = namedObjectsQuery.getNamesAssociation(); + + return createSolrResultPage(query, clazz, response, objectsName); + } + private SolrResultPage createSolrResultPage(Query query, Class clazz, QueryResponse response, Map objectsName) { diff --git a/src/main/java/org/springframework/data/solr/core/query/AbstractDisMaxQueryDecorator.java b/src/main/java/org/springframework/data/solr/core/query/AbstractDisMaxQueryDecorator.java new file mode 100644 index 000000000..826eb7478 --- /dev/null +++ b/src/main/java/org/springframework/data/solr/core/query/AbstractDisMaxQueryDecorator.java @@ -0,0 +1,30 @@ +package org.springframework.data.solr.core.query; + +import org.springframework.lang.Nullable; + + +/** + * General purpose {@link DisMaxQuery} decorator. + * + * @author Matthew Hall + * @since 4.1.0 + */ +public abstract class AbstractDisMaxQueryDecorator extends AbstractQueryDecorator implements DisMaxQuery { + + private DisMaxQuery query; + + public AbstractDisMaxQueryDecorator(DisMaxQuery query) { + super(query); + this.query = query; + } + + @Nullable + @Override + public DisMaxOptions getDisMaxOptions() { + return this.query.getDisMaxOptions(); + } + + public T setDisMaxOptions(DisMaxOptions disMaxOptions) { + return this.query.setDisMaxOptions(disMaxOptions); + } +} diff --git a/src/main/java/org/springframework/data/solr/core/query/DisMaxOptions.java b/src/main/java/org/springframework/data/solr/core/query/DisMaxOptions.java new file mode 100644 index 000000000..d8a32fe63 --- /dev/null +++ b/src/main/java/org/springframework/data/solr/core/query/DisMaxOptions.java @@ -0,0 +1,151 @@ +package org.springframework.data.solr.core.query; + +/** + * DisMax Options. + * + * @author Matthew Hall + */ +public class DisMaxOptions { + + private String altQuery; + private String queryFunction; + private String minimumMatch; + private String boostQuery; + private String boostFunction; + private Integer phraseSlop; + private Integer querySlop; + private Double tie; + private String phraseFunction; + private String defaultField; + + + public String getAltQuery() { + return altQuery; + } + + public String getQueryFunction() { + return queryFunction; + } + + public String getMinimumMatch() { + return minimumMatch; + } + + public String getBoostQuery() { + return boostQuery; + } + + public String getBoostFunction() { + return boostFunction; + } + + public Integer getPhraseSlop() { + return phraseSlop; + } + + public Integer getQuerySlop() { + return querySlop; + } + + public Double getTie() { + return tie; + } + + public String getPhraseFunction() { + return phraseFunction; + } + + public String getDefaultField() { + return defaultField; + } + + private DisMaxOptions(String altQuery, String queryFunction, String minimumMatch, String boostQuery, + String boostFunction, Integer phraseSlop, Integer querySlop, Double tie, String phraseFunction, + String defaultField) { + + this.altQuery = altQuery; + this.queryFunction = queryFunction; + this.minimumMatch = minimumMatch; + this.boostQuery = boostQuery; + this.boostFunction = boostFunction; + this.phraseSlop = phraseSlop; + this.querySlop = querySlop; + this.tie = tie; + this.phraseFunction = phraseFunction; + this.defaultField = defaultField; + } + + + public static class Builder { + + private String altQuery; + private String queryFunction; + private String minimumMatch; + private String boostQuery; + private String boostFunction; + private Integer phraseSlop; + private Integer querySlop; + private Double tie; + private String phraseFunction; + + private String defaultField; + + public Builder() {} + + public Builder altQuery(String altQuery) { + this.altQuery = altQuery; + return this; + } + + public Builder queryFunction(String queryFunction) { + this.queryFunction = queryFunction; + return this; + } + + public Builder minimumMatch(String minimumMatch) { + this.minimumMatch = minimumMatch; + return this; + } + + public Builder boostQuery(String boostQuery) { + this.boostQuery = boostQuery; + return this; + } + + public Builder phraseSlop(Integer phraseSlop) { + this.phraseSlop = phraseSlop; + return this; + } + + public Builder querySlop(Integer querySlop) { + this.querySlop = querySlop; + return this; + } + + public Builder tie(Double tie) { + this.tie = tie; + return this; + } + + public Builder boostFunction(String boostFunction) { + this.boostFunction = boostFunction; + return this; + } + + public Builder phraseFunction(String phraseFunction) { + this.phraseFunction = phraseFunction; + return this; + } + + public Builder defaultField(String defaultField) { + this.defaultField = defaultField; + return this; + } + + public DisMaxOptions build() { + return new DisMaxOptions(altQuery, queryFunction, minimumMatch, boostQuery, boostFunction, + phraseSlop, querySlop, tie, phraseFunction, defaultField); + } + + } +} diff --git a/src/main/java/org/springframework/data/solr/core/query/DisMaxQuery.java b/src/main/java/org/springframework/data/solr/core/query/DisMaxQuery.java new file mode 100644 index 000000000..c4a74c66e --- /dev/null +++ b/src/main/java/org/springframework/data/solr/core/query/DisMaxQuery.java @@ -0,0 +1,17 @@ +package org.springframework.data.solr.core.query; + +import org.springframework.lang.Nullable; + +/** + * Query to be used for DisMax query type. + * + * @author Matthew Hall + */ +public interface DisMaxQuery extends Query { + + @Nullable + DisMaxOptions getDisMaxOptions(); + + T setDisMaxOptions(DisMaxOptions disMaxOptions); + +} diff --git a/src/main/java/org/springframework/data/solr/core/query/SimpleDisMaxQuery.java b/src/main/java/org/springframework/data/solr/core/query/SimpleDisMaxQuery.java new file mode 100644 index 000000000..9df760431 --- /dev/null +++ b/src/main/java/org/springframework/data/solr/core/query/SimpleDisMaxQuery.java @@ -0,0 +1,34 @@ +package org.springframework.data.solr.core.query; + +import org.springframework.data.domain.Pageable; +import org.springframework.lang.Nullable; + +/** + * Trivial implementation of {@link DisMaxQuery} + * + * @author Matthew Hall + */ +public class SimpleDisMaxQuery extends SimpleQuery implements DisMaxQuery { + + private DisMaxOptions disMaxOptions; + + public SimpleDisMaxQuery(Criteria criteria) { + this(criteria, null); + } + + public SimpleDisMaxQuery(Criteria criteria, @Nullable Pageable pageable) { + super(criteria, pageable); + } + + @Nullable + @Override + public DisMaxOptions getDisMaxOptions() { + return this.disMaxOptions; + } + + @SuppressWarnings("unchecked") + public T setDisMaxOptions(DisMaxOptions disMaxOptions) { + this.disMaxOptions = disMaxOptions; + return (T) this; + } +} \ No newline at end of file diff --git a/src/test/java/org/springframework/data/solr/core/DefaultQueryParserTests.java b/src/test/java/org/springframework/data/solr/core/DefaultQueryParserTests.java index 83fcf7d33..2bab55359 100644 --- a/src/test/java/org/springframework/data/solr/core/DefaultQueryParserTests.java +++ b/src/test/java/org/springframework/data/solr/core/DefaultQueryParserTests.java @@ -15,8 +15,16 @@ */ package org.springframework.data.solr.core; -import static org.hamcrest.CoreMatchers.*; -import static org.junit.Assert.*; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.nullValue; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; import java.util.Arrays; import java.util.Calendar; @@ -24,11 +32,11 @@ import java.util.Date; import java.util.List; import java.util.TimeZone; - import org.apache.commons.lang3.StringUtils; import org.apache.solr.client.solrj.SolrQuery; import org.apache.solr.client.solrj.beans.Field; import org.apache.solr.common.params.CommonParams; +import org.apache.solr.common.params.DisMaxParams; import org.apache.solr.common.params.FacetParams; import org.apache.solr.common.params.FacetParams.FacetRangeInclude; import org.apache.solr.common.params.FacetParams.FacetRangeOther; @@ -53,13 +61,34 @@ import org.springframework.data.geo.Point; import org.springframework.data.solr.core.mapping.SimpleSolrMappingContext; import org.springframework.data.solr.core.mapping.SolrDocument; -import org.springframework.data.solr.core.query.*; +import org.springframework.data.solr.core.query.AnyCriteria; +import org.springframework.data.solr.core.query.Criteria; +import org.springframework.data.solr.core.query.DisMaxOptions; +import org.springframework.data.solr.core.query.DisMaxQuery; +import org.springframework.data.solr.core.query.FacetOptions; import org.springframework.data.solr.core.query.FacetOptions.FacetParameter; import org.springframework.data.solr.core.query.FacetOptions.FacetSort; import org.springframework.data.solr.core.query.FacetOptions.FieldWithDateRangeParameters; import org.springframework.data.solr.core.query.FacetOptions.FieldWithFacetParameters; import org.springframework.data.solr.core.query.FacetOptions.FieldWithNumericRangeParameters; +import org.springframework.data.solr.core.query.FacetQuery; +import org.springframework.data.solr.core.query.GroupOptions; +import org.springframework.data.solr.core.query.HighlightOptions; +import org.springframework.data.solr.core.query.Join; +import org.springframework.data.solr.core.query.MaxFunction; +import org.springframework.data.solr.core.query.Query; import org.springframework.data.solr.core.query.Query.Operator; +import org.springframework.data.solr.core.query.SimpleDisMaxQuery; +import org.springframework.data.solr.core.query.SimpleFacetQuery; +import org.springframework.data.solr.core.query.SimpleField; +import org.springframework.data.solr.core.query.SimpleFilterQuery; +import org.springframework.data.solr.core.query.SimpleHighlightQuery; +import org.springframework.data.solr.core.query.SimpleQuery; +import org.springframework.data.solr.core.query.SimpleStringCriteria; +import org.springframework.data.solr.core.query.SolrDataQuery; +import org.springframework.data.solr.core.query.SolrPageRequest; +import org.springframework.data.solr.core.query.SpellcheckOptions; +import org.springframework.data.solr.core.query.StatsOptions; /** * @author Christoph Strobl @@ -70,6 +99,7 @@ * @author Francisco Spaeth * @author Petar Tahchiev * @author Michael Rocke + * @author Matthew Hall */ public class DefaultQueryParserTests { @@ -493,6 +523,7 @@ public void testConstructSimpleSolrQuery() { assertProjectionNotPresent(solrQuery); assertGroupingNotPresent(solrQuery); assertFactingNotPresent(solrQuery); + assertDisMaxParamsNotPresent(solrQuery); } @Test @@ -508,6 +539,7 @@ public void testConstructSolrQueryWithPagination() { assertProjectionNotPresent(solrQuery); assertGroupingNotPresent(solrQuery); assertFactingNotPresent(solrQuery); + assertDisMaxParamsNotPresent(solrQuery); } @Test @@ -522,6 +554,39 @@ public void testConstructSimpleSolrQueryWithProjection() { assertProjectionPresent(solrQuery, "projection_1,projection_2"); assertGroupingNotPresent(solrQuery); assertFactingNotPresent(solrQuery); + assertDisMaxParamsNotPresent(solrQuery); + } + + @Test + public void testConstructSimpleDisMaxSolrQuery() { + + DisMaxQuery query = new SimpleDisMaxQuery(new Criteria("field_1").is("value_1")); + + DisMaxOptions options = + new DisMaxOptions.Builder() + .altQuery("altq") + .boostFunction("boost_function") + .boostQuery("boost_query") + .defaultField("field_1") + .minimumMatch("100%") + .phraseFunction("phrase_function") + .phraseSlop(1) + .queryFunction("query_function") + .querySlop(2) + .tie(3.0) + .build(); + + query.setDisMaxOptions(options); + + SolrQuery solrQuery = queryParser.constructSolrQuery(query, null); + + assertNotNull(solrQuery); + assertQueryStringPresent(solrQuery); + assertPaginationNotPresent(solrQuery); + assertProjectionNotPresent(solrQuery); + assertGroupingNotPresent(solrQuery); + assertFactingNotPresent(solrQuery); + assertDisMaxParamsPresent(solrQuery, options); } @Test @@ -536,6 +601,7 @@ public void testConstructSolrQueryWithSingleGroupBy() { assertProjectionNotPresent(solrQuery); assertGroupingPresent(solrQuery, "group_1"); assertFactingNotPresent(solrQuery); + assertDisMaxParamsNotPresent(solrQuery); } @Test @@ -550,6 +616,7 @@ public void testConstructSolrQueryWithSingleFacetOnField() { assertProjectionNotPresent(solrQuery); assertGroupingNotPresent(solrQuery); assertFactingPresent(solrQuery, "facet_1"); + assertDisMaxParamsNotPresent(solrQuery); } @Test @@ -564,6 +631,7 @@ public void testConstructSolrQueryWithSinglePivot() { assertProjectionNotPresent(solrQuery); assertGroupingNotPresent(solrQuery); assertPivotFactingPresent(solrQuery, "field_1,field_2"); + assertDisMaxParamsNotPresent(solrQuery); } @Test @@ -578,6 +646,7 @@ public void testConstructSolrQueryWithMultipleFacetOnFields() { assertProjectionNotPresent(solrQuery); assertGroupingNotPresent(solrQuery); assertFactingPresent(solrQuery, "facet_1", "facet_2"); + assertDisMaxParamsNotPresent(solrQuery); } @Test @@ -592,6 +661,7 @@ public void testConstructSolrQueryWithMultiplePivot() { assertProjectionNotPresent(solrQuery); assertGroupingNotPresent(solrQuery); assertPivotFactingPresent(solrQuery, "field_1,field_2", "field_2,field_3"); + assertDisMaxParamsNotPresent(solrQuery); } @Test @@ -609,6 +679,7 @@ public void testConstructSolrQueryWithFacetPrefix() { assertProjectionNotPresent(solrQuery); assertGroupingNotPresent(solrQuery); assertFactingPresent(solrQuery, "facet_1", "facet_2"); + assertDisMaxParamsNotPresent(solrQuery); assertEquals(facetOptions.getFacetPrefix(), solrQuery.getParams("facet.prefix")[0]); } @@ -628,6 +699,7 @@ public void testConstructSolrQueryWithFieldFacetParameters() { assertProjectionNotPresent(solrQuery); assertGroupingNotPresent(solrQuery); assertFactingPresent(solrQuery, "facet_1", "facet_2"); + assertDisMaxParamsNotPresent(solrQuery); assertEquals(fieldWithFacetParameters.getPrefix(), solrQuery.getParams("f." + fieldWithFacetParameters.getName() + ".facet.prefix")[0]); assertEquals(FacetParams.FACET_SORT_INDEX, @@ -680,6 +752,7 @@ public void testConstructSolrQueryWithSingleFacetFilterQuery() { assertPaginationNotPresent(solrQuery); assertProjectionNotPresent(solrQuery); assertGroupingNotPresent(solrQuery); + assertDisMaxParamsNotPresent(solrQuery); assertArrayEquals(new String[] { "field_2:[* TO 5]" }, solrQuery.getFacetQuery()); } @@ -696,6 +769,7 @@ public void testConstructSolrQueryWithMultipleFacetFilterQuerues() { assertPaginationNotPresent(solrQuery); assertProjectionNotPresent(solrQuery); assertGroupingNotPresent(solrQuery); + assertDisMaxParamsNotPresent(solrQuery); assertArrayEquals(new String[] { "field_2:[* TO 5]", "field_3:prefix*" }, solrQuery.getFacetQuery()); } @@ -732,6 +806,7 @@ public void testWithSimpleStringCriteria() { assertProjectionNotPresent(solrQuery); assertGroupingNotPresent(solrQuery); assertFactingNotPresent(solrQuery); + assertDisMaxParamsNotPresent(solrQuery); assertEquals(criteria.getQueryString(), solrQuery.getQuery()); } @@ -1879,6 +1954,36 @@ private void assertGroupFormatPresent(SolrQuery solrQuery, boolean groupTotalCou assertEquals(String.valueOf(groupTotalCount), solrQuery.get(GroupParams.GROUP_TOTAL_COUNT)); } + private void assertDisMaxParamsNotPresent(SolrQuery solrQuery) { + + assertNull(solrQuery.get(DisMaxParams.ALTQ)); + assertNull(solrQuery.get(DisMaxParams.BF)); + assertNull(solrQuery.get(DisMaxParams.BQ)); + assertNull(solrQuery.get(DisMaxParams.MM)); + assertNull(solrQuery.get(DisMaxParams.PF)); + assertNull(solrQuery.get(DisMaxParams.PS)); + assertNull(solrQuery.get(DisMaxParams.QF)); + assertNull(solrQuery.get(DisMaxParams.QS)); + assertNull(solrQuery.get(DisMaxParams.TIE)); + assertNull(solrQuery.get(CommonParams.DF)); + + } + + private void assertDisMaxParamsPresent(SolrQuery solrQuery, DisMaxOptions options) { + + assertEquals(solrQuery.get(DisMaxParams.ALTQ), options.getAltQuery()); + assertEquals(solrQuery.get(DisMaxParams.BF), options.getBoostFunction()); + assertEquals(solrQuery.get(DisMaxParams.BQ), options.getBoostQuery()); + assertEquals(solrQuery.get(DisMaxParams.MM), options.getMinimumMatch()); + assertEquals(solrQuery.get(DisMaxParams.PF), options.getPhraseFunction()); + assertEquals(solrQuery.get(DisMaxParams.PS), String.valueOf(options.getPhraseSlop())); + assertEquals(solrQuery.get(DisMaxParams.QF), options.getQueryFunction()); + assertEquals(solrQuery.get(DisMaxParams.QS), String.valueOf(options.getQuerySlop())); + assertEquals(solrQuery.get(DisMaxParams.TIE), String.valueOf(options.getTie())); + assertEquals(solrQuery.get(CommonParams.DF), options.getDefaultField()); + + } + @SolrDocument static class Sample {