Example usage for org.apache.solr.common.params MoreLikeThisParams DOC_COUNT

List of usage examples for org.apache.solr.common.params MoreLikeThisParams DOC_COUNT

Introduction

In this page you can find the example usage for org.apache.solr.common.params MoreLikeThisParams DOC_COUNT.

Prototype

String DOC_COUNT

To view the source code for org.apache.solr.common.params MoreLikeThisParams DOC_COUNT.

Click Source Link

Usage

From source file:org.dspace.discovery.SolrServiceImpl.java

License:BSD License

@Override
public List<Item> getRelatedItems(Context context, Item item, DiscoveryMoreLikeThisConfiguration mltConfig) {
    List<Item> results = new ArrayList<Item>();
    try {/*from   ww  w .j ava  2s .c  o  m*/
        SolrQuery solrQuery = new SolrQuery();
        //Set the query to handle since this is unique
        solrQuery.setQuery("handle: " + item.getHandle());
        //Add the more like this parameters !
        solrQuery.setParam(MoreLikeThisParams.MLT, true);
        //Add a comma separated list of the similar fields
        @SuppressWarnings("unchecked")
        java.util.Collection<String> similarityMetadataFields = CollectionUtils
                .collect(mltConfig.getSimilarityMetadataFields(), new Transformer() {
                    @Override
                    public Object transform(Object input) {
                        //Add the mlt appendix !
                        return input + "_mlt";
                    }
                });

        solrQuery.setParam(MoreLikeThisParams.SIMILARITY_FIELDS,
                StringUtils.join(similarityMetadataFields, ','));
        solrQuery.setParam(MoreLikeThisParams.MIN_TERM_FREQ, String.valueOf(mltConfig.getMinTermFrequency()));
        solrQuery.setParam(MoreLikeThisParams.DOC_COUNT, String.valueOf(mltConfig.getMax()));
        solrQuery.setParam(MoreLikeThisParams.MIN_WORD_LEN, String.valueOf(mltConfig.getMinWordLength()));

        if (getSolr() == null) {
            return Collections.emptyList();
        }
        QueryResponse rsp = getSolr().query(solrQuery);
        NamedList mltResults = (NamedList) rsp.getResponse().get("moreLikeThis");
        if (mltResults != null && mltResults.get(item.getType() + "-" + item.getID()) != null) {
            SolrDocumentList relatedDocs = (SolrDocumentList) mltResults
                    .get(item.getType() + "-" + item.getID());
            for (Object relatedDoc : relatedDocs) {
                SolrDocument relatedDocument = (SolrDocument) relatedDoc;
                DSpaceObject relatedItem = findDSpaceObject(context, relatedDocument);
                if (relatedItem.getType() == Constants.ITEM) {
                    results.add((Item) relatedItem);
                }
            }
        }

    } catch (Exception e) {
        log.error(LogManager.getHeader(context, "Error while retrieving related items",
                "Handle: " + item.getHandle()), e);
    }
    return results;
}