Example usage for org.apache.solr.schema IndexSchema printableUniqueKey

List of usage examples for org.apache.solr.schema IndexSchema printableUniqueKey

Introduction

In this page you can find the example usage for org.apache.solr.schema IndexSchema printableUniqueKey.

Prototype

public String printableUniqueKey(org.apache.lucene.document.Document doc) 

Source Link

Document

The printable value of the Unique Key field for the specified Document

Usage

From source file:com.o19s.solr.swan.highlight.SwanHighlighter.java

License:Apache License

/**
 * Generates a list of Highlighted query fragments for each item in a list
 * of documents, or returns null if highlighting is disabled.
 *
 * @param docs query results//from  w w  w. java 2  s . c om
 * @param query the query
 * @param req the current request
 * @param defaultFields default list of fields to summarize
 *
 * @return NamedList containing a NamedList for each document, which in
 * turns contains sets (field, summary) pairs.
 */
@Override
@SuppressWarnings("unchecked")
public NamedList<Object> doHighlighting(DocList docs, Query query, SolrQueryRequest req, String[] defaultFields)
        throws IOException {

    NamedList fragments = new SimpleOrderedMap();

    SolrParams params = req.getParams();
    if (!isHighlightingEnabled(params))
        return null;

    SolrIndexSearcher searcher = req.getSearcher();
    IndexSchema schema = searcher.getSchema();
    String[] fieldNames = getHighlightFields(query, req, defaultFields);
    Set<String> fset = new HashSet<String>();

    {
        // pre-fetch documents using the Searcher's doc cache
        Collections.addAll(fset, fieldNames);
        // fetch unique key if one exists.
        SchemaField keyField = schema.getUniqueKeyField();
        if (null != keyField)
            fset.add(keyField.getName());
    }

    //CHANGE start
    //       int[] docIds = new int[docs.swordize()];
    TreeSet<Integer> docIds = new TreeSet<Integer>();
    DocIterator iterator = docs.iterator();
    for (int i = 0; i < docs.size(); i++) {
        docIds.add(iterator.nextDoc());
    }
    // Get Frag list builder
    String fragListBuilderString = params.get(HighlightParams.FRAG_LIST_BUILDER).toLowerCase();
    FragListBuilder fragListBuilder;
    if (fragListBuilderString.equals("single")) {
        fragListBuilder = new SingleFragListBuilder();
    } else {
        fragListBuilder = new com.o19s.solr.swan.highlight.SimpleFragListBuilder();
    }

    // get FastVectorHighlighter instance out of the processing loop
    SpanAwareFastVectorHighlighter safvh = new SpanAwareFastVectorHighlighter(
            // FVH cannot process hl.usePhraseHighlighter parameter per-field basis
            params.getBool(HighlightParams.USE_PHRASE_HIGHLIGHTER, true),
            // FVH cannot process hl.requireFieldMatch parameter per-field basis
            params.getBool(HighlightParams.FIELD_MATCH, false), fragListBuilder,
            //new com.o19s.solr.swan.highlight.ScoreOrderFragmentsBuilder(),
            new WordHashFragmentsBuilder(),
            // List of docIds to filter spans
            docIds);
    safvh.setPhraseLimit(params.getInt(HighlightParams.PHRASE_LIMIT, Integer.MAX_VALUE));
    SpanAwareFieldQuery fieldQuery = safvh.getFieldQuery(query, searcher.getIndexReader(), docIds);

    // Highlight each document
    for (int docId : docIds) {
        Document doc = searcher.doc(docId, fset);
        NamedList docSummaries = new SimpleOrderedMap();
        for (String fieldName : fieldNames) {
            fieldName = fieldName.trim();
            if (useFastVectorHighlighter(params, schema, fieldName))
                doHighlightingByFastVectorHighlighter(safvh, fieldQuery, req, docSummaries, docId, doc,
                        fieldName);
            else
                doHighlightingByHighlighter(query, req, docSummaries, docId, doc, fieldName);
        }
        String printId = schema.printableUniqueKey(doc);
        fragments.add(printId == null ? null : printId, docSummaries);
    }
    //CHANGE end
    return fragments;
}

From source file:com.searchbox.solr.SenseQueryHandler.java

private static NamedList<Explanation> getExplanations(Query query, DocList docs, SolrIndexSearcher searcher,
        IndexSchema schema) throws IOException {

    NamedList<Explanation> explainList = new SimpleOrderedMap<Explanation>();
    DocIterator iterator = docs.iterator();
    for (int i = 0; i < docs.size(); i++) {
        int id = iterator.nextDoc();

        Document doc = searcher.doc(id);
        String strid = schema.printableUniqueKey(doc);
        explainList.add(strid, searcher.explain(query, id));
    }/*w w  w .j a  v  a2 s . c  o m*/
    return explainList;
}

From source file:jp.co.atware.solr.geta.GETAssocComponent.java

License:Apache License

/**
 * {@inheritDoc}/*w w  w. j  a va 2 s  . c  om*/
 * 
 * @see SearchComponent#process(ResponseBuilder)
 */
@Override
public void process(ResponseBuilder rb) throws IOException {

    NamedList<Object> result = new NamedList<Object>();
    HttpClient client = new HttpClient();

    // ???(geta.settings.process.req[*])
    if (!config.settings.req.isEmpty()) {
        NamedList<Object> paramList = new NamedList<Object>();
        for (Entry<String, QueryType> entry : config.settings.req.entrySet()) {
            String param = entry.getKey();
            NamedList<Object> paramValueList = new NamedList<Object>();
            String[] paramValues = rb.req.getParams().getParams(param);
            if (paramValues != null) {
                for (String paramValue : paramValues) {
                    NamedList<Object> getaResultList = convertResult(postGss3Request(client,
                            convertRequest(rb.req.getParams(), paramValue, entry.getValue())));
                    paramValueList.add(paramValue, getaResultList);
                }
            }
            paramList.add(param, paramValueList);
        }
        result.add("req", paramList);
    }

    // ????(geta.settings.process.doc[*])
    if (!config.settings.doc.isEmpty()) {
        NamedList<Object> docList = new NamedList<Object>();

        SolrIndexSearcher searcher = rb.req.getSearcher();
        IndexSchema schema = searcher.getSchema();
        String key = schema.getUniqueKeyField().getName();
        List<String> targetFieldNames = new ArrayList<String>(config.settings.doc.size() + 1);
        targetFieldNames.add(key);
        targetFieldNames.addAll(config.settings.doc.keySet());
        FieldSelector selector = new MapFieldSelector(targetFieldNames);

        DocIterator iterator = rb.getResults().docList.iterator();
        while (iterator.hasNext()) {
            Document doc = searcher.doc(iterator.next(), selector);
            String docKey = schema.printableUniqueKey(doc);

            NamedList<Object> fieldList = new NamedList<Object>();
            for (Entry<String, QueryType> entry : config.settings.doc.entrySet()) {

                String field = entry.getKey();

                NamedList<Object> queryList = new NamedList<Object>();
                for (Fieldable fieldable : doc.getFieldables(field)) {
                    NamedList<Object> getaResultList = convertResult(postGss3Request(client,
                            convertRequest(rb.req.getParams(), fieldable.stringValue(), entry.getValue())));
                    queryList.add(fieldable.stringValue(), getaResultList);
                }
                fieldList.add(entry.getKey(), queryList);
            }
            docList.add(docKey, fieldList);
        }
        result.add("doc", docList);
    }

    if (result.size() != 0) {
        rb.rsp.add("geta", result);
    }
}

From source file:org.alfresco.solr.LegacySolrInformationServer.java

License:Open Source License

public static Document toDocument(SolrInputDocument doc, IndexSchema schema, AlfrescoSolrDataModel model) {
    Document out = new Document();
    out.setBoost(doc.getDocumentBoost());

    // Load fields from SolrDocument to Document
    for (SolrInputField field : doc) {
        String name = field.getName();
        SchemaField sfield = schema.getFieldOrNull(name);
        boolean used = false;
        float boost = field.getBoost();

        // Make sure it has the correct number
        if (sfield != null && !sfield.multiValued() && field.getValueCount() > 1) {
            String id = "";
            SchemaField sf = schema.getUniqueKeyField();
            if (sf != null) {
                id = "[" + doc.getFieldValue(sf.getName()) + "] ";
            }/*  w ww.  j av a 2s  .com*/
            throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
                    "ERROR: " + id + "multiple values encountered for non multiValued field " + sfield.getName()
                            + ": " + field.getValue());
        }

        // load each field value
        boolean hasField = false;
        for (Object v : field) {
            // TODO: Sort out null
            if (v == null) {
                continue;
            }
            String val = null;
            hasField = true;
            boolean isBinaryField = false;
            if (sfield != null && sfield.getType() instanceof BinaryField) {
                isBinaryField = true;
                BinaryField binaryField = (BinaryField) sfield.getType();
                Field f = binaryField.createField(sfield, v, boost);
                if (f != null)
                    out.add(f);
                used = true;
            } else {
                // TODO!!! HACK -- date conversion
                if (sfield != null && v instanceof Date && sfield.getType() instanceof DateField) {
                    DateField df = (DateField) sfield.getType();
                    val = df.toInternal((Date) v) + 'Z';
                } else if (v != null) {
                    val = v.toString();
                }

                if (sfield != null) {
                    if (v instanceof Reader) {
                        used = true;
                        Field f = new Field(field.getName(), (Reader) v, model.getFieldTermVec(sfield));
                        f.setOmitNorms(model.getOmitNorms(sfield));
                        f.setOmitTermFreqAndPositions(sfield.omitTf());

                        if (f != null) { // null fields are not added
                            out.add(f);
                        }
                    } else {
                        used = true;
                        Field f = sfield.createField(val, boost);
                        if (f != null) { // null fields are not added
                            out.add(f);
                        }
                    }
                }
            }

            // Check if we should copy this field to any other fields.
            // This could happen whether it is explicit or not.
            List<CopyField> copyFields = schema.getCopyFieldsList(name);
            for (CopyField cf : copyFields) {
                SchemaField destinationField = cf.getDestination();
                // check if the copy field is a multivalued or not
                if (!destinationField.multiValued() && out.get(destinationField.getName()) != null) {
                    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
                            "ERROR: multiple values encountered for non multiValued copy field "
                                    + destinationField.getName() + ": " + val);
                }

                used = true;
                Field f = null;
                if (isBinaryField) {
                    if (destinationField.getType() instanceof BinaryField) {
                        BinaryField binaryField = (BinaryField) destinationField.getType();
                        f = binaryField.createField(destinationField, v, boost);
                    }
                } else {
                    f = destinationField.createField(cf.getLimitedValue(val), boost);
                }
                if (f != null) { // null fields are not added
                    out.add(f);
                }
            }

            // In lucene, the boost for a given field is the product of the
            // document boost and *all* boosts on values of that field.
            // For multi-valued fields, we only want to set the boost on the
            // first field.
            boost = 1.0f;
        }

        // make sure the field was used somehow...
        if (!used && hasField) {
            throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "ERROR:unknown field '" + name + "'");
        }
    }

    // Now validate required fields or add default values
    // fields with default values are defacto 'required'
    for (SchemaField field : schema.getRequiredFields()) {
        if (out.getField(field.getName()) == null) {
            if (field.getDefaultValue() != null) {
                out.add(field.createField(field.getDefaultValue(), 1.0f));
            } else {
                String id = schema.printableUniqueKey(out);
                String msg = "Document [" + id + "] missing required field: " + field.getName();
                throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, msg);
            }
        }
    }
    return out;
}