Search in sources :

Example 66 with ConstantScoreQuery

use of org.apache.lucene.search.ConstantScoreQuery in project SearchServices by Alfresco.

the class Solr4QueryParser method createFingerPrintQuery.

/**
 * @param field
 * @param queryText
 * @param analysisMode
 *
 * @param luceneFunction
 * @return
 * @throws IOException
 * @throws ParseException
 */
private Query createFingerPrintQuery(String field, String queryText, Collection values, AnalysisMode analysisMode, LuceneFunction luceneFunction) throws IOException, ParseException {
    String[] parts = queryText.split("_");
    if (parts.length == 0) {
        return createIsNodeQuery("T");
    }
    if (values != null) {
        int bandSize = 1;
        float fraction = -1;
        float truePositive = 1;
        if (parts.length > 1) {
            fraction = Float.parseFloat(parts[1]);
            if (fraction > 1) {
                fraction /= 100;
            }
        }
        if (parts.length > 2) {
            truePositive = Float.parseFloat(parts[2]);
            if (truePositive > 1) {
                truePositive /= 100;
            }
            bandSize = computeBandSize(values.size(), fraction, truePositive);
        }
        BooleanQuery.Builder builder = new BooleanQuery.Builder();
        BooleanQuery.Builder childBuilder = new BooleanQuery.Builder();
        int rowInBand = 0;
        for (Object token : values) {
            TermQuery tq = new TermQuery(new Term("MINHASH", token.toString()));
            if (bandSize == 1) {
                builder.add(new ConstantScoreQuery(tq), Occur.SHOULD);
            } else {
                childBuilder.add(new ConstantScoreQuery(tq), Occur.MUST);
                rowInBand++;
                if (rowInBand == bandSize) {
                    builder.add(new ConstantScoreQuery(childBuilder.setDisableCoord(true).build()), Occur.SHOULD);
                    childBuilder = new BooleanQuery.Builder();
                    rowInBand = 0;
                }
            }
        }
        // start
        if (childBuilder.build().clauses().size() > 0) {
            for (Object token : values) {
                TermQuery tq = new TermQuery(new Term("MINHASH", token.toString()));
                childBuilder.add(new ConstantScoreQuery(tq), Occur.MUST);
                rowInBand++;
                if (rowInBand == bandSize) {
                    builder.add(new ConstantScoreQuery(childBuilder.setDisableCoord(true).build()), Occur.SHOULD);
                    break;
                }
            }
        }
        builder.setDisableCoord(true);
        if (parts.length == 2) {
            builder.setMinimumNumberShouldMatch((int) (Math.ceil(values.size() * fraction)));
        }
        Query q = builder.build();
        return q;
    } else {
        return getFieldQueryImpl(field, queryText, analysisMode, luceneFunction);
    }
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) RegexpQuery(org.apache.lucene.search.RegexpQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) Builder(org.apache.lucene.search.BooleanQuery.Builder) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) JSONObject(org.json.JSONObject) Term(org.apache.lucene.index.Term) Builder(org.apache.lucene.search.BooleanQuery.Builder)

Example 67 with ConstantScoreQuery

use of org.apache.lucene.search.ConstantScoreQuery in project greplin-lucene-utils by Cue.

the class Queries method constantScore.

/**
 * Returns a version of the given query that always matches with the
 * given score.
 * @param query the query
 * @param score the desired score
 * @return the constant score query
 */
public static ConstantScoreQuery constantScore(final Query query, final float score) {
    ConstantScoreQuery result = new ConstantScoreQuery(query);
    result.setBoost(score);
    return result;
}
Also used : ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery)

Example 68 with ConstantScoreQuery

use of org.apache.lucene.search.ConstantScoreQuery in project neo4j by neo4j.

the class LuceneDocumentStructureTest method shouldBuildQueryRepresentingStringProperty.

@Test
void shouldBuildQueryRepresentingStringProperty() {
    // given
    BooleanQuery booleanQuery = (BooleanQuery) newSeekQuery("Characters");
    ConstantScoreQuery query = (ConstantScoreQuery) booleanQuery.clauses().get(0).getQuery();
    // then
    assertEquals("Characters", ((TermQuery) query.getQuery()).getTerm().text());
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) Test(org.junit.jupiter.api.Test)

Example 69 with ConstantScoreQuery

use of org.apache.lucene.search.ConstantScoreQuery in project neo4j by neo4j.

the class LuceneDocumentStructureTest method shouldBuildQueryRepresentingMultipleProperties.

@Test
void shouldBuildQueryRepresentingMultipleProperties() {
    // given
    BooleanQuery booleanQuery = (BooleanQuery) newSeekQuery("foo", "bar");
    ConstantScoreQuery fooScoreQuery = (ConstantScoreQuery) booleanQuery.clauses().get(0).getQuery();
    TermQuery fooTermQuery = (TermQuery) fooScoreQuery.getQuery();
    ConstantScoreQuery barScoreQuery = (ConstantScoreQuery) booleanQuery.clauses().get(1).getQuery();
    TermQuery barTermQuery = (TermQuery) barScoreQuery.getQuery();
    // then
    assertEquals("foo", fooTermQuery.getTerm().text());
    assertEquals("bar", barTermQuery.getTerm().text());
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) Test(org.junit.jupiter.api.Test)

Example 70 with ConstantScoreQuery

use of org.apache.lucene.search.ConstantScoreQuery in project neo4j by neo4j.

the class FulltextIndexReader method query.

@Override
public void query(QueryContext context, IndexProgressor.EntityValueClient client, IndexQueryConstraints constraints, PropertyIndexQuery... queries) throws IndexNotApplicableKernelException {
    BooleanQuery.Builder queryBuilder = new BooleanQuery.Builder();
    for (PropertyIndexQuery indexQuery : queries) {
        if (indexQuery.type() == PropertyIndexQuery.IndexQueryType.fulltextSearch) {
            PropertyIndexQuery.FulltextSearchPredicate fulltextSearch = (PropertyIndexQuery.FulltextSearchPredicate) indexQuery;
            try {
                queryBuilder.add(parseFulltextQuery(fulltextSearch.query()), BooleanClause.Occur.SHOULD);
            } catch (ParseException e) {
                throw new RuntimeException("Could not parse the given fulltext search query: '" + fulltextSearch.query() + "'.", e);
            }
        } else {
            // Not fulltext query
            assertNotComposite(queries);
            assertCypherCompatible();
            Query query;
            if (indexQuery.type() == PropertyIndexQuery.IndexQueryType.stringContains) {
                PropertyIndexQuery.StringContainsPredicate scp = (PropertyIndexQuery.StringContainsPredicate) indexQuery;
                String searchTerm = QueryParser.escape(scp.contains().stringValue());
                Term term = new Term(propertyNames[0], "*" + searchTerm + "*");
                query = new WildcardQuery(term);
            } else if (indexQuery.type() == PropertyIndexQuery.IndexQueryType.stringSuffix) {
                PropertyIndexQuery.StringSuffixPredicate ssp = (PropertyIndexQuery.StringSuffixPredicate) indexQuery;
                String searchTerm = QueryParser.escape(ssp.suffix().stringValue());
                Term term = new Term(propertyNames[0], "*" + searchTerm);
                query = new WildcardQuery(term);
            } else if (indexQuery.type() == PropertyIndexQuery.IndexQueryType.stringPrefix) {
                PropertyIndexQuery.StringPrefixPredicate spp = (PropertyIndexQuery.StringPrefixPredicate) indexQuery;
                String searchTerm = spp.prefix().stringValue();
                Term term = new Term(propertyNames[0], searchTerm);
                query = new LuceneDocumentStructure.PrefixMultiTermsQuery(term);
            } else if (indexQuery.getClass() == PropertyIndexQuery.ExactPredicate.class && indexQuery.valueGroup() == ValueGroup.TEXT) {
                PropertyIndexQuery.ExactPredicate exact = (PropertyIndexQuery.ExactPredicate) indexQuery;
                String searchTerm = ((TextValue) exact.value()).stringValue();
                Term term = new Term(propertyNames[0], searchTerm);
                query = new ConstantScoreQuery(new TermQuery(term));
            } else if (indexQuery.getClass() == PropertyIndexQuery.TextRangePredicate.class) {
                PropertyIndexQuery.TextRangePredicate sp = (PropertyIndexQuery.TextRangePredicate) indexQuery;
                query = newRangeSeekByStringQuery(propertyNames[0], sp.from(), sp.fromInclusive(), sp.to(), sp.toInclusive());
            } else {
                throw new IndexNotApplicableKernelException("A fulltext schema index cannot answer " + indexQuery.type() + " queries on " + indexQuery.valueCategory() + " values.");
            }
            queryBuilder.add(query, BooleanClause.Occur.MUST);
        }
    }
    Query query = queryBuilder.build();
    ValuesIterator itr = searchLucene(query, constraints, context, context.cursorContext(), context.memoryTracker());
    IndexProgressor progressor = new FulltextIndexProgressor(itr, client, constraints);
    client.initialize(index, progressor, queries, constraints, true);
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) Query(org.apache.lucene.search.Query) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) PropertyIndexQuery(org.neo4j.internal.kernel.api.PropertyIndexQuery) PropertyIndexQuery(org.neo4j.internal.kernel.api.PropertyIndexQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) LuceneDocumentStructure(org.neo4j.kernel.api.impl.schema.LuceneDocumentStructure) TermQuery(org.apache.lucene.search.TermQuery) Term(org.apache.lucene.index.Term) IndexNotApplicableKernelException(org.neo4j.internal.kernel.api.exceptions.schema.IndexNotApplicableKernelException) IndexProgressor(org.neo4j.kernel.api.index.IndexProgressor) ParseException(org.apache.lucene.queryparser.classic.ParseException) ValuesIterator(org.neo4j.kernel.api.impl.index.collector.ValuesIterator)

Aggregations

ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)71 BooleanQuery (org.apache.lucene.search.BooleanQuery)46 TermQuery (org.apache.lucene.search.TermQuery)45 Query (org.apache.lucene.search.Query)43 Term (org.apache.lucene.index.Term)33 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)19 IndexSearcher (org.apache.lucene.search.IndexSearcher)17 BoostQuery (org.apache.lucene.search.BoostQuery)14 ArrayList (java.util.ArrayList)13 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)13 TermRangeQuery (org.apache.lucene.search.TermRangeQuery)12 TopDocs (org.apache.lucene.search.TopDocs)12 MultiTermQuery (org.apache.lucene.search.MultiTermQuery)11 Test (org.junit.Test)11 Document (org.apache.lucene.document.Document)10 Sort (org.apache.lucene.search.Sort)9 StringField (org.apache.lucene.document.StringField)8 IndexReader (org.apache.lucene.index.IndexReader)8 BooleanClause (org.apache.lucene.search.BooleanClause)8 RegexpQuery (org.apache.lucene.search.RegexpQuery)8