Example usage for org.apache.lucene.codecs PostingsFormat forName

List of usage examples for org.apache.lucene.codecs PostingsFormat forName

Introduction

In this page you can find the example usage for org.apache.lucene.codecs PostingsFormat forName.

Prototype

public static PostingsFormat forName(String name) 

Source Link

Document

looks up a format by name

Usage

From source file:com.rocana.lucene.codec.v1.RocanaSearchCodecV1.java

License:Apache License

public RocanaSearchCodecV1() {
    super(SHORT_NAME, new Lucene54Codec());
    logger.debug("Instantiated custom codec: {} which wraps codec: {}", getClass(),
            getWrappedCodec().getClass());

    // use SPI so we use the exact instance Lucene instantiated (it only instantiates it once, then caches it)
    actualPostingsFormat = (RocanaLucene50PostingsFormat) PostingsFormat
            .forName(RocanaLucene50PostingsFormat.SHORT_NAME);

    perFieldPostingsFormat = new RocanaPerFieldPostingsFormat() {
        @Override/*from w w  w  .  j a v a 2s  .com*/
        public PostingsFormat getPostingsFormatForField(String field) {
            return actualPostingsFormat;
        }
    };
}

From source file:com.rocana.lucene.codec.v1.TestRocanaLucene50PostingsFormat.java

License:Apache License

/**
 * This test verifies we created the SPI lookup file and registered our postings
 * format in it.//from  w w  w  .  j a va 2  s.c  o  m
 *
 * If we didn't do this Lucene would put its postings format name ("Lucene50")
 * in the Lucene index files, then, when reading those index files, Lucene
 * may go searching for the Lucene50 postings format. Instead we want Lucene
 * using our postings format, and the best way to do that is to use our own
 * short name and register our postings format in the SPI file:
 * META-INF/services/org.apache.lucene.codecs.PostingsFormat
 */
@Test
public void classShouldBeRegisteredForSPILookups() {
    PostingsFormat postingsFormat = PostingsFormat.forName(RocanaLucene50PostingsFormat.SHORT_NAME);

    Assert.assertEquals("Expected a non-null instance of our postings format",
            RocanaLucene50PostingsFormat.class, postingsFormat.getClass());
}

From source file:com.rocana.lucene.codec.v1.TestRocanaPerFieldPostingsFormat3.java

License:Apache License

/**
 * This is a sunny day test, ensuring the method returns the postings format it
 * was asked to return. This does not cover any special cases, like "Lucene50".
 *///  w w  w.  j av  a2 s. c om
@Test
public void lookupPostingsFormatShouldReturnTheRocanaPostingsFormat() {
    String shortName = RocanaLucene50PostingsFormat.SHORT_NAME;
    PostingsFormat expected = PostingsFormat.forName(shortName);

    PostingsFormat actual = RocanaPerFieldPostingsFormat.FieldsReader.lookupPostingsFormat(shortName);

    Assert.assertSame("Expected the same instance the SPI lookup returns", expected, actual);
    Assert.assertEquals("Expected an instance of the class we asked for", RocanaLucene50PostingsFormat.class,
            actual.getClass());
}

From source file:com.rocana.lucene.codec.v1.TestRocanaPerFieldPostingsFormat3.java

License:Apache License

/**
 * This verifies we have logic to return Rocana's custom postings format
 * whenever asked for Lucene's postings format.
 *
 * This scenario comes up as Lucene reads old index files (from data written
 * with the original Lucene54 codec, before Rocana's custom codec). When
 * Lucene reads those index files and comes across "Lucene50" specified as the
 * postings format, the code we're testing asks the SPI (Service Provider Interface)
 * for that "Lucene50" postings format, but we want it to use our custom postings
 * format for the performance benefits. So instead we return Rocana's fork of
 * the postings format. This only works because the on-disk representation of
 * both Lucene's and Rocana's postings formats are identical.
 */// w  w  w.  ja  va 2  s .  c  om
@Test
public void lookupPostingsFormatShouldReturnTheRocanaPostingsFormtForLucene50() {
    PostingsFormat rocanaPostingsFormat = PostingsFormat.forName(RocanaLucene50PostingsFormat.SHORT_NAME);

    PostingsFormat actual = RocanaPerFieldPostingsFormat.FieldsReader.lookupPostingsFormat("Lucene50");

    Assert.assertSame("Expected the same instance the SPI lookup returns", rocanaPostingsFormat, actual);
    Assert.assertEquals("Expected an instance of the class we asked for", RocanaLucene50PostingsFormat.class,
            actual.getClass());
}

From source file:com.rocana.lucene.codec.v1.TestRocanaPerFieldPostingsFormat3.java

License:Apache License

/**
 * This is a sunny day test to ensure the method returns the postings format
 * asked for. The other tests verify how the method behaves when returning
 * {@link RocanaLucene50PostingsFormat}, which means if the method
 * always 100% of the time returned {@link RocanaLucene50PostingsFormat} those
 * tests would pass. This test ensures the method actually does return the
 * real postings format asked for, and therefore proves the method only does
 * something special when asked for "Lucene50".
 *//*from w w  w.  j a v  a2  s. c  o m*/
@Test
public void lookupPostingsFormatShouldReturnThePostingsFormatAskedFor() {
    String shortName = "SimpleText";
    PostingsFormat expected = PostingsFormat.forName(shortName);

    PostingsFormat actual = RocanaPerFieldPostingsFormat.FieldsReader.lookupPostingsFormat(shortName);

    Assert.assertSame("Expected the same instance the SPI lookup returns", expected, actual);
    Assert.assertEquals("Expected an instance of the class we asked for", SimpleTextPostingsFormat.class,
            actual.getClass());
}

From source file:com.rocana.lucene.codec.v1.TestRocanaSearchCodecV1.java

License:Apache License

/**
 * Verify we get the exact same instance Lucene instantiated for SPI
 * (Service Provider Interface) lookups. Lucene only instantiates the
 * postings format once, and there's no reason we shouldn't use that
 * exact same instance./* w w w. j a  v  a2  s. c o m*/
 */
@Test
public void getActualPostingsFormatShouldReturnPostingsFormatFromSPI() {
    RocanaSearchCodecV1 codec = new RocanaSearchCodecV1();
    PostingsFormat expected = PostingsFormat.forName(RocanaLucene50PostingsFormat.SHORT_NAME);

    PostingsFormat actual = codec.getActualPostingsFormat();

    Assert.assertSame("Expected the exact same instance the SPI lookup returned", expected, actual);
}

From source file:org.apache.solr.core.DefaultCodecFactory.java

License:Apache License

@Override
public Codec create(final IndexSchema schema) {
    return new Lucene40Codec() {
        @Override//from  w w w. j  a  va 2 s.com
        public PostingsFormat getPostingsFormatForField(String field) {
            final SchemaField fieldOrNull = schema.getFieldOrNull(field);
            if (fieldOrNull == null) {
                throw new IllegalArgumentException("no such field " + field);
            }
            String postingsFormatName = fieldOrNull.getType().getPostingsFormat();
            if (postingsFormatName != null) {
                return PostingsFormat.forName(postingsFormatName);
            }
            return super.getPostingsFormatForField(field);
        }
    };
}

From source file:org.apache.solr.core.SchemaCodecFactory.java

License:Apache License

@Override
public void init(NamedList args) {
    super.init(args);
    codec = new Lucene46Codec() {
        @Override/*from  w  ww  .  j  av  a  2  s .com*/
        public PostingsFormat getPostingsFormatForField(String field) {
            final SchemaField schemaField = core.getLatestSchema().getFieldOrNull(field);
            if (schemaField != null) {
                String postingsFormatName = schemaField.getType().getPostingsFormat();
                if (postingsFormatName != null) {
                    return PostingsFormat.forName(postingsFormatName);
                }
            }
            return super.getPostingsFormatForField(field);
        }

        @Override
        public DocValuesFormat getDocValuesFormatForField(String field) {
            final SchemaField schemaField = core.getLatestSchema().getFieldOrNull(field);
            if (schemaField != null) {
                String docValuesFormatName = schemaField.getType().getDocValuesFormat();
                if (docValuesFormatName != null) {
                    return DocValuesFormat.forName(docValuesFormatName);
                }
            }
            return super.getDocValuesFormatForField(field);
        }
    };
}

From source file:org.elasticsearch.index.shard.IndexShard.java

License:Apache License

public CompletionStats completionStats(String... fields) {
    CompletionStats completionStats = new CompletionStats();
    final Engine.Searcher currentSearcher = acquireSearcher("completion_stats");
    try {//from ww  w . ja  va 2  s.  c o  m
        Completion090PostingsFormat postingsFormat = ((Completion090PostingsFormat) PostingsFormat
                .forName(Completion090PostingsFormat.CODEC_NAME));
        completionStats.add(postingsFormat.completionStats(currentSearcher.reader(), fields));
    } finally {
        currentSearcher.close();
    }
    return completionStats;
}

From source file:org.kie.kieora.backend.lucene.setups.DirectoryLuceneSetup.java

License:Apache License

public DirectoryLuceneSetup(final Directory directory, final boolean freshIndex) {
    try {/*from   w  w  w. jav a 2s . co  m*/
        this.freshIndex = freshIndex;
        this.directory = checkNotNull("directory", directory);
        this.analyzer = new StandardAnalyzer(LUCENE_40);
        final IndexWriterConfig config = new IndexWriterConfig(LUCENE_40, getAnalyzer());

        final Codec codec = new Lucene40Codec() {
            @Override
            public PostingsFormat getPostingsFormatForField(String field) {
                if (field.equals("id")) {
                    return PostingsFormat.forName("Memory");
                } else {
                    return PostingsFormat.forName("Lucene40");
                }
            }
        };
        config.setCodec(codec);

        this.writer = new IndexWriter(directory, config);
    } catch (final Exception ex) {
        throw new RuntimeException(ex);
    }
}