Search in sources :

Example 1 with Similarity

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;
}
Also used : Similarity(org.apache.lucene.search.Similarity) Analyzer(org.apache.lucene.analysis.Analyzer)

Example 2 with Similarity

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);
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Similarity(org.apache.lucene.search.Similarity) IndexWriter(org.apache.lucene.index.IndexWriter) IOException(java.io.IOException) SnapshotDeletionPolicy(org.apache.lucene.index.SnapshotDeletionPolicy) Directory(org.apache.lucene.store.Directory) FSDirectory(org.apache.lucene.store.FSDirectory)

Example 3 with Similarity

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;
}
Also used : Similarity(org.apache.lucene.search.Similarity) Analyzer(org.apache.lucene.analysis.Analyzer)

Example 4 with Similarity

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);
        }
    };
}
Also used : Similarity(org.apache.lucene.search.Similarity) Explanation(org.apache.lucene.search.Explanation) IndexReader(org.apache.lucene.index.IndexReader) Bits(org.apache.lucene.util.Bits) Weight(org.apache.lucene.search.Weight)

Example 5 with Similarity

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;
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) Similarity(org.apache.lucene.search.Similarity) DefaultSimilarity(org.apache.lucene.search.DefaultSimilarity) Searcher(org.apache.lucene.search.Searcher) DefaultSimilarity(org.apache.lucene.search.DefaultSimilarity)

Aggregations

Similarity (org.apache.lucene.search.Similarity)6 IOException (java.io.IOException)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Analyzer (org.apache.lucene.analysis.Analyzer)2 IndexWriter (org.apache.lucene.index.IndexWriter)2 Directory (org.apache.lucene.store.Directory)2 FSDirectory (org.apache.lucene.store.FSDirectory)2 IndexReader (org.apache.lucene.index.IndexReader)1 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)1 SnapshotDeletionPolicy (org.apache.lucene.index.SnapshotDeletionPolicy)1 BooleanQuery (org.apache.lucene.search.BooleanQuery)1 DefaultSimilarity (org.apache.lucene.search.DefaultSimilarity)1 Explanation (org.apache.lucene.search.Explanation)1 Searcher (org.apache.lucene.search.Searcher)1 Weight (org.apache.lucene.search.Weight)1 Bits (org.apache.lucene.util.Bits)1