Search in sources :

Example 26 with ConstantScoreQuery

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

the class EnumField method getRangeQuery.

/**
   * {@inheritDoc}
   */
@Override
public Query getRangeQuery(QParser parser, SchemaField field, String min, String max, boolean minInclusive, boolean maxInclusive) {
    Integer minValue = stringValueToIntValue(min);
    Integer maxValue = stringValueToIntValue(max);
    if (field.multiValued() && field.hasDocValues() && !field.indexed()) {
        // for the multi-valued dv-case, the default rangeimpl over toInternal is correct
        return super.getRangeQuery(parser, field, minValue.toString(), maxValue.toString(), minInclusive, maxInclusive);
    }
    Query query = null;
    final boolean matchOnly = field.hasDocValues() && !field.indexed();
    if (matchOnly) {
        long lowerValue = Long.MIN_VALUE;
        long upperValue = Long.MAX_VALUE;
        if (minValue != null) {
            lowerValue = minValue.longValue();
            if (minInclusive == false) {
                ++lowerValue;
            }
        }
        if (maxValue != null) {
            upperValue = maxValue.longValue();
            if (maxInclusive == false) {
                --upperValue;
            }
        }
        query = new ConstantScoreQuery(NumericDocValuesField.newRangeQuery(field.getName(), lowerValue, upperValue));
    } else {
        query = LegacyNumericRangeQuery.newIntRange(field.getName(), DEFAULT_PRECISION_STEP, min == null ? null : minValue, max == null ? null : maxValue, minInclusive, maxInclusive);
    }
    return query;
}
Also used : Query(org.apache.lucene.search.Query) LegacyNumericRangeQuery(org.apache.solr.legacy.LegacyNumericRangeQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery)

Example 27 with ConstantScoreQuery

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

the class TestSolrQueryParser method testCSQ.

@Test
public void testCSQ() throws Exception {
    SolrQueryRequest req = req();
    QParser qParser = QParser.getParser("text:x^=3", req);
    Query q = qParser.getQuery();
    assertTrue(q instanceof BoostQuery);
    assertTrue(((BoostQuery) q).getQuery() instanceof ConstantScoreQuery);
    assertEquals(3.0, ((BoostQuery) q).getBoost(), 0.0f);
    qParser = QParser.getParser("(text:x text:y)^=-3", req);
    q = qParser.getQuery();
    assertTrue(q instanceof BoostQuery);
    assertTrue(((BoostQuery) q).getQuery() instanceof ConstantScoreQuery);
    assertEquals(-3.0, ((BoostQuery) q).getBoost(), 0.0f);
    req.close();
}
Also used : SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) Query(org.apache.lucene.search.Query) PointInSetQuery(org.apache.lucene.search.PointInSetQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) TermInSetQuery(org.apache.lucene.search.TermInSetQuery) TermQuery(org.apache.lucene.search.TermQuery) FilterQuery(org.apache.solr.query.FilterQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) BoostQuery(org.apache.lucene.search.BoostQuery) Test(org.junit.Test)

Example 28 with ConstantScoreQuery

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

the class PredicateBonusQueryTest method testBasics.

@Test
public void testBasics() throws Exception {
    IndexWriter writer = new IndexWriter(this.directory, new IndexWriterConfig(Version.LUCENE_35, new WhitespaceAnalyzer(Version.LUCENE_35)));
    writer.addDocument(new DocumentBuilder().add("value", "5").build());
    writer.close();
    IndexReader reader = IndexReader.open(this.directory);
    IndexSearcher searcher = new IndexSearcher(reader);
    Query query = new ConstantScoreQuery(new TermQuery(new Term("value", "5")));
    Assert.assertEquals(1.0, searcher.search(query, 1).getMaxScore(), 0.00001);
    Query noBonus = new PredicateBonusQuery(query, Predicates.NONE, 10.0f);
    Assert.assertEquals(1.0, searcher.search(noBonus, 1).getMaxScore(), 0.00001);
    Query bonus = new PredicateBonusQuery(query, Predicates.ALL, 100.0f);
    Assert.assertEquals(101.0, searcher.search(bonus, 1).getMaxScore(), 0.00001);
    Query noMatch = new TermQuery(new Term("value", "not5"));
    Assert.assertEquals(Double.NaN, searcher.search(noMatch, 1).getMaxScore(), 0.00001);
    Query noMatchNoBonus = new PredicateBonusQuery(noMatch, Predicates.NONE, 10.0f);
    Assert.assertEquals(Double.NaN, searcher.search(noMatchNoBonus, 1).getMaxScore(), 0.00001);
    Query noMatchIgnoresBonus = new PredicateBonusQuery(noMatch, Predicates.ALL, 100.0f);
    Assert.assertEquals(Double.NaN, searcher.search(noMatchIgnoresBonus, 1).getMaxScore(), 0.00001);
}
Also used : WhitespaceAnalyzer(org.apache.lucene.analysis.WhitespaceAnalyzer) IndexSearcher(org.apache.lucene.search.IndexSearcher) TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) TermQuery(org.apache.lucene.search.TermQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) IndexWriter(org.apache.lucene.index.IndexWriter) DocumentBuilder(com.greplin.lucene.document.DocumentBuilder) IndexReader(org.apache.lucene.index.IndexReader) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) Term(org.apache.lucene.index.Term) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig) Test(org.junit.Test)

Example 29 with ConstantScoreQuery

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

the class MinHashFilterTest method buildQuery.

private Query buildQuery(String field, String query, int min, int hashCount, int hashSetSize) throws IOException {
    TokenizerChain chain = createMinHashAnalyzer(min, hashCount, hashSetSize);
    ArrayList<String> tokens = getTokens(chain, field, query);
    chain.close();
    BooleanQuery.Builder builder = new BooleanQuery.Builder();
    for (String token : tokens) {
        builder.add(new ConstantScoreQuery(new TermQuery(new Term("text", token))), Occur.SHOULD);
    }
    builder.setDisableCoord(true);
    return builder.build();
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) Term(org.apache.lucene.index.Term)

Example 30 with ConstantScoreQuery

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

the class SolrAuthoritySetQuery method createWeight.

@Override
public Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException {
    if (!(searcher instanceof SolrIndexSearcher)) {
        throw new IllegalStateException("Must have a SolrIndexSearcher");
    }
    String[] auths = authorities.substring(1).split(authorities.substring(0, 1));
    SolrIndexSearcher solrIndexSearcher = (SolrIndexSearcher) searcher;
    Properties p = solrIndexSearcher.getSchema().getResourceLoader().getCoreProperties();
    boolean doPermissionChecks = Boolean.parseBoolean(p.getProperty("alfresco.doPermissionChecks", "true"));
    boolean hasGlobalRead = false;
    final HashSet<String> globalReaders = GlobalReaders.getReaders();
    for (String auth : auths) {
        if (globalReaders.contains(auth)) {
            hasGlobalRead = true;
            break;
        }
    }
    if (hasGlobalRead || (doPermissionChecks == false)) {
        return new MatchAllDocsQuery().createWeight(searcher, needsScores);
    }
    BitsFilter readFilter = getACLFilter(auths, QueryConstants.FIELD_READER, solrIndexSearcher);
    BitsFilter ownerFilter = getOwnerFilter(auths, solrIndexSearcher);
    if (globalReaders.contains(PermissionService.OWNER_AUTHORITY)) {
        readFilter.or(ownerFilter);
        return new ConstantScoreQuery(readFilter).createWeight(searcher, needsScores);
    } else {
        String[] ownerAuth = { PermissionService.OWNER_AUTHORITY };
        BitsFilter ownerReadFilter = getACLFilter(ownerAuth, QueryConstants.FIELD_READER, solrIndexSearcher);
        ownerReadFilter.and(ownerFilter);
        readFilter.or(ownerReadFilter);
        return new ConstantScoreQuery(readFilter).createWeight(searcher, needsScores);
    }
}
Also used : ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) SolrIndexSearcher(org.apache.solr.search.SolrIndexSearcher) Properties(java.util.Properties) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery)

Aggregations

ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)68 BooleanQuery (org.apache.lucene.search.BooleanQuery)43 TermQuery (org.apache.lucene.search.TermQuery)42 Query (org.apache.lucene.search.Query)40 Term (org.apache.lucene.index.Term)30 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)19 BoostQuery (org.apache.lucene.search.BoostQuery)14 IndexSearcher (org.apache.lucene.search.IndexSearcher)14 ArrayList (java.util.ArrayList)13 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)13 TermRangeQuery (org.apache.lucene.search.TermRangeQuery)12 MultiTermQuery (org.apache.lucene.search.MultiTermQuery)11 Document (org.apache.lucene.document.Document)10 Test (org.junit.Test)10 Sort (org.apache.lucene.search.Sort)9 TopDocs (org.apache.lucene.search.TopDocs)9 StringField (org.apache.lucene.document.StringField)8 BooleanClause (org.apache.lucene.search.BooleanClause)8 RegexpQuery (org.apache.lucene.search.RegexpQuery)8 SortField (org.apache.lucene.search.SortField)8