use of org.apache.lucene.search.Similarity in project neo4j-mobile-android by neo4j-contrib.
the class IndexType method getIndexType.
static IndexType getIndexType(IndexIdentifier identifier, Map<String, String> config) {
String type = config.get(LuceneIndexImplementation.KEY_TYPE);
IndexType result = null;
Similarity similarity = getCustomSimilarity(config);
boolean toLowerCase = parseBoolean(config.get(LuceneIndexImplementation.KEY_TO_LOWER_CASE), true);
Analyzer customAnalyzer = getCustomAnalyzer(config);
if (type != null) {
// Use the built in alternatives... "exact" or "fulltext"
if (type.equals("exact")) {
result = EXACT;
} else if (type.equals("fulltext")) {
Analyzer analyzer = customAnalyzer;
if (analyzer == null) {
analyzer = toLowerCase ? LuceneDataSource.LOWER_CASE_WHITESPACE_ANALYZER : LuceneDataSource.WHITESPACE_ANALYZER;
}
result = new CustomType(analyzer, toLowerCase, similarity);
}
} else {
// 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.Similarity in project graphdb by neo4j-attic.
the class LuceneDataSource method getIndexWriter.
synchronized IndexWriter getIndexWriter(IndexIdentifier identifier) {
Triplet<IndexWriter, AtomicBoolean, SnapshotDeletionPolicy> writer = indexWriters.get(identifier);
if (writer != null) {
return writer.first();
}
try {
Directory dir = getDirectory(baseStorePath, identifier);
directoryExists(dir);
IndexType type = getType(identifier);
SnapshotDeletionPolicy deletionPolicy = new MultipleBackupDeletionPolicy();
IndexWriter indexWriter = new IndexWriter(dir, type.analyzer, deletionPolicy, MaxFieldLength.UNLIMITED);
writer = Triplet.of(indexWriter, new AtomicBoolean(), deletionPolicy);
Similarity similarity = type.getSimilarity();
if (similarity != null) {
writer.first().setSimilarity(similarity);
}
// TODO We should tamper with this value and see how it affects the
// general performance. Lucene docs says rather <10 for mixed
// reads/writes
// writer.setMergeFactor( 8 );
indexWriters.put(identifier, writer);
return writer.first();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
use of org.apache.lucene.search.Similarity in project graphdb by neo4j-attic.
the class IndexType method getIndexType.
static IndexType getIndexType(IndexIdentifier identifier, Map<String, String> config) {
String type = config.get(LuceneIndexImplementation.KEY_TYPE);
IndexType result = null;
Similarity similarity = getCustomSimilarity(config);
boolean toLowerCase = parseBoolean(config.get(LuceneIndexImplementation.KEY_TO_LOWER_CASE), true);
Analyzer customAnalyzer = getCustomAnalyzer(config);
if (type != null) {
// Use the built in alternatives... "exact" or "fulltext"
if (type.equals("exact")) {
result = EXACT;
} else if (type.equals("fulltext")) {
Analyzer analyzer = customAnalyzer;
if (analyzer == null) {
analyzer = toLowerCase ? LuceneDataSource.LOWER_CASE_WHITESPACE_ANALYZER : LuceneDataSource.WHITESPACE_ANALYZER;
}
result = new CustomType(analyzer, toLowerCase, similarity);
}
} else {
// 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.Similarity in project greplin-lucene-utils by Cue.
the class PredicateQuery method createWeight.
/**
* Returns a Weight that applies the predicate to the enclosed query's Weight.
* This is accomplished by overriding the Scorer returned by the Weight.
* @param searcher the searcher to create a weight for.
* @return a Weight that applies the predicate to the query.
* @throws IOException if IO issues occur.
*/
@Override
public Weight createWeight(final Searcher searcher) throws IOException {
final Weight weight = this.query.createWeight(searcher);
final Similarity similarity = this.query.getSimilarity(searcher);
return new Weight() {
private float value;
// pass these methods through to enclosed query's weight
@Override
public float getValue() {
return this.value;
}
@Override
public boolean scoresDocsOutOfOrder() {
return false;
}
public float sumOfSquaredWeights() throws IOException {
return weight.sumOfSquaredWeights() * getBoost() * getBoost();
}
@Override
public void normalize(final float v) {
// incorporate boost
weight.normalize(v * getBoost());
this.value = weight.getValue();
}
@Override
public Explanation explain(final IndexReader reader, final int i) throws IOException {
Explanation inner = weight.explain(reader, i);
Bits predicate = PredicateQuery.this.predicate.get(reader);
if (predicate.get(i)) {
return inner;
} else {
Explanation result = new Explanation(0.0f, "failure to match predicate: " + predicate.toString());
result.addDetail(inner);
return result;
}
}
@Override
public Query getQuery() {
return PredicateQuery.this;
}
@Override
public Scorer scorer(final IndexReader reader, final boolean scoreDocsInOrder, final boolean topScorer) throws IOException {
Bits predicate = PredicateQuery.this.predicate.get(reader);
return PredicateQuery.getScorer(reader, similarity, weight, this, predicate);
}
};
}
use of org.apache.lucene.search.Similarity in project greplin-lucene-utils by Cue.
the class ConstantQueryNormBoostingQuery method rewrite.
@Override
public Query rewrite(final IndexReader reader) throws IOException {
BooleanQuery result = new BooleanQuery() {
@Override
public Similarity getSimilarity(final Searcher searcher) {
final Similarity base = searcher.getSimilarity();
return new DefaultSimilarity() {
@Override
public float queryNorm(final float sumOfSquaredWeights) {
return base.queryNorm(sumOfSquaredWeights);
}
@Override
public float coord(final int overlap, final int max) {
switch(overlap) {
case // matched only one clause
1:
return 1.0f;
case // matched both clauses
2:
return ConstantQueryNormBoostingQuery.this.boost;
default:
return 0.0f;
}
}
};
}
};
result.add(this.match, BooleanClause.Occur.MUST);
result.add(this.context, BooleanClause.Occur.SHOULD);
return result;
}
Aggregations