Search in sources :

Example 26 with IndexWriterConfig

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

the class IndexFieldDataServiceTests method testFieldDataCacheListener.

public void testFieldDataCacheListener() throws Exception {
    final IndexService indexService = createIndex("test");
    final IndicesService indicesService = getInstanceFromNode(IndicesService.class);
    // copy the ifdService since we can set the listener only once.
    final IndexFieldDataService ifdService = new IndexFieldDataService(indexService.getIndexSettings(), indicesService.getIndicesFieldDataCache(), indicesService.getCircuitBreakerService(), indexService.mapperService());
    final BuilderContext ctx = new BuilderContext(indexService.getIndexSettings().getSettings(), new ContentPath(1));
    final MappedFieldType mapper1 = new TextFieldMapper.Builder("s").fielddata(true).build(ctx).fieldType();
    final IndexWriter writer = new IndexWriter(new RAMDirectory(), new IndexWriterConfig(new KeywordAnalyzer()));
    Document doc = new Document();
    doc.add(new StringField("s", "thisisastring", Store.NO));
    writer.addDocument(doc);
    DirectoryReader open = DirectoryReader.open(writer);
    final boolean wrap = randomBoolean();
    final IndexReader reader = wrap ? ElasticsearchDirectoryReader.wrap(open, new ShardId("test", "_na_", 1)) : open;
    final AtomicInteger onCacheCalled = new AtomicInteger();
    final AtomicInteger onRemovalCalled = new AtomicInteger();
    ifdService.setListener(new IndexFieldDataCache.Listener() {

        @Override
        public void onCache(ShardId shardId, String fieldName, Accountable ramUsage) {
            if (wrap) {
                assertEquals(new ShardId("test", "_na_", 1), shardId);
            } else {
                assertNull(shardId);
            }
            onCacheCalled.incrementAndGet();
        }

        @Override
        public void onRemoval(ShardId shardId, String fieldName, boolean wasEvicted, long sizeInBytes) {
            if (wrap) {
                assertEquals(new ShardId("test", "_na_", 1), shardId);
            } else {
                assertNull(shardId);
            }
            onRemovalCalled.incrementAndGet();
        }
    });
    IndexFieldData<?> ifd = ifdService.getForField(mapper1);
    LeafReaderContext leafReaderContext = reader.getContext().leaves().get(0);
    AtomicFieldData load = ifd.load(leafReaderContext);
    assertEquals(1, onCacheCalled.get());
    assertEquals(0, onRemovalCalled.get());
    reader.close();
    load.close();
    writer.close();
    assertEquals(1, onCacheCalled.get());
    assertEquals(1, onRemovalCalled.get());
    ifdService.clear();
}
Also used : IndexService(org.elasticsearch.index.IndexService) Matchers.containsString(org.hamcrest.Matchers.containsString) Document(org.apache.lucene.document.Document) ShardId(org.elasticsearch.index.shard.ShardId) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) KeywordAnalyzer(org.apache.lucene.analysis.core.KeywordAnalyzer) ElasticsearchDirectoryReader(org.elasticsearch.common.lucene.index.ElasticsearchDirectoryReader) DirectoryReader(org.apache.lucene.index.DirectoryReader) Accountable(org.apache.lucene.util.Accountable) IndicesService(org.elasticsearch.indices.IndicesService) ContentPath(org.elasticsearch.index.mapper.ContentPath) RAMDirectory(org.apache.lucene.store.RAMDirectory) IndexWriter(org.apache.lucene.index.IndexWriter) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StringField(org.apache.lucene.document.StringField) IndexReader(org.apache.lucene.index.IndexReader) BuilderContext(org.elasticsearch.index.mapper.Mapper.BuilderContext) TextFieldMapper(org.elasticsearch.index.mapper.TextFieldMapper) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Example 27 with IndexWriterConfig

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

the class CodecTests method assertCompressionEquals.

// write some docs with it, inspect .si to see this was the used compression
private void assertCompressionEquals(Mode expected, Codec actual) throws Exception {
    Directory dir = newDirectory();
    IndexWriterConfig iwc = newIndexWriterConfig(null);
    iwc.setCodec(actual);
    IndexWriter iw = new IndexWriter(dir, iwc);
    iw.addDocument(new Document());
    iw.commit();
    iw.close();
    DirectoryReader ir = DirectoryReader.open(dir);
    SegmentReader sr = (SegmentReader) ir.leaves().get(0).reader();
    String v = sr.getSegmentInfo().info.getAttribute(Lucene50StoredFieldsFormat.MODE_KEY);
    assertNotNull(v);
    assertEquals(expected, Mode.valueOf(v));
    ir.close();
    dir.close();
}
Also used : IndexWriter(org.apache.lucene.index.IndexWriter) DirectoryReader(org.apache.lucene.index.DirectoryReader) Document(org.apache.lucene.document.Document) SegmentReader(org.apache.lucene.index.SegmentReader) Directory(org.apache.lucene.store.Directory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Example 28 with IndexWriterConfig

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

the class BooleanFieldMapperTests method testDefaults.

public void testDefaults() throws IOException {
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("field").field("type", "boolean").endObject().endObject().endObject().endObject().string();
    DocumentMapper defaultMapper = parser.parse("type", new CompressedXContent(mapping));
    ParsedDocument doc = defaultMapper.parse("test", "type", "1", XContentFactory.jsonBuilder().startObject().field("field", true).endObject().bytes());
    try (Directory dir = new RAMDirectory();
        IndexWriter w = new IndexWriter(dir, new IndexWriterConfig(new MockAnalyzer(random())))) {
        w.addDocuments(doc.docs());
        try (DirectoryReader reader = DirectoryReader.open(w)) {
            final LeafReader leaf = reader.leaves().get(0).reader();
            // boolean fields are indexed and have doc values by default
            assertEquals(new BytesRef("T"), leaf.terms("field").iterator().next());
            SortedNumericDocValues values = leaf.getSortedNumericDocValues("field");
            assertNotNull(values);
            values.setDocument(0);
            assertEquals(1, values.count());
            assertEquals(1, values.valueAt(0));
        }
    }
}
Also used : SortedNumericDocValues(org.apache.lucene.index.SortedNumericDocValues) LeafReader(org.apache.lucene.index.LeafReader) DirectoryReader(org.apache.lucene.index.DirectoryReader) Matchers.containsString(org.hamcrest.Matchers.containsString) RAMDirectory(org.apache.lucene.store.RAMDirectory) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) IndexWriter(org.apache.lucene.index.IndexWriter) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) BytesRef(org.apache.lucene.util.BytesRef) RAMDirectory(org.apache.lucene.store.RAMDirectory) Directory(org.apache.lucene.store.Directory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Example 29 with IndexWriterConfig

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

the class HalfFloatFielddataTests method testSingleValued.

public void testSingleValued() throws IOException {
    Directory dir = newDirectory();
    // we need the default codec to check for singletons
    IndexWriter w = new IndexWriter(dir, new IndexWriterConfig(null).setCodec(TestUtil.getDefaultCodec()));
    Document doc = new Document();
    for (IndexableField f : NumberFieldMapper.NumberType.HALF_FLOAT.createFields("half_float", 3f, false, true, false)) {
        doc.add(f);
    }
    w.addDocument(doc);
    final DirectoryReader dirReader = DirectoryReader.open(w);
    LeafReader reader = getOnlyLeafReader(dirReader);
    SortedNumericDoubleValues values = new SortedNumericDVIndexFieldData.SortedNumericHalfFloatFieldData(reader, "half_float").getDoubleValues();
    assertNotNull(FieldData.unwrapSingleton(values));
    values.setDocument(0);
    assertEquals(1, values.count());
    assertEquals(3f, values.valueAt(0), 0f);
    IOUtils.close(dirReader, w, dir);
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) LeafReader(org.apache.lucene.index.LeafReader) IndexWriter(org.apache.lucene.index.IndexWriter) DirectoryReader(org.apache.lucene.index.DirectoryReader) Document(org.apache.lucene.document.Document) SortedNumericDoubleValues(org.elasticsearch.index.fielddata.SortedNumericDoubleValues) Directory(org.apache.lucene.store.Directory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Example 30 with IndexWriterConfig

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

the class ScaledFloatFieldTypeTests method testStats.

public void testStats() throws IOException {
    ScaledFloatFieldMapper.ScaledFloatFieldType ft = new ScaledFloatFieldMapper.ScaledFloatFieldType();
    ft.setName("scaled_float");
    ft.setScalingFactor(0.1 + randomDouble() * 100);
    Directory dir = newDirectory();
    IndexWriter w = new IndexWriter(dir, new IndexWriterConfig(null));
    try (DirectoryReader reader = DirectoryReader.open(w)) {
        assertNull(ft.stats(reader));
    }
    Document doc = new Document();
    doc.add(new StoredField("scaled_float", -1));
    w.addDocument(doc);
    try (DirectoryReader reader = DirectoryReader.open(w)) {
        // field exists, but has no point values
        FieldStats<?> stats = ft.stats(reader);
        assertFalse(stats.hasMinMax());
        assertNull(stats.getMinValue());
        assertNull(stats.getMaxValue());
    }
    LongPoint point = new LongPoint("scaled_float", -1);
    doc.add(point);
    w.addDocument(doc);
    point.setLongValue(10);
    w.addDocument(doc);
    try (DirectoryReader reader = DirectoryReader.open(w)) {
        FieldStats<?> stats = ft.stats(reader);
        assertEquals(-1 / ft.getScalingFactor(), stats.getMinValue());
        assertEquals(10 / ft.getScalingFactor(), stats.getMaxValue());
        assertEquals(3, stats.getMaxDoc());
    }
    w.deleteAll();
    try (DirectoryReader reader = DirectoryReader.open(w)) {
        assertNull(ft.stats(reader));
    }
    IOUtils.close(w, dir);
}
Also used : StoredField(org.apache.lucene.document.StoredField) IndexWriter(org.apache.lucene.index.IndexWriter) DirectoryReader(org.apache.lucene.index.DirectoryReader) LongPoint(org.apache.lucene.document.LongPoint) Document(org.apache.lucene.document.Document) Directory(org.apache.lucene.store.Directory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Aggregations

IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)427 IndexWriter (org.apache.lucene.index.IndexWriter)291 Document (org.apache.lucene.document.Document)277 Directory (org.apache.lucene.store.Directory)267 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)162 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)139 IndexReader (org.apache.lucene.index.IndexReader)133 Term (org.apache.lucene.index.Term)102 IndexSearcher (org.apache.lucene.search.IndexSearcher)86 DirectoryReader (org.apache.lucene.index.DirectoryReader)85 RAMDirectory (org.apache.lucene.store.RAMDirectory)79 TextField (org.apache.lucene.document.TextField)77 Field (org.apache.lucene.document.Field)71 BytesRef (org.apache.lucene.util.BytesRef)68 IOException (java.io.IOException)58 Analyzer (org.apache.lucene.analysis.Analyzer)57 Test (org.junit.Test)52 StringField (org.apache.lucene.document.StringField)47 TermQuery (org.apache.lucene.search.TermQuery)41 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)38