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

Commit

Permalink
DATASOLR-153 - Added support for dismax queries.
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Hall committed Jul 23, 2019
1 parent 6d10043 commit a65189a
Show file tree
Hide file tree
Showing 9 changed files with 490 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -65,6 +90,7 @@
* @author Joachim Uhrlaß
* @author Petar Tahchiev
* @author Juan Manuel de Blas
* @author Matthew Hall
*/
public class DefaultQueryParser extends QueryParserBase<SolrDataQuery> {

Expand Down Expand Up @@ -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;
}

Expand All @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
* @author Radek Mensik
* @author David Webb
* @author Michael Rocke
* @author Matthew Hall
*/
public abstract class QueryParserBase<QUERYTPYE extends SolrDataQuery> implements QueryParser {

Expand Down Expand Up @@ -1064,6 +1065,31 @@ public Map<String, Object> getNamesAssociation() {
}
}

/**
* @author Matthew Hall
* @since 4.1
*/
static class NamedObjectsDisMaxQuery extends AbstractDisMaxQueryDecorator implements NamedObjects {

private Map<String, Object> namesAssociation = new HashMap<>();

public NamedObjectsDisMaxQuery(DisMaxQuery query) {
super(query);
}

@Override
public void setName(Object object, String name) {
setObjectName(namesAssociation, object, name);
}

@Override
public Map<String, Object> getNamesAssociation() {
return Collections.unmodifiableMap(namesAssociation);
}

}


/**
* Create new new {@link Context} for rendering {@link Function functions}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -50,6 +51,7 @@
* @author Francisco Spaeth
* @author Shiradwade Sateesh Krishna
* @author David Webb
* @author Matthew Hall
*/
public interface SolrOperations {

Expand Down Expand Up @@ -455,6 +457,29 @@ <T> FacetAndHighlightPage<T> queryForFacetAndHighlightPage(String collection, Fa
*/
<T> StatsPage<T> queryForStatsPage(String collection, Query query, Class<T> 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
*/
<T> SolrResultPage<T> queryForDisMaxPage(String collection, DisMaxQuery query, Class<T> 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 <T> SolrResultPage<T> queryForDisMaxPage(String collection, DisMaxQuery query, Class<T> clazz,
@Nullable RequestMethod requestMethod);
/**
* Execute the query against Solr and return result as page.
*
Expand Down
34 changes: 32 additions & 2 deletions src/main/java/org/springframework/data/solr/core/SolrTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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 {

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

@Override
public <T> SolrResultPage<T> queryForDisMaxPage(String collection, DisMaxQuery query, Class<T> clazz) {
return queryForDisMaxPage(collection, query, clazz, null);
}

@Override
public <T> SolrResultPage<T> queryForDisMaxPage(String collection, DisMaxQuery query, Class<T> clazz,
@Nullable RequestMethod requestMethod) {

QueryResponse response;
NamedObjectsDisMaxQuery namedObjectsQuery = new NamedObjectsDisMaxQuery(query);
response = querySolr(collection, namedObjectsQuery, clazz,
requestMethod != null ? requestMethod : getDefaultRequestMethod());
Map<String, Object> objectsName = namedObjectsQuery.getNamesAssociation();

return createSolrResultPage(query, clazz, response, objectsName);
}

private <T> SolrResultPage<T> createSolrResultPage(Query query, Class<T> clazz, QueryResponse response,
Map<String, Object> objectsName) {

Expand Down
Original file line number Diff line number Diff line change
@@ -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 extends SolrDataQuery> T setDisMaxOptions(DisMaxOptions disMaxOptions) {
return this.query.setDisMaxOptions(disMaxOptions);
}
}
Loading

0 comments on commit a65189a

Please sign in to comment.