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

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

Introduction

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

Prototype

public String getName(int idx) 

Source Link

Document

The name of the pair at the specified List index

Usage

From source file:co.diji.solr.XMLWriter.java

License:Apache License

public void writeNamedList(String name, NamedList val) throws IOException {
    int sz = val.size();
    startTag("lst", name, sz <= 0);

    for (int i = 0; i < sz; i++) {
        writeVal(val.getName(i), val.getVal(i));
    }//  w  w w . j a  v a 2s. co  m

    if (sz > 0) {
        writer.write("</lst>");
    }
}

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 ww w.ja  va 2 s. com*/
        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.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();

    /*/*  w  w w.ja v  a2 s  .c om*/
     * // * 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.AutocompleteServicesImpl.java

License:Open Source License

private static void print(String q, NamedList<Object> suggestions) {
    System.out.println("########### " + q + " ##############");
    for (int i = 0; i < suggestions.size(); i++) {
        System.out.println(suggestions.getName(i) + "\t" + suggestions.getVal(i));
    }//from ww w  .  j  av  a  2 s  .  co m
}

From source file:com.frank.search.solr.core.ResultHelper.java

License:Apache License

static Map<com.frank.search.solr.core.query.PivotField, List<FacetPivotFieldEntry>> convertFacetQueryResponseToFacetPivotMap(
        FacetQuery query, QueryResponse response) {

    if (VersionUtil.isSolr3XAvailable()) {
        // pivot facets are a solr 4+ Feature
        return Collections.emptyMap();
    }//  ww w  . ja va 2 s.c  o  m

    Map<com.frank.search.solr.core.query.PivotField, List<FacetPivotFieldEntry>> facetResult = new HashMap<com.frank.search.solr.core.query.PivotField, List<FacetPivotFieldEntry>>();
    NamedList<List<PivotField>> facetPivot = response.getFacetPivot();
    if (facetPivot != null && facetPivot.size() > 0) {
        for (int i = 0; i < facetPivot.size(); i++) {
            String name = facetPivot.getName(i);
            List<PivotField> pivotResult = facetPivot.get(name);
            facetResult.put(new SimplePivotField(name), convertPivotResult(pivotResult));
        }
    }

    return facetResult;
}

From source file:com.github.fengtan.sophie.tables.CoresTable.java

License:Open Source License

/**
 * Recursively convert a hierarchical NamedList into a linear Map.
 * /*from  ww  w  . java 2 s  .  c o m*/
 * @param namedList
 *            Hierarchical NamedList.
 * @param map
 *            Map to be populated.
 * @return Linear Map populated with values in the NamedList.
 */
private Map<String, String> linearizeNamedList(NamedList<?> namedList, Map<String, String> map) {
    // Inspect all elements in the NamedList.
    for (int idx = 0; idx < namedList.size(); idx++) {
        Object object = namedList.getVal(idx);
        if (object instanceof NamedList) {
            // Element is a NamedList: populate the map recursively.
            linearizeNamedList((NamedList<?>) object, map);
        } else {
            // Element is not a NamedList: add it to the map.
            String name = namedList.getName(idx);
            map.put(name, object.toString());
            // Create column if it does not exist yet.
            if (!hasColumn(name)) {
                addColumn(name);
            }
        }
    }
    // Return populated map.
    return map;
}

From source file:com.ifactory.press.db.solr.processor.FieldMergingProcessorFactory.java

License:Apache License

private void doInit() {
    Object o = initArgs.get("destinationField");
    if (o == null || !(o instanceof String)) {
        log.error("destinationField must be present as a string, got " + o);
        return;/*from   w ww .  ja v a 2 s  . c  o  m*/
    }
    destinationField = (String) o;
    FieldType destinationFieldType = schema.getFieldType(destinationField);
    if (destinationFieldType == null) {
        log.error("deistinationField is not defined in the schema: it has no schema type");
        return;
    }
    o = initArgs.get("sourceField");
    if (o == null || !(o instanceof NamedList)) {
        log.error("sourceField must be present as a list, got " + o);
        return;
    }
    NamedList<?> sourceFields = (NamedList<?>) o;
    if (sourceFields.size() == 0) {
        log.error("destinationField must not be empty");
    }
    sourceAnalyzers = new HashMap<String, Analyzer>();
    for (int i = 0; i < sourceFields.size(); i++) {
        String sourceFieldName = sourceFields.getName(i);
        o = sourceFields.getVal(i);
        FieldType fieldType;
        if (o instanceof String && !((String) o).isEmpty()) {
            String analysisFieldName = (String) o;
            fieldType = schema.getFieldTypeByName(analysisFieldName);
            if (fieldType == null) {
                log.error("No such field type: " + analysisFieldName);
            }
        } else {
            fieldType = schema.getFieldType(sourceFieldName);
            if (fieldType == null) {
                log.error("No field type for field: " + sourceFieldName);
            }
        }
        if (fieldType != null) {
            sourceAnalyzers.put(sourceFieldName, fieldType.getIndexAnalyzer());
        }
    }

}

From source file:com.nridge.ds.solr.SolrResponseBuilder.java

License:Open Source License

private void populateMoreLikeThis(QueryResponse aQueryResponse) {
    String docName;//from w  ww.j av  a2 s . c  om
    Document mltDocument;
    DataField docNameField;
    SolrDocumentList solrDocumentList;
    Logger appLogger = mAppMgr.getLogger(this, "populateMoreLikeThis");

    appLogger.trace(mAppMgr.LOGMSG_TRACE_ENTER);

    NamedList<SolrDocumentList> mltDocuments = aQueryResponse.getMoreLikeThis();
    if (mltDocuments != null) {
        mDocument.addRelationship(Solr.RESPONSE_MORE_LIKE_THIS, createMLTBag());
        Relationship mltRelationship = mDocument.getFirstRelationship(Solr.RESPONSE_MORE_LIKE_THIS);
        if (mltRelationship != null) {
            int mltCount = mltDocuments.size();
            DataBag mltBag = mltRelationship.getBag();
            docNameField = mltBag.getFieldByName("mlt_name");
            mltBag.setValueByName("mlt_total", mltCount);
            for (int i = 0; i < mltCount; i++) {
                docName = mltDocuments.getName(i);
                docNameField.addValue(docName);
                solrDocumentList = mltDocuments.getVal(i);
                if (solrDocumentList.getNumFound() > 0L) {
                    mltDocument = new Document(Solr.RESPONSE_MLT_DOCUMENT, createDocumentTable());
                    populateDocument(mltDocument, solrDocumentList);
                    mltRelationship.add(mltDocument);
                }
            }
        }
    }

    appLogger.trace(mAppMgr.LOGMSG_TRACE_DEPART);
}

From source file:com.o19s.solr.swan.TermHighlightComponentTest.java

License:Apache License

@Test
public void testFinishStage() throws Exception {

    TermHighlightComponent c = new TermHighlightComponent();
    NamedList<NamedList> n1, n2;
    NamedList<Object> nsub1, nsub2;

    nsub1 = new NamedList<Object>();
    nsub1.add("termFrequency", 1l);
    nsub1.add("id", 100);

    nsub2 = new NamedList<Object>();
    nsub2.add("termFrequency", 4l);
    nsub2.add("id", 100);

    n1 = new NamedList<NamedList>();
    n1.add("john", nsub1);

    n2 = new NamedList<NamedList>();
    n2.add("john", nsub2);

    Assert.assertEquals(1l, nsub1.get("termFrequency"));
    c.merge(n1, n2);//from  ww  w . j  av a2 s  .c om

    Assert.assertEquals(1, n1.size());
    Assert.assertEquals("john", n1.getName(0));

    Assert.assertEquals(2, nsub1.size());
    Assert.assertEquals(100, nsub1.get("id"));
    Assert.assertEquals(5l, nsub1.get("termFrequency"));

}

From source file:com.pjaol.ESB.formatters.JSONFormatter.java

License:Apache License

private String iterateNamedList(NamedList output) {
    JSONObject jo = new JSONObject();
    int sz = output.size();

    for (int i = 0; i < sz; i++) {
        String k = output.getName(i);

        Object v = output.getVal(i);

        if (v instanceof NamedList) {

            jo.put(k, recurseNamedList((NamedList) v));
        } else {/* www  .  ja va 2 s. co  m*/
            jo.put(k, v);
        }
    }

    return jo.toJSONString();
}