nodeindexer.java

来自「jsr170接口的java实现。是个apache的开源项目。」· Java 代码 · 共 578 行 · 第 1/2 页

JAVA
578
字号
                String type = typeValue.internalValue().toString();                // jcr:encoding is not mandatory                String encoding = null;                InternalValue encodingValue = getValue(QName.JCR_ENCODING);                if (encodingValue != null) {                    encoding = encodingValue.internalValue().toString();                }                InputStream stream =                        ((BLOBFileValue) internalValue).getStream();                Reader reader = extractor.extractText(stream, type, encoding);                doc.add(createFulltextField(reader));            }        } catch (Exception e) {            // TODO: How to recover from a transient indexing failure?            log.warn("Exception while indexing binary property: " + e.toString());            log.debug("Dump: ", e);        }    }    /**     * Utility method that extracts the first value of the named property     * of the current node. Returns <code>null</code> if the property does     * not exist or contains no values.     *     * @param name property name     * @return value of the named property, or <code>null</code>     * @throws ItemStateException if the property can not be accessed     */    protected InternalValue getValue(QName name) throws ItemStateException {        try {            PropertyId id = new PropertyId(node.getNodeId(), name);            PropertyState property =                (PropertyState) stateProvider.getItemState(id);            InternalValue[] values = property.getValues();            if (values.length > 0) {                return values[0];            } else {                return null;            }        } catch (NoSuchItemStateException e) {            return null;        }    }    /**     * Adds the string representation of the boolean value to the document as     * the named field.     *     * @param doc           The document to which to add the field     * @param fieldName     The name of the field to add     * @param internalValue The value for the field to add to the document.     */    protected void addBooleanValue(Document doc, String fieldName, Object internalValue) {        doc.add(new Field(FieldNames.PROPERTIES,                FieldNames.createNamedValue(fieldName, internalValue.toString()),                Field.Store.NO,                Field.Index.UN_TOKENIZED,                Field.TermVector.NO));    }    /**     * Adds the calendar value to the document as the named field. The calendar     * value is converted to an indexable string value using the {@link DateField}     * class.     *     * @param doc           The document to which to add the field     * @param fieldName     The name of the field to add     * @param internalValue The value for the field to add to the document.     */    protected void addCalendarValue(Document doc, String fieldName, Object internalValue) {        long millis = ((Calendar) internalValue).getTimeInMillis();        doc.add(new Field(FieldNames.PROPERTIES,                FieldNames.createNamedValue(fieldName, DateField.timeToString(millis)),                Field.Store.NO,                Field.Index.UN_TOKENIZED,                Field.TermVector.NO));    }    /**     * Adds the double value to the document as the named field. The double     * value is converted to an indexable string value using the     * {@link DoubleField} class.     *     * @param doc           The document to which to add the field     * @param fieldName     The name of the field to add     * @param internalValue The value for the field to add to the document.     */    protected void addDoubleValue(Document doc, String fieldName, Object internalValue) {        double doubleVal = ((Double) internalValue).doubleValue();        doc.add(new Field(FieldNames.PROPERTIES,                FieldNames.createNamedValue(fieldName, DoubleField.doubleToString(doubleVal)),                Field.Store.NO,                Field.Index.UN_TOKENIZED,                Field.TermVector.NO));    }    /**     * Adds the long value to the document as the named field. The long     * value is converted to an indexable string value using the {@link LongField}     * class.     *     * @param doc           The document to which to add the field     * @param fieldName     The name of the field to add     * @param internalValue The value for the field to add to the document.     */    protected void addLongValue(Document doc, String fieldName, Object internalValue) {        long longVal = ((Long) internalValue).longValue();        doc.add(new Field(FieldNames.PROPERTIES,                FieldNames.createNamedValue(fieldName, LongField.longToString(longVal)),                Field.Store.NO,                Field.Index.UN_TOKENIZED,                Field.TermVector.NO));    }    /**     * Adds the reference value to the document as the named field. The value's     * string representation is added as the reference data. Additionally the     * reference data is stored in the index.     *     * @param doc           The document to which to add the field     * @param fieldName     The name of the field to add     * @param internalValue The value for the field to add to the document.     */    protected void addReferenceValue(Document doc, String fieldName, Object internalValue) {        String uuid = internalValue.toString();        doc.add(new Field(FieldNames.PROPERTIES,                FieldNames.createNamedValue(fieldName, uuid),                Field.Store.YES, // store                Field.Index.UN_TOKENIZED,                Field.TermVector.NO));    }    /**     * Adds the path value to the document as the named field. The path     * value is converted to an indexable string value using the name space     * mappings with which this class has been created.     *     * @param doc           The document to which to add the field     * @param fieldName     The name of the field to add     * @param internalValue The value for the field to add to the document.     */    protected void addPathValue(Document doc, String fieldName, Object internalValue) {        Path path = (Path) internalValue;        String pathString = path.toString();        try {            pathString = PathFormat.format(path, mappings);        } catch (NoPrefixDeclaredException e) {            // will never happen        }        doc.add(new Field(FieldNames.PROPERTIES,                FieldNames.createNamedValue(fieldName, pathString),                Field.Store.NO,                Field.Index.UN_TOKENIZED,                Field.TermVector.NO));    }    /**     * Adds the string value to the document both as the named field and for     * full text indexing.     *     * @param doc           The document to which to add the field     * @param fieldName     The name of the field to add     * @param internalValue The value for the field to add to the document.     * @deprecated Use {@link #addStringValue(Document, String, Object, boolean)     *             addStringValue(Document, String, Object, boolean)} instead.     */    protected void addStringValue(Document doc, String fieldName, Object internalValue) {        addStringValue(doc, fieldName, internalValue, true);    }    /**     * Adds the string value to the document both as the named field and     * optionally for full text indexing if <code>tokenized</code> is     * <code>true</code>.     *     * @param doc           The document to which to add the field     * @param fieldName     The name of the field to add     * @param internalValue The value for the field to add to the document.     * @param tokenized     If <code>true</code> the string is also tokenized     *                      and fulltext indexed.     */    protected void addStringValue(Document doc, String fieldName, Object internalValue, boolean tokenized) {        String stringValue = String.valueOf(internalValue);        // simple String        doc.add(new Field(FieldNames.PROPERTIES,                FieldNames.createNamedValue(fieldName, stringValue),                Field.Store.NO,                Field.Index.UN_TOKENIZED,                Field.TermVector.NO));        if (tokenized) {            // also create fulltext index of this value            doc.add(createFulltextField(stringValue));            // create fulltext index on property            int idx = fieldName.indexOf(':');            fieldName = fieldName.substring(0, idx + 1)                    + FieldNames.FULLTEXT_PREFIX + fieldName.substring(idx + 1);            doc.add(new Field(fieldName, stringValue,                    Field.Store.NO,                    Field.Index.TOKENIZED,                    Field.TermVector.NO));        }    }    /**     * Adds the name value to the document as the named field. The name     * value is converted to an indexable string treating the internal value     * as a qualified name and mapping the name space using the name space     * mappings with which this class has been created.     *     * @param doc           The document to which to add the field     * @param fieldName     The name of the field to add     * @param internalValue The value for the field to add to the document.     */    protected void addNameValue(Document doc, String fieldName, Object internalValue) {        QName qualiName = (QName) internalValue;        String normValue = internalValue.toString();        try {            normValue = mappings.getPrefix(qualiName.getNamespaceURI())                    + ":" + qualiName.getLocalName();        } catch (NamespaceException e) {            // will never happen        }        doc.add(new Field(FieldNames.PROPERTIES,                FieldNames.createNamedValue(fieldName, normValue),                Field.Store.NO,                Field.Index.UN_TOKENIZED,                Field.TermVector.NO));    }    /**     * Creates a fulltext field for the string <code>value</code>.     *     * @param value the string value.     * @return a lucene field.     */    protected Field createFulltextField(String value) {        if (supportHighlighting) {            // store field compressed if greater than 16k            Field.Store stored;            if (value.length() > 0x4000) {                stored = Field.Store.COMPRESS;            } else {                stored = Field.Store.YES;            }            return new Field(FieldNames.FULLTEXT, value, stored,                    Field.Index.TOKENIZED, Field.TermVector.WITH_OFFSETS);        } else {            return new Field(FieldNames.FULLTEXT, value,                    Field.Store.NO, Field.Index.TOKENIZED);        }    }    /**     * Creates a fulltext field for the reader <code>value</code>.     *     * @param value the reader value.     * @return a lucene field.     */    protected Field createFulltextField(Reader value) {        if (supportHighlighting) {            // need to create a string value            StringBuffer textExtract = new StringBuffer();            char[] buffer = new char[1024];            int len;            try {                while ((len = value.read(buffer)) > -1) {                    textExtract.append(buffer, 0, len);                }            } catch (IOException e) {                log.warn("Exception reading value for fulltext field: " +                        e.getMessage());                log.debug("Dump:", e);            } finally {                try {                    value.close();                } catch (IOException e) {                    // ignore                }            }            return createFulltextField(textExtract.toString());        } else {            return new Field(FieldNames.FULLTEXT, value);        }    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?