1

Is it possible to add an additional parameter to a Solr query using Spring Data Solr that generates the following request?

"params": {
  "indent": "true",
  "q": "*.*",
  "_": "1430295713114",
  "wt": "java",
  "AuthenticatedUserName": "user@domain.com"
}

I want to add a parameter needed by Apache Manifoldcf, AuthenticatedUserName and its value, alongside the other ones that are automatically populated by Spring Data Solr (q, wt).

Thank you, V.

virgium03
  • 627
  • 1
  • 5
  • 14

1 Answers1

1

I managed to make it work by looking at the source code of the SolrTemplate class but I was wondering if there is a less intrusive solution.

public Page<Document> searchDocuments(DocumentSearchCriteria criteria, Pageable page) {
    String[] words = criteria.getTitle().split(" ");
    Criteria conditions = createSearchConditions(words);
    SimpleQuery query = new SimpleQuery(conditions);
    query.setPageRequest(page);
    SolrQuery solrQuery = queryParsers.getForClass(query.getClass()).constructSolrQuery(query);
    solrQuery.add(AUTHENTICATED_USER_NAME, criteria.getLoggedUsername());
    try {
        String queryString = this.queryParsers.getForClass(query.getClass()).getQueryString(query);
        solrQuery.set(CommonParams.Q, queryString);
        QueryResponse response = solrTemplate.getSolrServer().query(solrQuery);

        List<Document> beans = convertQueryResponseToBeans(response, Document.class);
        SolrDocumentList results = response.getResults();

        return new SolrResultPage<>(beans, query.getPageRequest(), results.getNumFound(), results.getMaxScore());
    } catch (SolrServerException e) {
        log.error(e.getMessage(), e);
        return new SolrResultPage<>(Collections.<Document>emptyList());
    }
}

private  <T> List<T> convertQueryResponseToBeans(QueryResponse response, Class<T> targetClass) {
    return response != null ? convertSolrDocumentListToBeans(response.getResults(), targetClass) : Collections
            .<T> emptyList();
}

public <T> List<T> convertSolrDocumentListToBeans(SolrDocumentList documents, Class<T> targetClass) {
    if (documents == null) {
        return Collections.emptyList();
    }
    return solrTemplate.getConverter().read(documents, targetClass);
}

private Criteria createSearchConditions(String[] words) {
    return new Criteria("title").contains(words)
            .or(new Criteria("description").contains(words))
                    .or(new Criteria("content").contains(words))
                    .or(new Criteria("resourcename").contains(words));
}
virgium03
  • 627
  • 1
  • 5
  • 14