Example usage for org.apache.solr.handler.admin LukeRequestHandler NUMTERMS

List of usage examples for org.apache.solr.handler.admin LukeRequestHandler NUMTERMS

Introduction

In this page you can find the example usage for org.apache.solr.handler.admin LukeRequestHandler NUMTERMS.

Prototype

String NUMTERMS

To view the source code for org.apache.solr.handler.admin LukeRequestHandler NUMTERMS.

Click Source Link

Usage

From source file:com.doculibre.constellio.services.IndexFieldServicesImpl.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override/*from  ww  w .  java2 s  .  co  m*/
public List<String> suggestValues(IndexField indexField, String text) {
    List<String> values = new ArrayList<String>();
    RecordCollection collection = indexField.getRecordCollection();
    SolrServices solrServices = ConstellioSpringUtils.getSolrServices();
    SolrServer solrServer = solrServices.getSolrServer(collection);
    if (solrServer != null) {
        SolrQuery query = new SolrQuery();
        query.setRequestHandler("/admin/luke");
        query.setParam(CommonParams.FL, indexField.getName());
        query.setParam(LukeRequestHandler.NUMTERMS, "" + 100);

        if (text != null) {
            query.setQuery(indexField.getName() + ":" + text + "*");
        }
        if (collection.isOpenSearch()) {
            query.setParam("openSearchURL", collection.getOpenSearchURL());
        }

        try {
            QueryResponse queryResponse = solrServer.query(query);
            NamedList<Object> fields = (NamedList<Object>) queryResponse.getResponse().get("fields");
            if (fields != null) {
                NamedList<Object> field = (NamedList<Object>) fields.get(indexField.getName());
                if (field != null) {
                    NamedList<Object> topTerms = (NamedList<Object>) field.get("topTerms");
                    if (topTerms != null) {
                        for (Map.Entry<String, Object> topTerm : topTerms) {
                            String topTermKey = topTerm.getKey();
                            if (text == null || topTermKey.toLowerCase().startsWith(text.toLowerCase())) {
                                values.add(topTerm.getKey());
                            }
                        }
                    }
                }
            }
        } catch (SolrServerException e) {
            throw new RuntimeException(e);
        }
    }
    return values;
}