Search in sources :

Example 26 with Similarity

use of org.apache.lucene.search.similarities.Similarity in project elasticsearch by elastic.

the class BlendedTermQueryTests method setSimilarity.

public IndexSearcher setSimilarity(IndexSearcher searcher) {
    Similarity similarity = random().nextBoolean() ? new BM25Similarity() : new ClassicSimilarity();
    searcher.setSimilarity(similarity);
    return searcher;
}
Also used : ClassicSimilarity(org.apache.lucene.search.similarities.ClassicSimilarity) Similarity(org.apache.lucene.search.similarities.Similarity) ClassicSimilarity(org.apache.lucene.search.similarities.ClassicSimilarity) BM25Similarity(org.apache.lucene.search.similarities.BM25Similarity) BM25Similarity(org.apache.lucene.search.similarities.BM25Similarity)

Example 27 with Similarity

use of org.apache.lucene.search.similarities.Similarity in project neo4j by neo4j.

the class WritableIndexReferenceFactory method newIndexWriter.

private IndexWriter newIndexWriter(IndexIdentifier identifier) {
    try {
        Directory indexDirectory = getIndexDirectory(identifier);
        IndexType type = getType(identifier);
        IndexWriterConfig writerConfig = new IndexWriterConfig(type.analyzer);
        writerConfig.setIndexDeletionPolicy(new MultipleBackupDeletionPolicy());
        Similarity similarity = type.getSimilarity();
        if (similarity != null) {
            writerConfig.setSimilarity(similarity);
        }
        return new IndexWriter(indexDirectory, writerConfig);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : Similarity(org.apache.lucene.search.similarities.Similarity) IndexWriter(org.apache.lucene.index.IndexWriter) IOException(java.io.IOException) Directory(org.apache.lucene.store.Directory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Example 28 with Similarity

use of org.apache.lucene.search.similarities.Similarity in project neo4j by neo4j.

the class IndexType method getIndexType.

static IndexType getIndexType(Map<String, String> config) {
    String type = config.get(LuceneIndexImplementation.KEY_TYPE);
    IndexType result = null;
    Similarity similarity = getCustomSimilarity(config);
    Boolean toLowerCaseUnbiased = config.get(LuceneIndexImplementation.KEY_TO_LOWER_CASE) != null ? parseBoolean(config.get(LuceneIndexImplementation.KEY_TO_LOWER_CASE), true) : null;
    Analyzer customAnalyzer = getCustomAnalyzer(config);
    if (type != null) {
        // Use the built in alternatives... "exact" or "fulltext"
        if ("exact".equals(type)) {
            // In the exact case we default to false
            boolean toLowerCase = TRUE.equals(toLowerCaseUnbiased);
            result = toLowerCase ? new CustomType(new LowerCaseKeywordAnalyzer(), true, similarity) : EXACT;
        } else if ("fulltext".equals(type)) {
            // In the fulltext case we default to true
            boolean toLowerCase = !FALSE.equals(toLowerCaseUnbiased);
            Analyzer analyzer = customAnalyzer;
            if (analyzer == null) {
                analyzer = TRUE.equals(toLowerCase) ? LuceneDataSource.LOWER_CASE_WHITESPACE_ANALYZER : LuceneDataSource.WHITESPACE_ANALYZER;
            }
            result = new CustomType(analyzer, toLowerCase, similarity);
        }
    } else {
        // In the custom case we default to true
        boolean toLowerCase = !FALSE.equals(toLowerCaseUnbiased);
        // Use custom analyzer
        if (customAnalyzer == null) {
            throw new IllegalArgumentException("No 'type' was given (which can point out " + "built-in analyzers, such as 'exact' and 'fulltext')" + " and no 'analyzer' was given either (which can point out a custom " + Analyzer.class.getName() + " to use)");
        }
        result = new CustomType(customAnalyzer, toLowerCase, similarity);
    }
    return result;
}
Also used : Similarity(org.apache.lucene.search.similarities.Similarity) Analyzer(org.apache.lucene.analysis.Analyzer)

Example 29 with Similarity

use of org.apache.lucene.search.similarities.Similarity in project lucene-solr by apache.

the class BaseSimilarityTestCase method getSimilarity.

/** returns the similarity in use for the field */
protected Similarity getSimilarity(String field) {
    SolrCore core = h.getCore();
    RefCounted<SolrIndexSearcher> searcher = core.getSearcher();
    Similarity sim = searcher.get().getSimilarity(true);
    searcher.decref();
    while (sim instanceof PerFieldSimilarityWrapper) {
        sim = ((PerFieldSimilarityWrapper) sim).get(field);
    }
    return sim;
}
Also used : Similarity(org.apache.lucene.search.similarities.Similarity) SolrCore(org.apache.solr.core.SolrCore) PerFieldSimilarityWrapper(org.apache.lucene.search.similarities.PerFieldSimilarityWrapper) SolrIndexSearcher(org.apache.solr.search.SolrIndexSearcher)

Example 30 with Similarity

use of org.apache.lucene.search.similarities.Similarity in project lucene-solr by apache.

the class TestBulkSchemaAPI method assertFieldSimilarity.

/**
   * whitebox checks the Similarity for the specified field according to {@link SolrCore#getLatestSchema}
   * 
   * Executes each of the specified Similarity-accepting validators.
   */
@SafeVarargs
private static <T extends Similarity> void assertFieldSimilarity(String fieldname, Class<T> expected, Consumer<T>... validators) {
    CoreContainer cc = jetty.getCoreContainer();
    try (SolrCore core = cc.getCore("collection1")) {
        SimilarityFactory simfac = core.getLatestSchema().getSimilarityFactory();
        assertNotNull(simfac);
        assertTrue("test only works with SchemaSimilarityFactory", simfac instanceof SchemaSimilarityFactory);
        Similarity mainSim = core.getLatestSchema().getSimilarity();
        assertNotNull(mainSim);
        // sanity check simfac vs sim in use - also verify infom called on simfac, otherwise exception
        assertEquals(mainSim, simfac.getSimilarity());
        assertTrue("test only works with PerFieldSimilarityWrapper, SchemaSimilarityFactory redefined?", mainSim instanceof PerFieldSimilarityWrapper);
        Similarity fieldSim = ((PerFieldSimilarityWrapper) mainSim).get(fieldname);
        assertEquals("wrong sim for field=" + fieldname, expected, fieldSim.getClass());
        Arrays.asList(validators).forEach(v -> v.accept((T) fieldSim));
    }
}
Also used : CoreContainer(org.apache.solr.core.CoreContainer) SweetSpotSimilarity(org.apache.lucene.misc.SweetSpotSimilarity) Similarity(org.apache.lucene.search.similarities.Similarity) DFISimilarity(org.apache.lucene.search.similarities.DFISimilarity) BM25Similarity(org.apache.lucene.search.similarities.BM25Similarity) SolrCore(org.apache.solr.core.SolrCore) PerFieldSimilarityWrapper(org.apache.lucene.search.similarities.PerFieldSimilarityWrapper) SchemaSimilarityFactory(org.apache.solr.search.similarities.SchemaSimilarityFactory) SimilarityFactory(org.apache.solr.schema.SimilarityFactory) SchemaSimilarityFactory(org.apache.solr.search.similarities.SchemaSimilarityFactory)

Aggregations

Similarity (org.apache.lucene.search.similarities.Similarity)48 BM25Similarity (org.apache.lucene.search.similarities.BM25Similarity)15 ClassicSimilarity (org.apache.lucene.search.similarities.ClassicSimilarity)15 Directory (org.apache.lucene.store.Directory)9 PerFieldSimilarityWrapper (org.apache.lucene.search.similarities.PerFieldSimilarityWrapper)8 SweetSpotSimilarity (org.apache.lucene.misc.SweetSpotSimilarity)7 IOException (java.io.IOException)6 Document (org.apache.lucene.document.Document)5 Term (org.apache.lucene.index.Term)5 IndexSearcher (org.apache.lucene.search.IndexSearcher)5 Collectors (java.util.stream.Collectors)4 IntStream (java.util.stream.IntStream)4 Field (org.apache.lucene.document.Field)4 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)4 NormValueSource (org.apache.lucene.queries.function.valuesource.NormValueSource)4 BytesRef (org.apache.lucene.util.BytesRef)4 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)3 Store (org.apache.lucene.document.Field.Store)3 IndexWriter (org.apache.lucene.index.IndexWriter)3 ConstValueSource (org.apache.lucene.queries.function.valuesource.ConstValueSource)3