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;
}
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);
}
}
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;
}
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;
}
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));
}
}
Aggregations