Search in sources :

Example 6 with IndexOptions

use of org.apache.lucene.index.IndexOptions in project lucene-solr by apache.

the class PreAnalyzedField method createFieldType.

/**
   * Utility method to create a {@link org.apache.lucene.document.FieldType}
   * based on the {@link SchemaField}
   */
public static org.apache.lucene.document.FieldType createFieldType(SchemaField field) {
    if (!field.indexed() && !field.stored()) {
        if (LOG.isTraceEnabled())
            LOG.trace("Ignoring unindexed/unstored field: " + field);
        return null;
    }
    org.apache.lucene.document.FieldType newType = new org.apache.lucene.document.FieldType();
    newType.setTokenized(field.isTokenized());
    newType.setStored(field.stored());
    newType.setOmitNorms(field.omitNorms());
    IndexOptions options = IndexOptions.DOCS_AND_FREQS_AND_POSITIONS;
    if (field.omitTermFreqAndPositions()) {
        options = IndexOptions.DOCS;
    } else if (field.omitPositions()) {
        options = IndexOptions.DOCS_AND_FREQS;
    } else if (field.storeOffsetsWithPositions()) {
        options = IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS;
    }
    newType.setIndexOptions(options);
    newType.setStoreTermVectors(field.storeTermVector());
    newType.setStoreTermVectorOffsets(field.storeTermOffsets());
    newType.setStoreTermVectorPositions(field.storeTermPositions());
    newType.setStoreTermVectorPayloads(field.storeTermPayloads());
    return newType;
}
Also used : IndexOptions(org.apache.lucene.index.IndexOptions)

Example 7 with IndexOptions

use of org.apache.lucene.index.IndexOptions in project elasticsearch by elastic.

the class TextFieldMapperTests method testIndexOptions.

public void testIndexOptions() throws IOException {
    Map<String, IndexOptions> supportedOptions = new HashMap<>();
    supportedOptions.put("docs", IndexOptions.DOCS);
    supportedOptions.put("freqs", IndexOptions.DOCS_AND_FREQS);
    supportedOptions.put("positions", IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);
    supportedOptions.put("offsets", IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);
    XContentBuilder mappingBuilder = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties");
    for (String option : supportedOptions.keySet()) {
        mappingBuilder.startObject(option).field("type", "text").field("index_options", option).endObject();
    }
    String mapping = mappingBuilder.endObject().endObject().endObject().string();
    DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping));
    XContentBuilder jsonDoc = XContentFactory.jsonBuilder().startObject();
    for (String option : supportedOptions.keySet()) {
        jsonDoc.field(option, "1234");
    }
    ParsedDocument doc = mapper.parse("test", "type", "1", jsonDoc.endObject().bytes());
    for (Map.Entry<String, IndexOptions> entry : supportedOptions.entrySet()) {
        String field = entry.getKey();
        IndexOptions options = entry.getValue();
        IndexableField[] fields = doc.rootDoc().getFields(field);
        assertEquals(1, fields.length);
        assertEquals(options, fields[0].fieldType().indexOptions());
    }
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) HashMap(java.util.HashMap) IndexOptions(org.apache.lucene.index.IndexOptions) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) Matchers.containsString(org.hamcrest.Matchers.containsString) HashMap(java.util.HashMap) Map(java.util.Map) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 8 with IndexOptions

use of org.apache.lucene.index.IndexOptions in project lucene-solr by apache.

the class Lucene60FieldInfosFormat method read.

@Override
public FieldInfos read(Directory directory, SegmentInfo segmentInfo, String segmentSuffix, IOContext context) throws IOException {
    final String fileName = IndexFileNames.segmentFileName(segmentInfo.name, segmentSuffix, EXTENSION);
    try (ChecksumIndexInput input = directory.openChecksumInput(fileName, context)) {
        Throwable priorE = null;
        FieldInfo[] infos = null;
        try {
            CodecUtil.checkIndexHeader(input, Lucene60FieldInfosFormat.CODEC_NAME, Lucene60FieldInfosFormat.FORMAT_START, Lucene60FieldInfosFormat.FORMAT_CURRENT, segmentInfo.getId(), segmentSuffix);
            //read in the size
            final int size = input.readVInt();
            infos = new FieldInfo[size];
            // previous field's attribute map, we share when possible:
            Map<String, String> lastAttributes = Collections.emptyMap();
            for (int i = 0; i < size; i++) {
                String name = input.readString();
                final int fieldNumber = input.readVInt();
                if (fieldNumber < 0) {
                    throw new CorruptIndexException("invalid field number for field: " + name + ", fieldNumber=" + fieldNumber, input);
                }
                byte bits = input.readByte();
                boolean storeTermVector = (bits & STORE_TERMVECTOR) != 0;
                boolean omitNorms = (bits & OMIT_NORMS) != 0;
                boolean storePayloads = (bits & STORE_PAYLOADS) != 0;
                final IndexOptions indexOptions = getIndexOptions(input, input.readByte());
                // DV Types are packed in one byte
                final DocValuesType docValuesType = getDocValuesType(input, input.readByte());
                final long dvGen = input.readLong();
                Map<String, String> attributes = input.readMapOfStrings();
                // just use the last field's map if its the same
                if (attributes.equals(lastAttributes)) {
                    attributes = lastAttributes;
                }
                lastAttributes = attributes;
                int pointDimensionCount = input.readVInt();
                int pointNumBytes;
                if (pointDimensionCount != 0) {
                    pointNumBytes = input.readVInt();
                } else {
                    pointNumBytes = 0;
                }
                try {
                    infos[i] = new FieldInfo(name, fieldNumber, storeTermVector, omitNorms, storePayloads, indexOptions, docValuesType, dvGen, attributes, pointDimensionCount, pointNumBytes);
                    infos[i].checkConsistency();
                } catch (IllegalStateException e) {
                    throw new CorruptIndexException("invalid fieldinfo for field: " + name + ", fieldNumber=" + fieldNumber, input, e);
                }
            }
        } catch (Throwable exception) {
            priorE = exception;
        } finally {
            CodecUtil.checkFooter(input, priorE);
        }
        return new FieldInfos(infos);
    }
}
Also used : ChecksumIndexInput(org.apache.lucene.store.ChecksumIndexInput) IndexOptions(org.apache.lucene.index.IndexOptions) CorruptIndexException(org.apache.lucene.index.CorruptIndexException) FieldInfos(org.apache.lucene.index.FieldInfos) DocValuesType(org.apache.lucene.index.DocValuesType) FieldInfo(org.apache.lucene.index.FieldInfo)

Aggregations

IndexOptions (org.apache.lucene.index.IndexOptions)8 FieldInfo (org.apache.lucene.index.FieldInfo)4 HashMap (java.util.HashMap)3 DocValuesType (org.apache.lucene.index.DocValuesType)3 FieldInfos (org.apache.lucene.index.FieldInfos)3 ChecksumIndexInput (org.apache.lucene.store.ChecksumIndexInput)3 Map (java.util.Map)2 CorruptIndexException (org.apache.lucene.index.CorruptIndexException)2 IndexableField (org.apache.lucene.index.IndexableField)2 BytesRefBuilder (org.apache.lucene.util.BytesRefBuilder)2 Document (org.apache.lucene.document.Document)1 Field (org.apache.lucene.document.Field)1 FieldType (org.apache.lucene.document.FieldType)1 TextField (org.apache.lucene.document.TextField)1 IndexOutput (org.apache.lucene.store.IndexOutput)1 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)1 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1