2015-04-29 103 views
0

是否可以使用Spring Data Solr將附加參數添加到Solr查詢中,從而生成以下請求?在使用Spring Data Solr時向Solr添加自定義參數

"params": { 
    "indent": "true", 
    "q": "*.*", 
    "_": "1430295713114", 
    "wt": "java", 
    "AuthenticatedUserName": "[email protected]" 
} 

我要添加被Apache Manifoldcf,AuthenticatedUserName和其值所需要的參數,沿着由彈簧數據Solr的自動填充的其他的(Q,重量)。

謝謝 五

回答

0

我設法使它通過查看SolrTemplate類的源代碼工作,但我不知道是否有一個不太侵入性的解決方案。

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)); 
}