Skip to content
This repository was archived by the owner on Sep 19, 2023. It is now read-only.

Commit a65189a

Browse files
author
Matt Hall
committed
DATASOLR-153 - Added support for dismax queries.
1 parent 6d10043 commit a65189a

File tree

9 files changed

+490
-10
lines changed

9 files changed

+490
-10
lines changed

src/main/java/org/springframework/data/solr/core/DefaultQueryParser.java

+65-3
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,21 @@
1515
*/
1616
package org.springframework.data.solr.core;
1717

18+
import static org.apache.solr.common.params.CommonParams.DF;
19+
import static org.apache.solr.common.params.DisMaxParams.ALTQ;
20+
import static org.apache.solr.common.params.DisMaxParams.BF;
21+
import static org.apache.solr.common.params.DisMaxParams.BQ;
22+
import static org.apache.solr.common.params.DisMaxParams.MM;
23+
import static org.apache.solr.common.params.DisMaxParams.PF;
24+
import static org.apache.solr.common.params.DisMaxParams.PS;
25+
import static org.apache.solr.common.params.DisMaxParams.QS;
26+
import static org.apache.solr.common.params.DisMaxParams.TIE;
27+
import static org.apache.solr.common.params.SimpleParams.QF;
28+
1829
import java.util.ArrayList;
1930
import java.util.Collection;
2031
import java.util.List;
2132
import java.util.Map.Entry;
22-
2333
import org.apache.commons.lang3.StringUtils;
2434
import org.apache.solr.client.solrj.SolrQuery;
2535
import org.apache.solr.client.solrj.SolrQuery.ORDER;
@@ -35,16 +45,31 @@
3545
import org.springframework.data.domain.Sort;
3646
import org.springframework.data.domain.Sort.Order;
3747
import org.springframework.data.mapping.context.MappingContext;
38-
import org.springframework.data.solr.core.query.*;
39-
import org.springframework.data.solr.core.query.Criteria.Predicate;
48+
import org.springframework.data.solr.core.query.Criteria;
49+
import org.springframework.data.solr.core.query.DisMaxOptions;
50+
import org.springframework.data.solr.core.query.DisMaxQuery;
51+
import org.springframework.data.solr.core.query.FacetOptions;
4052
import org.springframework.data.solr.core.query.FacetOptions.FacetParameter;
4153
import org.springframework.data.solr.core.query.FacetOptions.FieldWithDateRangeParameters;
4254
import org.springframework.data.solr.core.query.FacetOptions.FieldWithFacetParameters;
4355
import org.springframework.data.solr.core.query.FacetOptions.FieldWithNumericRangeParameters;
4456
import org.springframework.data.solr.core.query.FacetOptions.FieldWithRangeParameters;
57+
import org.springframework.data.solr.core.query.FacetQuery;
58+
import org.springframework.data.solr.core.query.Field;
59+
import org.springframework.data.solr.core.query.FilterQuery;
60+
import org.springframework.data.solr.core.query.Function;
4561
import org.springframework.data.solr.core.query.Function.Context.Target;
62+
import org.springframework.data.solr.core.query.GroupOptions;
63+
import org.springframework.data.solr.core.query.HighlightOptions;
4664
import org.springframework.data.solr.core.query.HighlightOptions.FieldWithHighlightParameters;
4765
import org.springframework.data.solr.core.query.HighlightOptions.HighlightParameter;
66+
import org.springframework.data.solr.core.query.HighlightQuery;
67+
import org.springframework.data.solr.core.query.PivotField;
68+
import org.springframework.data.solr.core.query.Query;
69+
import org.springframework.data.solr.core.query.QueryParameter;
70+
import org.springframework.data.solr.core.query.SolrDataQuery;
71+
import org.springframework.data.solr.core.query.SpellcheckOptions;
72+
import org.springframework.data.solr.core.query.StatsOptions;
4873
import org.springframework.lang.Nullable;
4974
import org.springframework.util.Assert;
5075
import org.springframework.util.CollectionUtils;
@@ -65,6 +90,7 @@
6590
* @author Joachim Uhrlaß
6691
* @author Petar Tahchiev
6792
* @author Juan Manuel de Blas
93+
* @author Matthew Hall
6894
*/
6995
public class DefaultQueryParser extends QueryParserBase<SolrDataQuery> {
7096

@@ -109,6 +135,10 @@ public final SolrQuery doConstructSolrQuery(SolrDataQuery query, @Nullable Class
109135
processHighlightOptions(solrQuery, (HighlightQuery) query, domainType);
110136
}
111137

138+
if (query instanceof DisMaxQuery) {
139+
processDisMaxOptions(solrQuery, (DisMaxQuery) query);
140+
}
141+
112142
return solrQuery;
113143
}
114144

@@ -132,6 +162,38 @@ private void processQueryOptions(SolrQuery solrQuery, Query query, @Nullable Cla
132162
LOGGER.debug("Constructed SolrQuery:\r\n {}", solrQuery);
133163
}
134164

165+
protected void processDisMaxOptions(SolrQuery solrQuery, DisMaxQuery disMaxQuery) {
166+
167+
if (disMaxQuery == null || disMaxQuery.getDisMaxOptions() == null) {
168+
return;
169+
}
170+
171+
DisMaxOptions disMaxOptions = disMaxQuery.getDisMaxOptions();
172+
173+
solrQuery.set("defType", "dismax");
174+
175+
setSolrParamIfPresent(solrQuery, DF, disMaxOptions.getDefaultField());
176+
177+
setSolrParamIfPresent(solrQuery, ALTQ, disMaxOptions.getAltQuery());
178+
setSolrParamIfPresent(solrQuery, QF, disMaxOptions.getQueryFunction());
179+
setSolrParamIfPresent(solrQuery, MM, disMaxOptions.getMinimumMatch());
180+
181+
setSolrParamIfPresent(solrQuery, BQ, disMaxOptions.getBoostQuery());
182+
setSolrParamIfPresent(solrQuery, BF, disMaxOptions.getBoostFunction());
183+
setSolrParamIfPresent(solrQuery, PF, disMaxOptions.getPhraseFunction());
184+
185+
setSolrParamIfPresent(solrQuery, PS, disMaxOptions.getPhraseSlop() == null ? null :
186+
String.valueOf(disMaxOptions.getPhraseSlop()));
187+
setSolrParamIfPresent(solrQuery, QS, disMaxOptions.getQuerySlop() == null ? null : String.valueOf(disMaxOptions.getQuerySlop()));
188+
setSolrParamIfPresent(solrQuery, TIE, disMaxOptions.getTie() == null ? null : String.valueOf(disMaxOptions.getTie()));
189+
}
190+
191+
private static void setSolrParamIfPresent(SolrQuery solrQuery, String param, String value) {
192+
if (!org.springframework.util.StringUtils.isEmpty(value)) {
193+
solrQuery.setParam(param, value);
194+
}
195+
}
196+
135197
private void processFacetOptions(SolrQuery solrQuery, FacetQuery query, @Nullable Class<?> domainType) {
136198

137199
if (enableFaceting(solrQuery, query)) {

src/main/java/org/springframework/data/solr/core/QueryParserBase.java

+26
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
* @author Radek Mensik
6363
* @author David Webb
6464
* @author Michael Rocke
65+
* @author Matthew Hall
6566
*/
6667
public abstract class QueryParserBase<QUERYTPYE extends SolrDataQuery> implements QueryParser {
6768

@@ -1064,6 +1065,31 @@ public Map<String, Object> getNamesAssociation() {
10641065
}
10651066
}
10661067

1068+
/**
1069+
* @author Matthew Hall
1070+
* @since 4.1
1071+
*/
1072+
static class NamedObjectsDisMaxQuery extends AbstractDisMaxQueryDecorator implements NamedObjects {
1073+
1074+
private Map<String, Object> namesAssociation = new HashMap<>();
1075+
1076+
public NamedObjectsDisMaxQuery(DisMaxQuery query) {
1077+
super(query);
1078+
}
1079+
1080+
@Override
1081+
public void setName(Object object, String name) {
1082+
setObjectName(namesAssociation, object, name);
1083+
}
1084+
1085+
@Override
1086+
public Map<String, Object> getNamesAssociation() {
1087+
return Collections.unmodifiableMap(namesAssociation);
1088+
}
1089+
1090+
}
1091+
1092+
10671093
/**
10681094
* Create new new {@link Context} for rendering {@link Function functions}.
10691095
*

src/main/java/org/springframework/data/solr/core/SolrOperations.java

+26-1
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@
1818
import java.time.Duration;
1919
import java.util.Collection;
2020
import java.util.Optional;
21-
2221
import org.apache.solr.client.solrj.SolrClient;
2322
import org.apache.solr.client.solrj.response.SolrPingResponse;
2423
import org.apache.solr.client.solrj.response.UpdateResponse;
2524
import org.apache.solr.common.SolrInputDocument;
2625
import org.springframework.data.domain.Page;
2726
import org.springframework.data.solr.core.convert.SolrConverter;
27+
import org.springframework.data.solr.core.query.DisMaxQuery;
2828
import org.springframework.data.solr.core.query.FacetAndHighlightQuery;
2929
import org.springframework.data.solr.core.query.FacetQuery;
3030
import org.springframework.data.solr.core.query.HighlightQuery;
@@ -37,6 +37,7 @@
3737
import org.springframework.data.solr.core.query.result.GroupPage;
3838
import org.springframework.data.solr.core.query.result.HighlightPage;
3939
import org.springframework.data.solr.core.query.result.ScoredPage;
40+
import org.springframework.data.solr.core.query.result.SolrResultPage;
4041
import org.springframework.data.solr.core.query.result.StatsPage;
4142
import org.springframework.data.solr.core.query.result.TermsPage;
4243
import org.springframework.data.solr.core.schema.SchemaOperations;
@@ -50,6 +51,7 @@
5051
* @author Francisco Spaeth
5152
* @author Shiradwade Sateesh Krishna
5253
* @author David Webb
54+
* @author Matthew Hall
5355
*/
5456
public interface SolrOperations {
5557

@@ -455,6 +457,29 @@ <T> FacetAndHighlightPage<T> queryForFacetAndHighlightPage(String collection, Fa
455457
*/
456458
<T> StatsPage<T> queryForStatsPage(String collection, Query query, Class<T> clazz, RequestMethod method);
457459

460+
/**
461+
* Execute the query against Solr and return result as {@link StatsPage}.
462+
*
463+
* @param collection must not be {@literal null}.
464+
* @param query must not be {@literal null}.
465+
* @param clazz must not be {@literal null}.
466+
* @return never {@literal null}.
467+
* @since 4.1
468+
*/
469+
<T> SolrResultPage<T> queryForDisMaxPage(String collection, DisMaxQuery query, Class<T> clazz);
470+
471+
/**
472+
* Execute the query against Solr and return result as {@link StatsPage}.
473+
*
474+
* @param collection must not be {@literal null}.
475+
* @param query must not be {@literal null}.
476+
* @param clazz must not be {@literal null}.
477+
* @param requestMethod must not be {@literal null}.
478+
* @return never {@literal null}.
479+
* @since 4.1
480+
*/
481+
public <T> SolrResultPage<T> queryForDisMaxPage(String collection, DisMaxQuery query, Class<T> clazz,
482+
@Nullable RequestMethod requestMethod);
458483
/**
459484
* Execute the query against Solr and return result as page.
460485
*

src/main/java/org/springframework/data/solr/core/SolrTemplate.java

+32-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import java.util.Optional;
2828
import java.util.Set;
2929
import java.util.stream.Collectors;
30-
3130
import org.apache.solr.client.solrj.SolrClient;
3231
import org.apache.solr.client.solrj.SolrQuery;
3332
import org.apache.solr.client.solrj.SolrRequest;
@@ -52,6 +51,7 @@
5251
import org.springframework.data.domain.Pageable;
5352
import org.springframework.data.mapping.context.MappingContext;
5453
import org.springframework.data.solr.UncategorizedSolrException;
54+
import org.springframework.data.solr.core.QueryParserBase.NamedObjectsDisMaxQuery;
5555
import org.springframework.data.solr.core.QueryParserBase.NamedObjectsFacetAndHighlightQuery;
5656
import org.springframework.data.solr.core.QueryParserBase.NamedObjectsFacetQuery;
5757
import org.springframework.data.solr.core.QueryParserBase.NamedObjectsHighlightQuery;
@@ -62,14 +62,25 @@
6262
import org.springframework.data.solr.core.mapping.SolrPersistentEntity;
6363
import org.springframework.data.solr.core.mapping.SolrPersistentProperty;
6464
import org.springframework.data.solr.core.query.AbstractQueryDecorator;
65+
import org.springframework.data.solr.core.query.DisMaxQuery;
6566
import org.springframework.data.solr.core.query.FacetAndHighlightQuery;
6667
import org.springframework.data.solr.core.query.FacetQuery;
6768
import org.springframework.data.solr.core.query.HighlightQuery;
6869
import org.springframework.data.solr.core.query.Query;
6970
import org.springframework.data.solr.core.query.SolrDataQuery;
7071
import org.springframework.data.solr.core.query.TermsQuery;
71-
import org.springframework.data.solr.core.query.result.*;
72+
import org.springframework.data.solr.core.query.result.Cursor;
73+
import org.springframework.data.solr.core.query.result.DelegatingCursor;
74+
import org.springframework.data.solr.core.query.result.FacetAndHighlightPage;
75+
import org.springframework.data.solr.core.query.result.FacetPage;
76+
import org.springframework.data.solr.core.query.result.GroupPage;
77+
import org.springframework.data.solr.core.query.result.HighlightPage;
78+
import org.springframework.data.solr.core.query.result.ScoredPage;
79+
import org.springframework.data.solr.core.query.result.SolrResultPage;
7280
import org.springframework.data.solr.core.query.result.SpellcheckQueryResult.Alternative;
81+
import org.springframework.data.solr.core.query.result.StatsPage;
82+
import org.springframework.data.solr.core.query.result.TermsPage;
83+
import org.springframework.data.solr.core.query.result.TermsResultPage;
7384
import org.springframework.data.solr.core.schema.DefaultSchemaOperations;
7485
import org.springframework.data.solr.core.schema.SchemaOperations;
7586
import org.springframework.data.solr.core.schema.SolrPersistentEntitySchemaCreator;
@@ -91,6 +102,7 @@
91102
* @author Petar Tahchiev
92103
* @author Mark Paluch
93104
* @author Juan Manuel de Blas
105+
* @author Matthew Hall
94106
*/
95107
public class SolrTemplate implements SolrOperations, InitializingBean, ApplicationContextAware {
96108

@@ -431,6 +443,24 @@ public <T> FacetAndHighlightPage<T> queryForFacetAndHighlightPage(String collect
431443
return createSolrResultPage(query, clazz, response, objectsName);
432444
}
433445

446+
@Override
447+
public <T> SolrResultPage<T> queryForDisMaxPage(String collection, DisMaxQuery query, Class<T> clazz) {
448+
return queryForDisMaxPage(collection, query, clazz, null);
449+
}
450+
451+
@Override
452+
public <T> SolrResultPage<T> queryForDisMaxPage(String collection, DisMaxQuery query, Class<T> clazz,
453+
@Nullable RequestMethod requestMethod) {
454+
455+
QueryResponse response;
456+
NamedObjectsDisMaxQuery namedObjectsQuery = new NamedObjectsDisMaxQuery(query);
457+
response = querySolr(collection, namedObjectsQuery, clazz,
458+
requestMethod != null ? requestMethod : getDefaultRequestMethod());
459+
Map<String, Object> objectsName = namedObjectsQuery.getNamesAssociation();
460+
461+
return createSolrResultPage(query, clazz, response, objectsName);
462+
}
463+
434464
private <T> SolrResultPage<T> createSolrResultPage(Query query, Class<T> clazz, QueryResponse response,
435465
Map<String, Object> objectsName) {
436466

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.springframework.data.solr.core.query;
2+
3+
import org.springframework.lang.Nullable;
4+
5+
6+
/**
7+
* General purpose {@link DisMaxQuery} decorator.
8+
*
9+
* @author Matthew Hall
10+
* @since 4.1.0
11+
*/
12+
public abstract class AbstractDisMaxQueryDecorator extends AbstractQueryDecorator implements DisMaxQuery {
13+
14+
private DisMaxQuery query;
15+
16+
public AbstractDisMaxQueryDecorator(DisMaxQuery query) {
17+
super(query);
18+
this.query = query;
19+
}
20+
21+
@Nullable
22+
@Override
23+
public DisMaxOptions getDisMaxOptions() {
24+
return this.query.getDisMaxOptions();
25+
}
26+
27+
public <T extends SolrDataQuery> T setDisMaxOptions(DisMaxOptions disMaxOptions) {
28+
return this.query.setDisMaxOptions(disMaxOptions);
29+
}
30+
}

0 commit comments

Comments
 (0)