Java Code Examples for org.apache.lucene.search.ConstantScoreQuery

The following examples show how to use org.apache.lucene.search.ConstantScoreQuery. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: Elasticsearch   Source File: CustomFieldQuery.java    License: Apache License 2.0 6 votes vote down vote up
@Override
void flatten(Query sourceQuery, IndexReader reader, Collection<Query> flatQueries, float boost) throws IOException {
    if (sourceQuery instanceof SpanTermQuery) {
        super.flatten(new TermQuery(((SpanTermQuery) sourceQuery).getTerm()), reader, flatQueries, boost);
    } else if (sourceQuery instanceof ConstantScoreQuery) {
        flatten(((ConstantScoreQuery) sourceQuery).getQuery(), reader, flatQueries, boost);
    } else if (sourceQuery instanceof FunctionScoreQuery) {
        flatten(((FunctionScoreQuery) sourceQuery).getSubQuery(), reader, flatQueries, boost);
    } else if (sourceQuery instanceof MultiPhrasePrefixQuery) {
        flatten(sourceQuery.rewrite(reader), reader, flatQueries, boost);
    } else if (sourceQuery instanceof FiltersFunctionScoreQuery) {
        flatten(((FiltersFunctionScoreQuery) sourceQuery).getSubQuery(), reader, flatQueries, boost);
    } else if (sourceQuery instanceof MultiPhraseQuery) {
        MultiPhraseQuery q = ((MultiPhraseQuery) sourceQuery);
        convertMultiPhraseQuery(0, new int[q.getTermArrays().size()], q, q.getTermArrays(), q.getPositions(), reader, flatQueries);
    } else if (sourceQuery instanceof BlendedTermQuery) {
        final BlendedTermQuery blendedTermQuery = (BlendedTermQuery) sourceQuery;
        flatten(blendedTermQuery.rewrite(reader), reader, flatQueries, boost);
    } else {
        super.flatten(sourceQuery, reader, flatQueries, boost);
    }
}
 
Example 2
Source Project: lucene-solr   Source File: HighlighterTest.java    License: Apache License 2.0 6 votes vote down vote up
public void testGetBestFragmentsConstantScore() throws Exception {
  TestHighlightRunner helper = new TestHighlightRunner() {

    @Override
    public void run() throws Exception {
      numHighlights = 0;
      if (random().nextBoolean()) {
        BooleanQuery.Builder bq = new BooleanQuery.Builder();
        bq.add(new ConstantScoreQuery(new TermQuery(
            new Term(FIELD_NAME, "kennedy"))), Occur.MUST);
        bq.add(new ConstantScoreQuery(new TermQuery(new Term(FIELD_NAME, "kennedy"))), Occur.MUST);
        doSearching(bq.build());
      } else {
        doSearching(new ConstantScoreQuery(new TermQuery(new Term(FIELD_NAME,
            "kennedy"))));
      }
      doStandardHighlights(analyzer, searcher, hits, query, HighlighterTest.this);
      assertTrue("Failed to find correct number of highlights " + numHighlights + " found",
          numHighlights == 4);
    }
  };

  helper.start();
}
 
Example 3
Source Project: lucene-solr   Source File: BBoxStrategy.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Query makeQuery(SpatialArgs args) {
  Shape shape = args.getShape();
  if (!(shape instanceof Rectangle))
    throw new UnsupportedOperationException("Can only query by Rectangle, not " + shape);

  Rectangle bbox = (Rectangle) shape;
  Query spatial;

  // Useful for understanding Relations:
  // http://edndoc.esri.com/arcsde/9.1/general_topics/understand_spatial_relations.htm
  SpatialOperation op = args.getOperation();
       if( op == SpatialOperation.BBoxIntersects ) spatial = makeIntersects(bbox);
  else if( op == SpatialOperation.BBoxWithin     ) spatial = makeWithin(bbox);
  else if( op == SpatialOperation.Contains       ) spatial = makeContains(bbox);
  else if( op == SpatialOperation.Intersects     ) spatial = makeIntersects(bbox);
  else if( op == SpatialOperation.IsEqualTo      ) spatial = makeEquals(bbox);
  else if( op == SpatialOperation.IsDisjointTo   ) spatial = makeDisjoint(bbox);
  else if( op == SpatialOperation.IsWithin       ) spatial = makeWithin(bbox);
  else { //no Overlaps support yet
      throw new UnsupportedSpatialOperation(op);
  }
  return new ConstantScoreQuery(spatial);
}
 
Example 4
Source Project: lucene-solr   Source File: ToParentBlockJoinQuery.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Weight createWeight(IndexSearcher searcher, org.apache.lucene.search.ScoreMode weightScoreMode, float boost) throws IOException {
  ScoreMode childScoreMode = weightScoreMode.needsScores() ? scoreMode : ScoreMode.None;
  final Weight childWeight;
  if (childScoreMode == ScoreMode.None) {
    // we don't need to compute a score for the child query so we wrap
    // it under a constant score query that can early terminate if the
    // minimum score is greater than 0 and the total hits that match the
    // query is not requested.
    childWeight = searcher.rewrite(new ConstantScoreQuery(childQuery)).createWeight(searcher, weightScoreMode, 0f);
  } else {
    // if the score is needed we force the collection mode to COMPLETE because the child query cannot skip
    // non-competitive documents.
    childWeight = childQuery.createWeight(searcher, weightScoreMode.needsScores() ? COMPLETE : weightScoreMode, boost);
  }
  return new BlockJoinWeight(this, childWeight, parentsFilter, childScoreMode);
}
 
Example 5
Source Project: lucene-solr   Source File: FuzzyLikeThisQuery.java    License: Apache License 2.0 6 votes vote down vote up
private Query newTermQuery(IndexReader reader, Term term) throws IOException {
  if (ignoreTF) {
    return new ConstantScoreQuery(new TermQuery(term));
  } else {
    // we build an artificial TermStates that will give an overall df and ttf
    // equal to 1
    TermStates context = new TermStates(reader.getContext());
    for (LeafReaderContext leafContext : reader.leaves()) {
      Terms terms = leafContext.reader().terms(term.field());
      if (terms != null) {
        TermsEnum termsEnum = terms.iterator();
        if (termsEnum.seekExact(term.bytes())) {
          int freq = 1 - context.docFreq(); // we want the total df and ttf to be 1
          context.register(termsEnum.termState(), leafContext.ord, freq, freq);
        }
      }
    }
    return new TermQuery(term, context);
  }
}
 
Example 6
Source Project: lucene-solr   Source File: BBoxStrategy.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Query makeQuery(SpatialArgs args) {
  Shape shape = args.getShape();
  if (!(shape instanceof Rectangle))
    throw new UnsupportedOperationException("Can only query by Rectangle, not " + shape);

  Rectangle bbox = (Rectangle) shape;
  Query spatial;

  // Useful for understanding Relations:
  // http://edndoc.esri.com/arcsde/9.1/general_topics/understand_spatial_relations.htm
  SpatialOperation op = args.getOperation();
       if( op == SpatialOperation.BBoxIntersects ) spatial = makeIntersects(bbox);
  else if( op == SpatialOperation.BBoxWithin     ) spatial = makeWithin(bbox);
  else if( op == SpatialOperation.Contains       ) spatial = makeContains(bbox);
  else if( op == SpatialOperation.Intersects     ) spatial = makeIntersects(bbox);
  else if( op == SpatialOperation.IsEqualTo      ) spatial = makeEquals(bbox);
  else if( op == SpatialOperation.IsDisjointTo   ) spatial = makeDisjoint(bbox);
  else if( op == SpatialOperation.IsWithin       ) spatial = makeWithin(bbox);
  else { //no Overlaps support yet
      throw new UnsupportedSpatialOperation(op);
  }
  return new ConstantScoreQuery(spatial);
}
 
Example 7
Source Project: lucene-solr   Source File: QueryUtils.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Combines a scoring query with a non-scoring (filter) query.
 * If both parameters are null then return a {@link MatchAllDocsQuery}.
 * If only {@code scoreQuery} is present then return it.
 * If only {@code filterQuery} is present then return it wrapped with constant scoring.
 * If neither are null then we combine with a BooleanQuery.
 */
public static Query combineQueryAndFilter(Query scoreQuery, Query filterQuery) {
  // check for *:* is simple and avoids needless BooleanQuery wrapper even though BQ.rewrite optimizes this away
  if (scoreQuery == null || scoreQuery instanceof MatchAllDocsQuery) {
    if (filterQuery == null) {
      return new MatchAllDocsQuery(); // default if nothing -- match everything
    } else {
      return new ConstantScoreQuery(filterQuery);
    }
  } else {
    if (filterQuery == null || filterQuery instanceof MatchAllDocsQuery) {
      return scoreQuery;
    } else {
      return new BooleanQuery.Builder()
          .add(scoreQuery, Occur.MUST)
          .add(filterQuery, Occur.FILTER)
          .build();
    }
  }
}
 
Example 8
Source Project: lucene-solr   Source File: HashQParserPlugin.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Weight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost) throws IOException {

  SolrIndexSearcher solrIndexSearcher = (SolrIndexSearcher)searcher;
  IndexReaderContext context = solrIndexSearcher.getTopReaderContext();

  List<LeafReaderContext> leaves =  context.leaves();
  FixedBitSet[] fixedBitSets = new FixedBitSet[leaves.size()];

  for(LeafReaderContext leaf : leaves) {
    try {
      SegmentPartitioner segmentPartitioner = new SegmentPartitioner(leaf,worker,workers, keys, solrIndexSearcher);
      segmentPartitioner.run();
      fixedBitSets[segmentPartitioner.context.ord] = segmentPartitioner.docs;
    } catch(Exception e) {
      throw new IOException(e);
    }
  }

  ConstantScoreQuery constantScoreQuery = new ConstantScoreQuery(new BitsFilter(fixedBitSets));
  return searcher.rewrite(constantScoreQuery).createWeight(searcher, ScoreMode.COMPLETE_NO_SCORES, boost);
}
 
Example 9
Source Project: lucene-solr   Source File: TestMinHashQParser.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testBandsWrap() throws SyntaxError {

  NamedList<Object> par = new NamedList<>();
  par.add("sim", "0.8");
  par.add("tp", "0.694");
  par.add("sep", ",");
  par.add("debug", "false");

  QParser qparser = h.getCore().getQueryPlugin("minhash").createParser("1, 2, 3, 4, 5, 6, 7, 8, 9, 10", SolrParams.toSolrParams(par), null, null);
  Query query = qparser.getQuery();

  BooleanQuery bq = (BooleanQuery)query;
  assertEquals(4, bq.clauses().size());
  for(BooleanClause clause : bq.clauses()) {
    assertEquals(3, ((BooleanQuery)((ConstantScoreQuery)clause.getQuery()).getQuery())  .clauses().size());
  }

}
 
Example 10
Source Project: Elasticsearch   Source File: DefaultSearchContext.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Query searchFilter(String[] types) {
    Query filter = mapperService().searchFilter(types);
    if (filter == null && aliasFilter == null) {
        return null;
    }
    BooleanQuery.Builder bq = new BooleanQuery.Builder();
    if (filter != null) {
        bq.add(filter, Occur.MUST);
    }
    if (aliasFilter != null) {
        bq.add(aliasFilter, Occur.MUST);
    }
    return new ConstantScoreQuery(bq.build());
}
 
Example 11
Source Project: Elasticsearch   Source File: IndexedGeoBoundingBoxQuery.java    License: Apache License 2.0 5 votes vote down vote up
private static Query westGeoBoundingBoxFilter(GeoPoint topLeft, GeoPoint bottomRight, GeoPointFieldMapperLegacy.GeoPointFieldType fieldType) {
    BooleanQuery.Builder filter = new BooleanQuery.Builder();
    filter.setMinimumNumberShouldMatch(1);
    filter.add(fieldType.lonFieldType().rangeQuery(null, bottomRight.lon(), true, true), Occur.SHOULD);
    filter.add(fieldType.lonFieldType().rangeQuery(topLeft.lon(), null, true, true), Occur.SHOULD);
    filter.add(fieldType.latFieldType().rangeQuery(bottomRight.lat(), topLeft.lat(), true, true), Occur.MUST);
    return new ConstantScoreQuery(filter.build());
}
 
Example 12
Source Project: Elasticsearch   Source File: TypeFieldMapper.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Query termQuery(Object value, @Nullable QueryParseContext context) {
    if (indexOptions() == IndexOptions.NONE) {
        return new ConstantScoreQuery(new PrefixQuery(new Term(UidFieldMapper.NAME, Uid.typePrefixAsBytes(BytesRefs.toBytesRef(value)))));
    }
    return new ConstantScoreQuery(new TermQuery(createTerm(value)));
}
 
Example 13
@Override
public Weight createWeight(IndexSearcher searcher, boolean requiresScore) throws IOException
{
    if(!(searcher instanceof SolrIndexSearcher))
    {
        throw new IllegalStateException("Must have a SolrIndexSearcher");
    }

    String[] auths = authorities.substring(1).split(authorities.substring(0, 1));
    BitsFilter denyFilter  = getACLFilter(auths, QueryConstants.FIELD_DENIED, (SolrIndexSearcher) searcher);
    return new ConstantScoreQuery(denyFilter).createWeight(searcher, false);
}
 
Example 14
@Override
public Weight createWeight(IndexSearcher searcher, boolean needsScore) throws IOException
{
    if(!(searcher instanceof SolrIndexSearcher))
    {
        throw new IllegalStateException("Must have a SolrIndexSearcher");
    }

    BitsFilter ownerFilter = getOwnerFilter(authority, (SolrIndexSearcher)searcher);
    return new ConstantScoreQuery(ownerFilter).createWeight(searcher, false);
}
 
Example 15
Source Project: lucene-solr   Source File: TestUnifiedHighlighterMTQ.java    License: Apache License 2.0 5 votes vote down vote up
public void testWildcardInConstantScore() throws Exception {
  RandomIndexWriter iw = new RandomIndexWriter(random(), dir, indexAnalyzer);

  Field body = new Field("body", "", fieldType);
  Document doc = new Document();
  doc.add(body);

  body.setStringValue("This is a test.");
  iw.addDocument(doc);
  body.setStringValue("Test a one sentence document.");
  iw.addDocument(doc);

  IndexReader ir = iw.getReader();
  iw.close();

  IndexSearcher searcher = newSearcher(ir);
  UnifiedHighlighter highlighter = randomUnifiedHighlighter(searcher, indexAnalyzer);
  ConstantScoreQuery query = new ConstantScoreQuery(new WildcardQuery(new Term("body", "te*")));
  TopDocs topDocs = searcher.search(query, 10, Sort.INDEXORDER);
  assertEquals(2, topDocs.totalHits.value);
  String snippets[] = highlighter.highlight("body", query, topDocs);
  assertEquals(2, snippets.length);
  assertEquals("This is a <b>test</b>.", snippets[0]);
  assertEquals("<b>Test</b> a one sentence document.", snippets[1]);

  ir.close();
}
 
Example 16
public void testSumScoreAggregator() throws Exception {
  Directory indexDir = newDirectory();
  Directory taxoDir = newDirectory();

  DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir);
  IndexWriter iw = new IndexWriter(indexDir, newIndexWriterConfig(new MockAnalyzer(random())));

  FacetsConfig config = new FacetsConfig();

  for(int i = atLeast(30); i > 0; --i) {
    Document doc = new Document();
    if (random().nextBoolean()) { // don't match all documents
      doc.add(new StringField("f", "v", Field.Store.NO));
    }
    doc.add(new FacetField("dim", "a"));
    iw.addDocument(config.build(taxoWriter, doc));
  }
  
  DirectoryReader r = DirectoryReader.open(iw);
  DirectoryTaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoWriter);
  
  FacetsCollector fc = new FacetsCollector(true);
  BoostQuery csq = new BoostQuery(new ConstantScoreQuery(new MatchAllDocsQuery()), 2f);
  
  TopDocs td = FacetsCollector.search(newSearcher(r), csq, 10, fc);

  Facets facets = new TaxonomyFacetSumValueSource(taxoReader, config, fc, DoubleValuesSource.SCORES);
  
  int expected = (int) (csq.getBoost() * td.totalHits.value);
  assertEquals(expected, facets.getSpecificValue("dim", "a").intValue());

  iw.close();
  IOUtils.close(taxoWriter, taxoReader, taxoDir, r, indexDir);
}
 
Example 17
Source Project: lucene-solr   Source File: TestExtractors.java    License: Apache License 2.0 5 votes vote down vote up
public void testConstantScoreQueryExtractor() {

    BooleanQuery.Builder bq = new BooleanQuery.Builder();
    bq.add(new TermQuery(new Term("f", "q1")), BooleanClause.Occur.MUST);
    bq.add(new TermQuery(new Term("f", "q2")), BooleanClause.Occur.SHOULD);

    Query csqWithQuery = new ConstantScoreQuery(bq.build());
    Set<Term> expected = Collections.singleton(new Term("f", "q1"));
    assertEquals(expected, collectTerms(csqWithQuery));

  }
 
Example 18
Source Project: lucene-solr   Source File: FilterQuery.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Weight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost) throws IOException {
  // SolrRequestInfo reqInfo = SolrRequestInfo.getRequestInfo();

  if (!(searcher instanceof SolrIndexSearcher)) {
    // delete-by-query won't have SolrIndexSearcher
    return new BoostQuery(new ConstantScoreQuery(q), 0).createWeight(searcher, scoreMode, 1f);
  }

  SolrIndexSearcher solrSearcher = (SolrIndexSearcher)searcher;
  DocSet docs = solrSearcher.getDocSet(q);
  // reqInfo.addCloseHook(docs);  // needed for off-heap refcounting

  return new BoostQuery(new SolrConstantScoreQuery(docs.getTopFilter()), 0).createWeight(searcher, scoreMode, 1f);
}
 
Example 19
Source Project: lucene-solr   Source File: NumericFieldType.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Override the default existence behavior, so that the non-docValued/norms implementation matches NaN values for double and float fields.
 * The [* TO *] query for those fields does not match 'NaN' values, so they must be matched separately.
 * <p>
 * For doubles and floats the query behavior is equivalent to (field:[* TO *] OR field:NaN).
 * For all other numeric types, the default existence query behavior is used.
 */
@Override
public Query getSpecializedExistenceQuery(QParser parser, SchemaField field) {
  if (doubleOrFloat.contains(getNumberType())) {
    return new ConstantScoreQuery(new BooleanQuery.Builder()
        .add(getSpecializedRangeQuery(parser, field, null, null, true, true), BooleanClause.Occur.SHOULD)
        .add(getFieldQuery(parser, field, Float.toString(Float.NaN)), BooleanClause.Occur.SHOULD)
        .setMinimumNumberShouldMatch(1).build());
  } else {
    return super.getSpecializedExistenceQuery(parser, field);
  }
}
 
Example 20
Source Project: lucene-solr   Source File: CurrencyFieldType.java    License: Apache License 2.0 5 votes vote down vote up
private Query getRangeQueryInternal(QParser parser, SchemaField field, final CurrencyValue p1, final CurrencyValue p2, final boolean minInclusive, final boolean maxInclusive) {
  String currencyCode = (p1 != null) ? p1.getCurrencyCode() :
      (p2 != null) ? p2.getCurrencyCode() : defaultCurrency;

  // ValueSourceRangeFilter doesn't check exists(), so we have to
  final Query docsWithValues = new DocValuesFieldExistsQuery(getAmountField(field).getName());
  final Query vsRangeFilter = new ValueSourceRangeFilter
      (new RawCurrencyValueSource(field, currencyCode, parser),
          p1 == null ? null : p1.getAmount() + "",
          p2 == null ? null : p2.getAmount() + "",
          minInclusive, maxInclusive);
  return new ConstantScoreQuery(new BooleanQuery.Builder()
      .add(docsWithValues, Occur.FILTER)
      .add(vsRangeFilter, Occur.FILTER).build());
}
 
Example 21
Source Project: lucene-solr   Source File: TestSolrQueryParser.java    License: Apache License 2.0 5 votes vote down vote up
@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);

  req.close();
}
 
Example 22
Source Project: solr-redis   Source File: ConstantScoreQueryExtractor.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void extract(final ConstantScoreQuery q, final Iterable<QueryExtractor<? extends Query>> extractors,
        final List<Query> extractedQueries) throws UnsupportedOperationException {
  if (q.getQuery() != null) {
    extractQuery(q.getQuery(), extractors, extractedQueries);
  } else {
    extractedQueries.add(q);
  }
}
 
Example 23
Source Project: solr-redis   Source File: ConstantScoreQueryExtractor.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void extractSubQueriesFields(final ConstantScoreQuery q,
        final Iterable<QueryExtractor<? extends Query>> extractors,
        final Set<String> extractedFields) throws UnsupportedOperationException {
  if (q.getQuery() != null) {
    extractFields(q.getQuery(), extractors, extractedFields);
  }
}
 
Example 24
@Test
public void testExtractWrappedQuery() {
  Query q1 = mock(Query.class);

  ConstantScoreQueryExtractor constantScoreQueryExtractor = new ConstantScoreQueryExtractor();
  ConstantScoreQuery constantScoreQuery = new ConstantScoreQuery(q1);

  List<Query> extractedQueries = new ArrayList<>();

  constantScoreQueryExtractor.extract(constantScoreQuery, DEFAULT_EXTRACTORS, extractedQueries);
  assertEquals(1, extractedQueries.size());
  assertEquals(q1, extractedQueries.get(0));
}
 
Example 25
@Test
public void testExtractSubqueryField() {
  Query q1 = new TermQuery(new Term("field1", "value1"));

  ConstantScoreQueryExtractor constantScoreQueryExtractor = new ConstantScoreQueryExtractor();
  ConstantScoreQuery constantScoreQuery = new ConstantScoreQuery(q1);

  Set<String> extractedFieldNames = new HashSet<>();

  constantScoreQueryExtractor.extractSubQueriesFields(constantScoreQuery, DEFAULT_EXTRACTORS, extractedFieldNames);
  assertEquals(1, extractedFieldNames.size());
  assertTrue(extractedFieldNames.contains("field1"));
}
 
Example 26
Source Project: solr-redis   Source File: TestRedisQParser.java    License: Apache License 2.0 5 votes vote down vote up
private static Set<Term> extractTerms(IndexSearcher searcher, Query query) throws IOException {
  final Set<Term> terms = new HashSet<>();
  Query rewrittenQuery = searcher.rewrite(query);
  if (rewrittenQuery instanceof ConstantScoreQuery) {
    ConstantScoreQuery constantScoreQuery = (ConstantScoreQuery)rewrittenQuery;
    rewrittenQuery = constantScoreQuery.getQuery();
  }
  searcher.createNormalizedWeight(rewrittenQuery, true).extractTerms(terms);
  return terms;
}
 
Example 27
@Override
public Query handle(Query query, QueryTransformer queryTransformer) {
    ConstantScoreQuery constantScoreQuery = (ConstantScoreQuery) query;
    Query newInnerConstantScoreQuery = queryTransformer.transform(constantScoreQuery.getQuery());
    if (newInnerConstantScoreQuery != constantScoreQuery.getQuery()) {
        return new ConstantScoreQuery(newInnerConstantScoreQuery);
    }
    return query;
}
 
Example 28
Source Project: crate   Source File: ProfileScorer.java    License: Apache License 2.0 5 votes vote down vote up
ProfileScorer(ProfileWeight w, Scorer scorer, QueryProfileBreakdown profile) throws IOException {
    super(w);
    this.scorer = scorer;
    this.profileWeight = w;
    scoreTimer = profile.getTimer(QueryTimingType.SCORE);
    nextDocTimer = profile.getTimer(QueryTimingType.NEXT_DOC);
    advanceTimer = profile.getTimer(QueryTimingType.ADVANCE);
    matchTimer = profile.getTimer(QueryTimingType.MATCH);
    shallowAdvanceTimer = profile.getTimer(QueryTimingType.SHALLOW_ADVANCE);
    computeMaxScoreTimer = profile.getTimer(QueryTimingType.COMPUTE_MAX_SCORE);
    ProfileScorer profileScorer = null;
    if (w.getQuery() instanceof ConstantScoreQuery && scorer instanceof ProfileScorer) {
        //Case when we have a totalHits query and it is not cached
        profileScorer = (ProfileScorer) scorer;
    } else if (w.getQuery() instanceof ConstantScoreQuery && scorer.getChildren().size() == 1) {
        //Case when we have a top N query. If the scorer has no children, it is because it is cached
        //and in that case we do not do any special treatment
        Scorable childScorer = scorer.getChildren().iterator().next().child;
        if (childScorer instanceof ProfileScorer) {
            profileScorer = (ProfileScorer) childScorer;
        }
    }
    if (profileScorer != null) {
        isConstantScoreQuery = true;
        profile.setTimer(QueryTimingType.NEXT_DOC, profileScorer.nextDocTimer);
        profile.setTimer(QueryTimingType.ADVANCE, profileScorer.advanceTimer);
        profile.setTimer(QueryTimingType.MATCH, profileScorer.matchTimer);
    } else {
        isConstantScoreQuery = false;
    }
}
 
Example 29
Source Project: crate   Source File: ExistsQueryBuilder.java    License: Apache License 2.0 5 votes vote down vote up
private static Query newFieldExistsQuery(QueryShardContext context, String field) {
    MappedFieldType fieldType = context.getMapperService().fullName(field);
    if (fieldType == null) {
        // The field does not exist as a leaf but could be an object so
        // check for an object mapper
        if (context.getObjectMapper(field) != null) {
            return newObjectFieldExistsQuery(context, field);
        }
        return Queries.newMatchNoDocsQuery("No field \"" + field + "\" exists in mappings.");
    }
    Query filter = fieldType.existsQuery(context);
    return new ConstantScoreQuery(filter);
}
 
Example 30
Source Project: crate   Source File: ExistsQueryBuilder.java    License: Apache License 2.0 5 votes vote down vote up
private static Query newObjectFieldExistsQuery(QueryShardContext context, String objField) {
    BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder();
    Collection<String> fields = context.simpleMatchToIndexNames(objField + ".*");
    for (String field : fields) {
        Query existsQuery = context.getMapperService().fullName(field).existsQuery(context);
        booleanQuery.add(existsQuery, Occur.SHOULD);
    }
    return new ConstantScoreQuery(booleanQuery.build());
}