Example usage for org.apache.solr.common.util NamedList get

List of usage examples for org.apache.solr.common.util NamedList get

Introduction

In this page you can find the example usage for org.apache.solr.common.util NamedList get.

Prototype

public T get(String name) 

Source Link

Document

Gets the value for the first instance of the specified name found.

Usage

From source file:actors.SolrActor.java

License:Apache License

public void indexUpdated(SolrIndexEvent msg) {
    try {//from w w w .  ja  v  a 2  s  .  c o  m
        System.out.println("SolrIndexEvent");
        SolrInputDocument doc = msg.getDocuement();
        //Making realtime GET
        System.out.println("GET");
        SolrQuery parameters = new SolrQuery();
        parameters.setRequestHandler("/get");
        String f1 = doc.getFieldValue("literal.id").toString();
        String f2 = doc.getFieldValue("literal.rev").toString();
        parameters.set("id", f1);
        parameters.set("rev", f2);
        //System.out.println(parameters);

        QueryResponse response = server.query(parameters);

        NamedList<Object> result = response.getResponse();
        //System.out.println(response.getResponse());
        //System.out.println(result.size() );
        //System.out.println();
        //System.out.println(result);
        //validate the doc exists
        if (result == null || result.get("doc") == null) {
            System.out.println("/update/extract");
            ContentStreamUpdateRequest req = new ContentStreamUpdateRequest("/update/extract");
            // url dropbox
            URL url = new URL(doc.getFieldValue("literal.links").toString());
            ContentStreamBase content = new ContentStreamBase.URLStream(url);
            System.out.println("ContentStreamBase");
            req.addContentStream(content);
            // Adittionall metadata
            req.setParam("literal.id", doc.getFieldValue("literal.id").toString());
            req.setParam("literal.title", doc.getFieldValue("literal.title").toString());
            req.setParam("literal.rev", doc.getFieldValue("literal.rev").toString());
            req.setParam("literal.when", doc.getFieldValue("literal.when").toString());
            req.setParam("literal.path", doc.getFieldValue("literal.path").toString());
            req.setParam("literal.icon", doc.getFieldValue("literal.icon").toString());
            req.setParam("literal.size", doc.getFieldValue("literal.size").toString());
            req.setParam("literal.url", doc.getFieldValue("literal.links").toString());

            req.setParam("uprefix", "attr_");
            req.setParam("fmap.content", "attr_content");
            req.setAction(AbstractUpdateRequest.ACTION.COMMIT, true, true);
            //Requesting Solr
            result = server.request(req);
            //System.out.println("Result: " + result.toString());

        } else {
            System.out.println("It's already update");

        }

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:at.newmedialab.lmf.util.solr.suggestion.service.SuggestionService.java

License:Apache License

public SuggestionService(SolrCore solrCore, NamedList args) {

    NamedList l = new NamedList();

    //set spellcheck component if there is one
    if (((ArrayList) args.get("first-components")).contains("spellcheck")) {
        List component = new ArrayList<String>();
        component.add("spellcheck");
        l.add("first-components", component);
        spellcheck_enabled = true;//from w  w w .  jav  a  2s  . co  m
    }

    this.solrCore = solrCore;
    this.searchHandler = new SearchHandler();
    this.searchHandler.init(l);
    this.searchHandler.inform(solrCore);
}

From source file:at.newmedialab.lmf.util.solr.SuggestionRequestHandler.java

License:Apache License

@SuppressWarnings({ "unchecked", "rawtypes" })
public void inform(SolrCore core) {
    super.inform(core);
    suggestionService = new SuggestionService(core, this.getInitArgs());

    //set default args
    NamedList args = (NamedList) this.getInitArgs().get("defaults");

    SUGGESTION = args.get(SuggestionRequestParams.SUGGESTION) != null
            ? Boolean.parseBoolean((String) args.get(SuggestionRequestParams.SUGGESTION))
            : SUGGESTION;/*from www .  j  a  v a 2  s. c o m*/

    MULTIVALUE = args.get(SuggestionRequestParams.SUGGESTION_MULTIVALUE) != null
            ? Boolean.parseBoolean((String) args.get(SuggestionRequestParams.SUGGESTION_MULTIVALUE))
            : MULTIVALUE;

    LIMIT = args.get(SuggestionRequestParams.SUGGESTION_LIMIT) != null
            ? Integer.parseInt((String) args.get(SuggestionRequestParams.SUGGESTION_LIMIT))
            : LIMIT;

    DF = args.get(SuggestionRequestParams.SUGGESTION_DF) != null
            ? (String) args.get(SuggestionRequestParams.SUGGESTION_DF)
            : DF;

    List<String> fields = args.getAll(SuggestionRequestParams.SUGGESTION_FIELD) != null
            ? args.getAll(SuggestionRequestParams.SUGGESTION_FIELD)
            : Collections.emptyList();
    if (!fields.isEmpty()) {
        FIELDS = fields.toArray(new String[fields.size()]);
    }

    List<String> fqs = args.getAll(CommonParams.FQ) != null ? args.getAll(CommonParams.FQ)
            : Collections.emptyList();
    if (!fqs.isEmpty()) {
        FQS = fqs.toArray(new String[fields.size()]);
    }

}

From source file:com.constellio.data.dao.services.bigVault.CustomizedSpellCheckResponse.java

License:Apache License

public CustomizedSpellCheckResponse(NamedList<Object> spellInfo) {
    Object sugg = spellInfo.get("suggestions");
    if (sugg == null) {
        correctlySpelled = true;/*from   www . j  a v a  2 s  .  c o  m*/
        return;
    }
    for (int i = 0; i < spellInfo.size(); i++) {
        String n = spellInfo.getName(i);
        if ("correctlySpelled".equals(n)) {
            correctlySpelled = (Boolean) spellInfo.getVal(i);
        } else if ("collationInternalRank".equals(n)) {
            //continue;
        } else if ("collations".equals(n)) {
            List<Object> collationInfo = spellInfo.getAll(n);
            collations = new ArrayList<>(collationInfo.size());
            for (Object o : collationInfo) {
                if (o instanceof String) {
                    collations.add(new Collation().setCollationQueryString((String) o));
                } else if (o instanceof NamedList) {
                    @SuppressWarnings("unchecked")
                    NamedList<Object> expandedCollation = (NamedList<Object>) o;
                    String collationQuery = (String) expandedCollation.get("collationQuery");
                    int hits = (Integer) expandedCollation.get("hits");
                    @SuppressWarnings("unchecked")
                    NamedList<String> misspellingsAndCorrections = (NamedList<String>) expandedCollation
                            .get("misspellingsAndCorrections");

                    Collation collation = new Collation();
                    collation.setCollationQueryString(collationQuery);
                    collation.setNumberOfHits(hits);

                    for (int ii = 0; ii < misspellingsAndCorrections.size(); ii++) {
                        String misspelling = misspellingsAndCorrections.getName(ii);
                        String correction = misspellingsAndCorrections.getVal(ii);
                        collation.addMisspellingsAndCorrection(new Correction(misspelling, correction));
                    }
                    collations.add(collation);
                } else {
                    throw new AssertionError("Should get Lists of Strings or List of NamedLists here.");
                }
            }
        } else {
            @SuppressWarnings("unchecked")
            Suggestion s = new Suggestion(n, (NamedList<Object>) spellInfo.getVal(i));
            suggestionMap.put(n, s);
            suggestions.add(s);
        }
    }
}

From source file:com.doculibre.constellio.opensearch.OpenSearchSolrServer.java

License:Open Source License

public static void main(String[] args) throws Exception {
    SolrDocumentList solrDocumentList;/*from   w ww. j a  v  a 2 s .  c o  m*/
    //        
    // InputStream inputXML = OpenSearchSolrServer.class.getResourceAsStream("opensearch_example.xml");
    // SAXReader saxReader = new SAXReader();
    // Document doc = saxReader.read(inputXML);
    // IOUtils.closeQuietly(inputXML);
    //
    // System.out.println("Mock request");
    // solrDocumentList = parse(doc.getRootElement());
    // printResults(solrDocumentList);

    System.out.println("Real request");
    OpenSearchSolrServer solrServer = new OpenSearchSolrServer();
    ModifiableSolrParams params = new ModifiableSolrParams();
    params.add("openSearchURL", "http://recherched.gouv.qc.ca/internet/opensearch");
    params.add(CommonParams.Q, "doculibre");
    params.add(CommonParams.START, "5");
    params.add(CommonParams.ROWS, "10");
    params.add("lang", "en");
    NamedList<Object> results = solrServer.request(new QueryRequest(params));
    solrDocumentList = (SolrDocumentList) results.get("response");
    printResults(solrDocumentList);

    QueryResponse queryResponse = solrServer.query(params);
    solrDocumentList = queryResponse.getResults();
    printResults(solrDocumentList);
}

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

License:Open Source License

@SuppressWarnings("unchecked")
static private NamedList<Object> suggest(String q, String fieldName, SolrServer server, Boolean isStringField) {
    NamedList<Object> returnList = new NamedList<Object>();

    // escape special characters
    SolrQuery query = new SolrQuery();

    /*/*from   w  w w.  j a  v a  2s .c  o m*/
     * // * Set terms.lower to the input term
     * query.setParam(TermsParams.TERMS_LOWER, q); // * Set terms.prefix to
     * the input term query.setParam(TermsParams.TERMS_PREFIX, q); // * Set
     * terms.lower.incl to false
     * query.setParam(TermsParams.TERMS_LOWER_INCLUSIVE, "false"); // * Set
     * terms.fl to the name of the source field
     * query.setParam(TermsParams.TERMS_FIELD, fieldName);
     */

    query.setParam(TermsParams.TERMS_FIELD, fieldName);// query.addTermsField("spell");
    query.setParam(TermsParams.TERMS_LIMIT, TERMS_LIMIT);// query.setTermsLimit(MAX_TERMS);
    query.setParam(TermsParams.TERMS, "true");// query.setTerms(true);
    query.setParam(TermsParams.TERMS_LOWER, q);// query.setTermsLower(q);
    query.setParam(TermsParams.TERMS_PREFIX, q);// query.setTermsPrefix(q);
    query.setParam(TermsParams.TERMS_MINCOUNT, TERMS_MINCOUNT);

    query.setRequestHandler(SolrServices.AUTOCOMPLETE_QUERY_NAME);

    try {
        QueryResponse qr = server.query(query);
        NamedList<Object> values = qr.getResponse();
        NamedList<Object> terms = (NamedList<Object>) values.get("terms");// TermsResponse
        // resp
        // =
        // qr.getTermsResponse();
        NamedList<Object> suggestions = (NamedList<Object>) terms.get(fieldName);// items
        // =
        // resp.getTerms("spell");
        if (!isStringField) {
            q = AnalyzerUtils.analyzePhrase(q, false);
        }

        for (int i = 0; i < suggestions.size(); i++) {
            String currentSuggestion = suggestions.getName(i);
            // System.out.println(currentSuggestion);
            if (isStringField) {
                if (currentSuggestion.contains(q)) {
                    // String suffix =
                    // StringUtils.substringAfter(currentSuggestion, q);
                    // if (suffix.isEmpty() &&
                    // !currentSuggestion.equals(q)){
                    // //q n est pas dans currentSuggestion
                    // break;
                    // }
                    if (currentSuggestion.contains(SPECIAL_CHAR)) {
                        // le resultat de recherche retourne des fois une
                        // partie de la valeur existant
                        // dans le champ!
                        currentSuggestion = StringUtils.substringAfter(currentSuggestion, SPECIAL_CHAR);
                    }
                    returnList.add(currentSuggestion, suggestions.getVal(i));
                }
            } else {
                currentSuggestion = AnalyzerUtils.analyzePhrase(currentSuggestion, false);
                if (currentSuggestion.contains(q)) {
                    returnList.add(currentSuggestion, suggestions.getVal(i));
                }
            }
        }

    } catch (SolrServerException e) {
        throw new RuntimeException(e);
    }
    return returnList;
}

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

License:Open Source License

@SuppressWarnings("unchecked")
@Override//from   w ww.  ja  v  a 2s  .  c om
public List<SimpleOrderedMap<Object>> cluster(SimpleSearch simpleSearch, CollectionFacet facet, int start,
        int row, ConstellioUser user) {
    //      String luceneQuery = simpleSearch.getLuceneQuery();
    RecordCollectionServices collectionServices = ConstellioSpringUtils.getRecordCollectionServices();
    String solrServerName = simpleSearch.getCollectionName();
    RecordCollection collection = collectionServices.get(solrServerName);

    //Remarque useDismax est  false car nous allons utiliser le type de requete "/clustering"
    //         donc pas besoin d'ajouter les paramtres necessaires  dismax
    SolrQuery query = SearchServicesImpl.toSolrQuery(simpleSearch, false, true, true);
    query.setRequestHandler("/clustering");

    query.setParam(ClusteringComponent.COMPONENT_NAME, Boolean.TRUE);
    query.setParam(ClusteringParams.USE_COLLECTION, facet.isClusteringUseCollection());
    query.setParam(ClusteringParams.USE_SEARCH_RESULTS, facet.isClusteringUseSearchResults());
    query.setParam(ClusteringParams.USE_DOC_SET, facet.isClusteringUseDocSet());
    query.setParam(ClusteringParams.ENGINE_NAME, facet.getClusteringEngine());

    //The maximum number of labels to produce
    query.setParam(CarrotParams.NUM_DESCRIPTIONS, "" + maxClusters);
    //      query.setParam(CarrotParams.NUM_DESCRIPTIONS, "" + facet.getCarrotNumDescriptions());

    query.setParam(CarrotParams.OUTPUT_SUB_CLUSTERS, facet.isCarrotOutputSubclusters());
    query.setParam(ConstellioSolrQueryParams.LUCENE_QUERY, simpleSearch.getLuceneQuery());
    query.setParam(ConstellioSolrQueryParams.COLLECTION_NAME, simpleSearch.getCollectionName());
    query.setParam(ConstellioSolrQueryParams.SIMPLE_SEARCH, simpleSearch.toSimpleParams().toString());
    if (user != null) {
        query.setParam(ConstellioSolrQueryParams.USER_ID, "" + user.getId());
    }

    //If true, then the snippet field (if no snippet field, then the title field) will be highlighted and the highlighted text will be used for the snippet. 
    query.setParam(CarrotParams.PRODUCE_SUMMARY, facet.isCarrotProduceSummary());
    if (facet.getCarrotTitleField() != null) {
        IndexField indexField = facet.getCarrotTitleField();
        query.setParam(CarrotParams.TITLE_FIELD_NAME, indexField.getName());
    }
    if (facet.getCarrotUrlField() != null) {
        IndexField indexField = facet.getCarrotUrlField();
        query.setParam(CarrotParams.URL_FIELD_NAME, indexField.getName());
    }
    if (facet.getCarrotSnippetField() != null) {
        IndexField indexField = facet.getCarrotSnippetField();
        query.setParam(CarrotParams.SNIPPET_FIELD_NAME, indexField.getName());
    }

    // Requte Lucene
    //      query.setQuery(luceneQuery);

    // nb rsultats par page
    query.setRows(row);

    // page de dbut
    query.setStart(start);
    //Les resultats ne vont pas etre affichs
    /*query.setHighlight(true);
    query.setHighlightFragsize(100);
    query.setHighlightSnippets(2);*/

    if (collection.isOpenSearch()) {
        query.setParam("openSearchURL", collection.getOpenSearchURL());
    }

    SolrServer server = SolrCoreContext.getSolrServer(solrServerName);

    QueryResponse queryResponse;
    try {
        queryResponse = server.query(query);
    } catch (SolrServerException e) {
        throw new RuntimeException(e);
    }

    NamedList<Object> values = queryResponse.getResponse();
    Object clusters = values.get("clusters");
    return (List<SimpleOrderedMap<Object>>) clusters;
}

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

License:Open Source License

@SuppressWarnings("unchecked")
@Override//  w ww . j  a va2s . c om
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;
}

From source file:com.doculibre.constellio.servlets.SolrJExampleMain.java

License:Open Source License

/**
 * Print documents and facets/*from   w  ww  .java2 s .  c  o  m*/
 * 
 * @param response
 */
@SuppressWarnings("unchecked")
public static void print(QueryResponse response) {
    SolrDocumentList docs = response.getResults();
    if (docs != null) {
        System.out.println(docs.getNumFound() + " documents found, " + docs.size() + " returned : ");
        for (int i = 0; i < docs.size(); i++) {
            SolrDocument doc = docs.get(i);
            System.out.println("\t" + doc.toString());
        }
    }

    List<FacetField> fieldFacets = response.getFacetFields();
    if (fieldFacets != null && fieldFacets.isEmpty()) {
        System.out.println("\nField Facets : ");
        for (FacetField fieldFacet : fieldFacets) {
            System.out.print("\t" + fieldFacet.getName() + " :\t");
            if (fieldFacet.getValueCount() > 0) {
                for (Count count : fieldFacet.getValues()) {
                    System.out.print(count.getName() + "[" + count.getCount() + "]\t");
                }
            }
            System.out.println("");
        }
    }

    Map<String, Integer> queryFacets = response.getFacetQuery();
    if (queryFacets != null && !queryFacets.isEmpty()) {
        System.out.println("\nQuery facets : ");
        for (String queryFacet : queryFacets.keySet()) {
            System.out.println("\t" + queryFacet + "\t[" + queryFacets.get(queryFacet) + "]");
        }
        System.out.println("");
    }

    NamedList<NamedList<Object>> spellCheckResponse = (NamedList<NamedList<Object>>) response.getResponse()
            .get("spellcheck");

    if (spellCheckResponse != null) {
        Iterator<Entry<String, NamedList<Object>>> wordsIterator = spellCheckResponse.iterator();

        while (wordsIterator.hasNext()) {
            Entry<String, NamedList<Object>> entry = wordsIterator.next();
            String word = entry.getKey();
            NamedList<Object> spellCheckWordResponse = entry.getValue();
            boolean correct = spellCheckWordResponse.get("frequency").equals(1);
            System.out.println("Word: " + word + ",\tCorrect?: " + correct);
            NamedList<Integer> suggestions = (NamedList<Integer>) spellCheckWordResponse.get("suggestions");
            if (suggestions != null && suggestions.size() > 0) {
                System.out.println("Suggestions : ");
                Iterator<Entry<String, Integer>> suggestionsIterator = suggestions.iterator();
                while (suggestionsIterator.hasNext()) {
                    System.out.println("\t" + suggestionsIterator.next().getKey());
                }

            }
            System.out.println("");
        }

    }

}

From source file:com.doculibre.constellio.solr.handler.component.SearchLogComponent.java

License:Apache License

@Override
@SuppressWarnings("unchecked")
public void init(NamedList args) {
    LOG.info("Initializing searchLogComponent");
    super.init(args);
    searchLogCoreName = StringUtils.defaultString((String) args.get("coreName"), "search_log");
    commitThreshold = (int) args.get("commitThreshold");
    searchLogCache = new ArrayList<SolrInputDocument>();
    localPort = 0;/*from   w w  w.  j a  v  a2 s  .c  om*/
    searchLogServer = null;
}