Example usage for org.apache.solr.common SolrInputDocument put

List of usage examples for org.apache.solr.common SolrInputDocument put

Introduction

In this page you can find the example usage for org.apache.solr.common SolrInputDocument put.

Prototype

@Override
    public SolrInputField put(String key, SolrInputField value) 

Source Link

Usage

From source file:com.sindicetech.siren.solr.UpdateProcessorTestBase.java

License:Open Source License

/**
 * Convenience method for building up SolrInputDocuments
 *//*from www  .j a va2s  . co m*/
protected final SolrInputDocument doc(SolrInputField... fields) {
    SolrInputDocument d = new SolrInputDocument();
    for (SolrInputField f : fields) {
        d.put(f.getName(), f);
    }
    return d;
}

From source file:lux.solr.LuxUpdateProcessor.java

License:Mozilla Public License

@Override
public void processAdd(final AddUpdateCommand cmd) throws IOException {
    SolrInputDocument solrInputDocument = cmd.getSolrInputDocument();
    String xmlFieldName = indexConfig.getFieldName(FieldRole.XML_STORE);
    String idFieldName = indexConfig.getFieldName(FieldRole.ID);

    // remove and stash the xml field value
    SolrInputField xmlField = solrInputDocument.removeField(xmlFieldName);
    SolrInputField luxIdField = solrInputDocument.removeField(idFieldName);
    String uri = (String) solrInputDocument.getFieldValue(indexConfig.getFieldName(FieldRole.URI));
    Document luceneDocument = cmd.getLuceneDocument();
    UpdateDocCommand luxCommand = null;/* ww w.  j  a  va2 s . c om*/
    if (uri != null && xmlField != null) {
        // restore the xml field value
        solrInputDocument.put(xmlFieldName, xmlField);
        XmlIndexer xmlIndexer = solrIndexConfig.checkoutXmlIndexer();
        Object xml = xmlField.getFirstValue();
        try {
            try {
                if (xml instanceof String) {
                    xmlIndexer.index(new StringReader((String) xml), uri);
                } else if (xml instanceof byte[]) {
                    TinyBinary xmlbin = new TinyBinary((byte[]) xml, Charset.forName("utf-8"));
                    xmlIndexer.index(xmlbin.getTinyDocument(saxonConfig), uri);
                } else if (xml instanceof NodeInfo) {
                    xmlIndexer.index((NodeInfo) xml, uri);
                }
                // why is this here?  we're getting double values now since we also call 
                // addDocumentFIelds below?
                //luceneDocument = xmlIndexer.createLuceneDocument();
            } catch (XMLStreamException e) {
                logger.error("Failed to parse " + FieldRole.XML_STORE, e);
            }
            addDocumentFields(xmlIndexer, solrIndexConfig.getSchema(), luceneDocument);
            if (luxIdField != null) {
                Object id = luxIdField.getValue();
                if (!(id instanceof Long)) {
                    // solr cloud distributes these as Strings
                    id = Long.valueOf(id.toString());
                }
                luceneDocument.add(new LongField(idFieldName, (Long) id, Store.YES));
            }
            luxCommand = new UpdateDocCommand(req, solrInputDocument, luceneDocument, uri);
        } catch (Exception e) {
            logger.error("An error occurred while indexing " + uri, e);
            throw new IOException(e);
        } finally {
            solrIndexConfig.returnXmlIndexer(xmlIndexer);
        }
        // logger.debug ("Indexed XML document " + uri);
    }
    if (next != null) {
        next.processAdd(luxCommand == null ? cmd : luxCommand);
    }
}

From source file:org.craftercms.search.service.impl.DenormalizingPostProcessor.java

License:Open Source License

protected void copyChildrenFieldsToParent(Collection<SolrInputField> childrenFields,
        SolrInputDocument parentDoc) {
    for (SolrInputField childField : childrenFields) {
        String fieldName = childField.getName();
        if (!parentDoc.containsKey(fieldName)) {
            parentDoc.put(fieldName, childField);
        }//from w w  w .jav a  2 s  .  c o m
    }
}

From source file:org.craftercms.search.service.impl.DenormalizingPostProcessor.java

License:Open Source License

protected void copyParentFieldsToChildren(Collection<SolrInputField> parentFields,
        Collection<SolrInputDocument> childDocs) {
    for (SolrInputDocument childDoc : childDocs) {
        for (SolrInputField parentField : parentFields) {
            String fieldName = parentField.getName();
            if (!childDoc.containsKey(fieldName)) {
                childDoc.put(fieldName, parentField);
            }/*from w  w w  .j  av  a  2s. c  om*/
        }
    }
}

From source file:org.craftercms.search.service.impl.RenameFieldsIfMultiValuePostProcessor.java

License:Open Source License

@Override
public void postProcess(SolrInputDocument solrDoc) {
    String id = solrDoc.getFieldValue(idFieldName).toString();
    List<SolrInputField> renamedFields = new ArrayList<>();

    // Remap single value fields to multi value fields if they have more than one value
    for (Iterator<SolrInputField> iter = solrDoc.iterator(); iter.hasNext();) {
        SolrInputField field = iter.next();
        SolrInputField renamedField = renameFieldIfMultiValue(id, field);

        if (renamedField != null) {
            renamedFields.add(renamedField);

            iter.remove();/*from w  w  w  .  j  a va2 s. com*/
        }
    }

    for (SolrInputField renamedField : renamedFields) {
        solrDoc.put(renamedField.getName(), renamedField);
    }

    // Do the same for child docs
    if (solrDoc.hasChildDocuments()) {
        for (SolrInputDocument childDoc : solrDoc.getChildDocuments()) {
            postProcess(childDoc);
        }
    }
}

From source file:org.obm.service.solr.jms.ContactUpdateCommand.java

License:Open Source License

private void f(SolrInputDocument sid, String field, Collection<Object> values) {
    if (values != null && !values.isEmpty()) {
        SolrInputField sif = new SolrInputField(field);
        for (Object v : values) {
            sif.addValue(v, 1);/* ww w  .  j a  v  a2  s .  c  o  m*/
        }
        sid.put(field, sif);
    }
}

From source file:org.obm.service.solr.jms.EventUpdateCommand.java

License:Open Source License

private void appendField(SolrInputDocument solrInputDocument, String field, Object... values) {
    SolrInputField solrInputField = new SolrInputField(field);
    for (Object value : values) {
        if (value != null) {
            solrInputField.addValue(value, 1);
        }/*from w w  w.  j av a 2 s .c  om*/
    }
    if (solrInputField.getValueCount() >= 1) {
        solrInputDocument.put(field, solrInputField);
    }
}

From source file:org.soas.solr.update.processor.AmbiguousTagCountUPF.java

License:Apache License

@Override
public final UpdateRequestProcessor getInstance(SolrQueryRequest req, SolrQueryResponse rsp,
        UpdateRequestProcessor next) {/*  ww w .  j a va 2 s . co  m*/
    return new UpdateRequestProcessor(next) {
        @Override
        public void processAdd(AddUpdateCommand cmd) throws IOException {

            final SolrInputDocument doc = cmd.getSolrInputDocument();

            if (doc.containsKey(wordsFieldName)) {
                String[] words = ((String) doc.getField(wordsFieldName).getValue()).trim().split("\\s+");

                //Pattern tagPattern = Pattern.compile("\\[[^\\]]+\\]");

                Pattern tagPattern = Pattern.compile("\\]\\[");

                Collection c = doc.getFieldValues(tagFieldName);
                if (c != null) {
                    Iterator it = c.iterator();

                    while (it.hasNext()) {
                        String next = (String) it.next();

                        if (doc.containsKey(guessFieldName + "_" + next)) {
                            String[] guess = ((String) doc.getField(guessFieldName + "_" + next).getValue())
                                    .trim().split("\\s+");

                            int match_count = 0;
                            int correct_count = 0;
                            int tag_count = 0;

                            //assume all tags include brackets
                            for (int i = 0; i < words.length; i++) {
                                String wTag = words[i].substring(words[i].indexOf('|') + 1);

                                int gIndex = guess[i].indexOf('|');
                                if (gIndex != -1) {
                                    String gTag = guess[i].substring(gIndex + 1);
                                    if (gTag.length() > 0) {
                                        List<String> wordTags = Arrays
                                                .asList(tagPattern.split(wTag.substring(1, wTag.length() - 1)));
                                        List<String> guessedTags = Arrays
                                                .asList(tagPattern.split(gTag.substring(1, gTag.length() - 1)));

                                        if (guessedTags.equals(wordTags)) {
                                            correct_count++;
                                            match_count++;
                                        } else if (guessedTags.containsAll(wordTags)) {
                                            match_count++;
                                        }

                                        tag_count += guessedTags.size();
                                    }
                                }
                            }

                            SolrInputField tokenCountField = new SolrInputField(
                                    tokenCountFieldName + "_" + next);
                            tokenCountField.addValue(new Integer(words.length), 1.0f);
                            doc.put(tokenCountFieldName + "_" + next, tokenCountField);

                            SolrInputField matchCountField = new SolrInputField(
                                    matchCountFieldName + "_" + next);
                            matchCountField.addValue(new Integer(match_count), 1.0f);
                            doc.put(matchCountFieldName + "_" + next, matchCountField);

                            SolrInputField correctCountField = new SolrInputField(
                                    correctCountFieldName + "_" + next);
                            correctCountField.addValue(new Integer(correct_count), 1.0f);
                            doc.put(correctCountFieldName + "_" + next, correctCountField);

                            SolrInputField tagCountField = new SolrInputField(tagCountFieldName + "_" + next);
                            tagCountField.addValue(new Integer(tag_count), 1.0f);
                            doc.put(tagCountFieldName + "_" + next, tagCountField);
                        }
                    }
                }
            }

            super.processAdd(cmd);
        }
    };
}

From source file:org.soas.solr.update.processor.SegScoreUPF.java

License:Apache License

@Override
public final UpdateRequestProcessor getInstance(SolrQueryRequest req, SolrQueryResponse rsp,
        UpdateRequestProcessor next) {// w  w w .java2  s  .  c o m
    return new UpdateRequestProcessor(next) {
        @Override
        public void processAdd(AddUpdateCommand cmd) throws IOException {

            final SolrInputDocument doc = cmd.getSolrInputDocument();

            Collection c = doc.getFieldValues(tagFieldName);

            if (c != null) {
                Iterator it = c.iterator();

                while (it.hasNext()) {
                    String next = (String) it.next();

                    // preserve initial values and boost (if any)
                    SolrInputField highlightField = null;
                    String prefix = null;
                    String suffix = null;
                    if (highlightFieldName != null) {
                        highlightField = new SolrInputField(highlightFieldName + "_" + next);
                        if (errorTag == null)
                            errorTag = DEFAULT_ERROR_TAG;
                        prefix = "<" + errorTag + ">";
                        suffix = "</" + errorTag + ">";
                    }

                    if (doc.containsKey(guessFieldName + "_" + next)) {
                        String guess = ((String) doc.getField(guessFieldName + "_" + next).getValue()).trim();
                        Pattern pattern = Pattern.compile("\\S+");
                        Matcher guessMatcher = pattern.matcher(guess);

                        int g_count = 0;

                        if (doc.containsKey(wordsFieldName)) {
                            String words = ((String) doc.getField(wordsFieldName).getValue()).trim();
                            Matcher wordsMatcher = pattern.matcher(words);

                            int correct = 0;
                            int w_count = 0;

                            int w_start = 0;
                            int w_end = 0;
                            int g_start = 0;
                            int g_end = 0;

                            List<String> out = new LinkedList<String>();

                            while (wordsMatcher.find()) {
                                w_start = wordsMatcher.start() - w_count;
                                w_end = wordsMatcher.end() - w_count;
                                w_count += 1;

                                //debug: out = out.concat(" w{" + w_start + "," + w_end + "}:" + words.substring(wordsMatcher.start(), wordsMatcher.end()));

                                while (g_end < w_end && guessMatcher.find()) {
                                    g_start = guessMatcher.start() - g_count;
                                    g_end = guessMatcher.end() - g_count;
                                    g_count += 1;
                                    //debug: out = out.concat(" g{" + g_start + "," + g_end + "}:" + guess.substring(guessMatcher.start(), guessMatcher.end()));
                                    if (g_start == w_start && g_end == w_end) {
                                        //debug: out = out.concat("|Y");
                                        if (null != highlightField) {
                                            out.add(guess.substring(guessMatcher.start(), guessMatcher.end()));
                                        }
                                        correct += 1;
                                    } else if (null != highlightField) {
                                        out.add(prefix
                                                + guess.substring(guessMatcher.start(), guessMatcher.end())
                                                + suffix);
                                    }
                                }
                            }

                            SolrInputField tokenCountField = new SolrInputField(
                                    tokenCountFieldName + "_" + next);
                            tokenCountField.addValue(new Integer(w_count), 1.0f);
                            doc.put(tokenCountFieldName + "_" + next, tokenCountField);

                            SolrInputField guessCountField = new SolrInputField(
                                    guessCountFieldName + "_" + next);
                            guessCountField.addValue(new Integer(g_count), 1.0f);
                            doc.put(guessCountFieldName + "_" + next, guessCountField);

                            SolrInputField correctCountField = new SolrInputField(
                                    correctCountFieldName + "_" + next);
                            correctCountField.addValue(new Integer(correct), 1.0f);
                            doc.put(correctCountFieldName + "_" + next, correctCountField);

                            if (null != highlightField) {
                                highlightField.addValue(StringUtils.join(out.iterator(), DELIMITER), 1.0f);
                                doc.put(highlightFieldName + "_" + next, highlightField);
                            }
                        } else {
                            while (guessMatcher.find()) {
                                g_count++;
                            }
                            SolrInputField guessCountField = new SolrInputField(
                                    guessCountFieldName + "_" + next);
                            guessCountField.addValue(new Integer(g_count), 1.0f);
                            doc.put(guessCountFieldName + "_" + next, guessCountField);
                        }
                    }
                }
            }

            super.processAdd(cmd);
        }
    };
}

From source file:org.soas.solr.update.processor.TagCompareUPF.java

License:Apache License

@Override
public final UpdateRequestProcessor getInstance(SolrQueryRequest req, SolrQueryResponse rsp,
        UpdateRequestProcessor next) {/*w  w  w. ja  va2  s  .  c  o m*/
    return new UpdateRequestProcessor(next) {
        @Override
        public void processAdd(AddUpdateCommand cmd) throws IOException {

            final SolrInputDocument doc = cmd.getSolrInputDocument();
            Collection c = doc.getFieldValues(tagFieldName);

            if (c != null) {
                Iterator it = c.iterator();

                while (it.hasNext()) {
                    String next = (String) it.next();

                    if (doc.containsKey(guessFieldName + "_" + next)) {
                        String guesses[] = ((String) doc.getField(guessFieldName + "_" + next).getValue())
                                .split("\\s+");

                        if (doc.containsKey(tagsFieldName)) {
                            String tags[] = ((String) doc.getField(tagsFieldName).getValue()).split("\\s+");

                            //should be an argument
                            char delimiter = '|';

                            int correct = 0;
                            //need to make sure tags and guesses are same length
                            for (int i = 0; i < tags.length; i++) {
                                if (tags[i].substring(tags[i].indexOf(delimiter) + 1)
                                        .equals(guesses[i].substring(guesses[i].indexOf(delimiter) + 1))) {
                                    correct++;
                                }
                            }

                            SolrInputField tokenCountField = new SolrInputField(
                                    tokenCountFieldName + "_" + next);
                            tokenCountField.addValue(new Integer(tags.length), 1.0f);
                            doc.put(tokenCountFieldName + "_" + next, tokenCountField);

                            SolrInputField correctCountField = new SolrInputField(
                                    correctCountFieldName + "_" + next);
                            correctCountField.addValue(new Integer(correct), 1.0f);
                            doc.put(correctCountFieldName + "_" + next, correctCountField);
                        }

                        SolrInputField guessCountField = new SolrInputField(guessCountFieldName + "_" + next);
                        guessCountField.addValue(new Integer(guesses.length), 1.0f);
                        doc.put(guessCountFieldName + "_" + next, guessCountField);
                    }
                }
            }

            super.processAdd(cmd);
        }
    };
}