Skip to content

Commit

Permalink
Customize solr indexer and searcher with beans
Browse files Browse the repository at this point in the history
  • Loading branch information
paulcwarren committed Aug 13, 2020
1 parent 9d42c77 commit 344bb2f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
Expand Up @@ -14,6 +14,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.content.commons.repository.StoreAccessException;
import org.springframework.content.commons.search.Searchable;
import org.springframework.content.solr.FilterQueryProvider;
import org.springframework.content.solr.SolrProperties;
import org.springframework.data.domain.Pageable;
import org.springframework.util.Assert;
Expand All @@ -25,13 +26,19 @@ public class SearchableImpl implements Searchable<Object> {
private SolrClient solr;
private SolrProperties solrProperties;
private Class<?> domainClass;
private FilterQueryProvider filterProvider;

@Autowired
public SearchableImpl(SolrClient solr, SolrProperties solrProperties) {
this.solr = solr;
this.solrProperties = solrProperties;
}

@Autowired(required=false)
public void setFilterQueryProvider(FilterQueryProvider provider) {
this.filterProvider = provider;
}

public void setDomainClass(Class<?> domainClass) {
this.domainClass = domainClass;
}
Expand Down Expand Up @@ -148,6 +155,11 @@ public List<Object> findAllKeywordsWithWeights(String[] terms, double[] weights)
/* package */ NamedList<Object> executeQuery(Class<?> domainClass, String queryString, Pageable pageable) {
SolrQuery query = new SolrQuery();
query.setQuery("(" + queryString + ") AND id:" + domainClass.getCanonicalName() + "\\:*");

if (this.filterProvider != null) {
query.setFilterQueries(this.filterProvider.filterQueries(domainClass));
}

query.setFields(field);

if (pageable != null) {
Expand Down
Expand Up @@ -2,6 +2,8 @@

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrServerException;
Expand All @@ -14,6 +16,7 @@
import org.springframework.content.commons.repository.StoreAccessException;
import org.springframework.content.commons.search.IndexService;
import org.springframework.content.commons.utils.BeanUtils;
import org.springframework.content.solr.AttributeSyncer;
import org.springframework.content.solr.SolrProperties;

import static java.lang.String.format;
Expand All @@ -23,13 +26,19 @@ public class SolrFulltextIndexServiceImpl implements IndexService {

private final SolrClient solrClient;
private final SolrProperties properties;
private AttributeSyncer<Object> syncer;

@Autowired
public SolrFulltextIndexServiceImpl(SolrClient solrClient, SolrProperties properties) {
this.solrClient = solrClient;
this.properties = properties;
}

@Autowired(required=false)
public void setAttributeSyncer(AttributeSyncer syncer) {
this.syncer = syncer;
}

@Override
public void index(Object entity, InputStream content) {

Expand All @@ -41,6 +50,12 @@ public void index(Object entity, InputStream content) {
up.addContentStream(new ContentEntityStream(content));
String id = BeanUtils.getFieldWithAnnotation(entity, ContentId.class).toString();
up.setParam("literal.id", entity.getClass().getCanonicalName() + ":" + id);
if (syncer != null) {
Map<String,String> attributesToSync = syncer.synchronize(entity);
for (Entry<String,String> entry : attributesToSync.entrySet()) {
up.setParam(format("literal.%s", entry.getKey()), entry.getValue());
}
}
up.setAction(COMMIT,true, true);

try {
Expand Down
@@ -0,0 +1,8 @@
package org.springframework.content.solr;

import java.util.Map;

public interface AttributeSyncer<S> {

Map<String, String> synchronize(S entity);
}
@@ -0,0 +1,6 @@
package org.springframework.content.solr;

public interface FilterQueryProvider {

String[] filterQueries(Class<?> entity);
}

0 comments on commit 344bb2f

Please sign in to comment.