Java 类org.apache.lucene.search.ConstantScoreQuery 实例源码

项目:elasticsearch_my    文件:CandidateQueryTests.java   
private void duelRun(PercolateQuery.QueryStore queryStore, MemoryIndex memoryIndex, IndexSearcher shardSearcher) throws IOException {
    boolean requireScore = randomBoolean();
    IndexSearcher percolateSearcher = memoryIndex.createSearcher();
    Query percolateQuery = fieldType.percolateQuery("type", queryStore, new BytesArray("{}"), percolateSearcher);
    Query query = requireScore ? percolateQuery : new ConstantScoreQuery(percolateQuery);
    TopDocs topDocs = shardSearcher.search(query, 10);

    Query controlQuery = new ControlQuery(memoryIndex, queryStore);
    controlQuery = requireScore ? controlQuery : new ConstantScoreQuery(controlQuery);
    TopDocs controlTopDocs = shardSearcher.search(controlQuery, 10);
    assertThat(topDocs.totalHits, equalTo(controlTopDocs.totalHits));
    assertThat(topDocs.scoreDocs.length, equalTo(controlTopDocs.scoreDocs.length));
    for (int j = 0; j < topDocs.scoreDocs.length; j++) {
        assertThat(topDocs.scoreDocs[j].doc, equalTo(controlTopDocs.scoreDocs[j].doc));
        assertThat(topDocs.scoreDocs[j].score, equalTo(controlTopDocs.scoreDocs[j].score));
        if (requireScore) {
            Explanation explain1 = shardSearcher.explain(query, topDocs.scoreDocs[j].doc);
            Explanation explain2 = shardSearcher.explain(controlQuery, controlTopDocs.scoreDocs[j].doc);
            assertThat(explain1.isMatch(), equalTo(explain2.isMatch()));
            assertThat(explain1.getValue(), equalTo(explain2.getValue()));
        }
    }
}
项目:elasticsearch_my    文件:DoubleNestedSortingTests.java   
@Override
protected void assertAvgScoreMode(Query parentFilter, IndexSearcher searcher) throws IOException {
    MultiValueMode sortMode = MultiValueMode.AVG;
    Query childFilter = Queries.not(parentFilter);
    XFieldComparatorSource nestedComparatorSource = createFieldComparator("field2", sortMode, -127, createNested(searcher, parentFilter, childFilter));
    Query query = new ToParentBlockJoinQuery(new ConstantScoreQuery(childFilter), new QueryBitSetProducer(parentFilter), ScoreMode.None);
    Sort sort = new Sort(new SortField("field2", nestedComparatorSource));
    TopDocs topDocs = searcher.search(query, 5, sort);
    assertThat(topDocs.totalHits, equalTo(7));
    assertThat(topDocs.scoreDocs.length, equalTo(5));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(11));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[0]).fields[0]).intValue(), equalTo(2));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(7));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[1]).fields[0]).intValue(), equalTo(2));
    assertThat(topDocs.scoreDocs[2].doc, equalTo(3));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[2]).fields[0]).intValue(), equalTo(3));
    assertThat(topDocs.scoreDocs[3].doc, equalTo(15));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[3]).fields[0]).intValue(), equalTo(3));
    assertThat(topDocs.scoreDocs[4].doc, equalTo(19));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[4]).fields[0]).intValue(), equalTo(3));
}
项目:elasticsearch_my    文件:FloatNestedSortingTests.java   
protected void assertAvgScoreMode(Query parentFilter, IndexSearcher searcher, IndexFieldData.XFieldComparatorSource innerFieldComparator) throws IOException {
    MultiValueMode sortMode = MultiValueMode.AVG;
    Query childFilter = Queries.not(parentFilter);
    XFieldComparatorSource nestedComparatorSource = createFieldComparator("field2", sortMode, -127, createNested(searcher, parentFilter, childFilter));
    Query query = new ToParentBlockJoinQuery(new ConstantScoreQuery(childFilter), new QueryBitSetProducer(parentFilter), ScoreMode.None);
    Sort sort = new Sort(new SortField("field2", nestedComparatorSource));
    TopDocs topDocs = searcher.search(query, 5, sort);
    assertThat(topDocs.totalHits, equalTo(7));
    assertThat(topDocs.scoreDocs.length, equalTo(5));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(11));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[0]).fields[0]).intValue(), equalTo(2));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(7));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[1]).fields[0]).intValue(), equalTo(2));
    assertThat(topDocs.scoreDocs[2].doc, equalTo(3));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[2]).fields[0]).intValue(), equalTo(3));
    assertThat(topDocs.scoreDocs[3].doc, equalTo(15));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[3]).fields[0]).intValue(), equalTo(3));
    assertThat(topDocs.scoreDocs[4].doc, equalTo(19));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[4]).fields[0]).intValue(), equalTo(3));
}
项目:elasticsearch_my    文件:AbstractNumberNestedSortingTestCase.java   
protected void assertAvgScoreMode(Query parentFilter, IndexSearcher searcher) throws IOException {
    MultiValueMode sortMode = MultiValueMode.AVG;
    Query childFilter = Queries.not(parentFilter);
    XFieldComparatorSource nestedComparatorSource = createFieldComparator("field2", sortMode, -127, createNested(searcher, parentFilter, childFilter));
    Query query = new ToParentBlockJoinQuery(new ConstantScoreQuery(childFilter), new QueryBitSetProducer(parentFilter), ScoreMode.None);
    Sort sort = new Sort(new SortField("field2", nestedComparatorSource));
    TopDocs topDocs = searcher.search(query, 5, sort);
    assertThat(topDocs.totalHits, equalTo(7));
    assertThat(topDocs.scoreDocs.length, equalTo(5));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(11));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[0]).fields[0]).intValue(), equalTo(2));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(3));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[1]).fields[0]).intValue(), equalTo(3));
    assertThat(topDocs.scoreDocs[2].doc, equalTo(7));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[2]).fields[0]).intValue(), equalTo(3));
    assertThat(topDocs.scoreDocs[3].doc, equalTo(15));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[3]).fields[0]).intValue(), equalTo(3));
    assertThat(topDocs.scoreDocs[4].doc, equalTo(19));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[4]).fields[0]).intValue(), equalTo(4));
}
项目:Elasticsearch    文件:CustomFieldQuery.java   
@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);
    }
}
项目:topicrawler    文件:KneserNeyLM.java   
double[] getNumFollowerPrecede2(String ngram_str) {
    if (ngram_str == null)
        throw new IllegalAccessError("Ngram is null.");
    if(ngram_str.isEmpty())
        return getNumFollowerPrecede(_UNKOWN_NGRAM_LUCENE_DOCUMENT);

    double[] v = new double[4]; 
    Query query = new ConstantScoreQuery(new TermQuery(new Term("history", ngram_str)));
    try {
        Document doc = null;
        ScoreDoc[] hits = _searcher_ngram.search(query, Integer.MAX_VALUE).scoreDocs;
        if(hits.length < 1)
            return getNumFollowerPrecede(_UNKOWN_NGRAM_LUCENE_DOCUMENT);

        for(ScoreDoc hit : hits){
            doc = _searcher_ngram.doc(hit.doc);
            double[] v_ = getNumFollow(doc);
            for(int i = 0; i < v_.length; i++)
                v[i] += v_[i];
        }
    } catch (IOException e) {
        LOG.error("Could not get ngram {}. Luceneindex failed.", ngram_str, e);
    }
    return v;
}
项目:search    文件:SpatialFileQueryMaker.java   
protected Query makeQueryFromShape(Shape shape) {
  SpatialArgs args = new SpatialArgs(operation, shape);
  if (!Double.isNaN(distErrPct))
    args.setDistErrPct(distErrPct);

  if (score) {
    ValueSource valueSource = strategy.makeDistanceValueSource(shape.getCenter());
    return new CustomScoreQuery(strategy.makeQuery(args), new FunctionQuery(valueSource));
  } else {
    //strategy.makeQuery() could potentially score (isn't well defined) so instead we call
    // makeFilter() and wrap

    Filter filter = strategy.makeFilter(args);
    if (filter instanceof QueryWrapperFilter) {
      return ((QueryWrapperFilter)filter).getQuery();
    } else {
      return new ConstantScoreQuery(filter);
    }
  }
}
项目:search    文件:DrillDownQuery.java   
/** Merges (ORs) a new path into an existing AND'd
 *  clause. */ 
private void merge(String dim, String[] path) {
  int index = drillDownDims.get(dim);
  if (query.getClauses().length == drillDownDims.size()+1) {
    index++;
  }
  ConstantScoreQuery q = (ConstantScoreQuery) query.clauses().get(index).getQuery();
  if ((q.getQuery() instanceof BooleanQuery) == false) {
    // App called .add(dim, customQuery) and then tried to
    // merge a facet label in:
    throw new RuntimeException("cannot merge with custom Query");
  }
  String indexedField = config.getDimConfig(dim).indexFieldName;

  BooleanQuery bq = (BooleanQuery) q.getQuery();
  bq.add(new TermQuery(term(indexedField, dim, path)), Occur.SHOULD);
}
项目:search    文件:DrillDownQuery.java   
/** Expert: add a custom drill-down subQuery.  Use this
 *  when you have a separate way to drill-down on the
 *  dimension than the indexed facet ordinals. */
public void add(String dim, Query subQuery) {

  if (drillDownDims.containsKey(dim)) {
    throw new IllegalArgumentException("dimension \"" + dim + "\" already has a drill-down");
  }
  // TODO: we should use FilteredQuery?

  // So scores of the drill-down query don't have an
  // effect:
  final ConstantScoreQuery drillDownQuery = new ConstantScoreQuery(subQuery);
  drillDownQuery.setBoost(0.0f);

  query.add(drillDownQuery, Occur.MUST);

  drillDownDims.put(dim, drillDownDims.size());
}
项目:search    文件:DrillDownQuery.java   
/** Expert: add a custom drill-down Filter, e.g. when
 *  drilling down after range faceting. */
public void add(String dim, Filter subFilter) {

  if (drillDownDims.containsKey(dim)) {
    throw new IllegalArgumentException("dimension \"" + dim + "\" already has a drill-down");
  }

  // TODO: we should use FilteredQuery?

  // So scores of the drill-down query don't have an
  // effect:
  final ConstantScoreQuery drillDownQuery = new ConstantScoreQuery(subFilter);
  drillDownQuery.setBoost(0.0f);

  query.add(drillDownQuery, Occur.MUST);

  drillDownDims.put(dim, drillDownDims.size());
}
项目:search    文件:ICUCollationField.java   
@Override
public Query getRangeQuery(QParser parser, SchemaField field, String part1, String part2, boolean minInclusive, boolean maxInclusive) {
  String f = field.getName();
  BytesRef low = part1 == null ? null : getCollationKey(f, part1);
  BytesRef high = part2 == null ? null : getCollationKey(f, part2);
  if (!field.indexed() && field.hasDocValues()) {
    if (field.multiValued()) {
        return new ConstantScoreQuery(DocTermOrdsRangeFilter.newBytesRefRange(
            field.getName(), low, high, minInclusive, maxInclusive));
      } else {
        return new ConstantScoreQuery(FieldCacheRangeFilter.newBytesRefRange(
            field.getName(), low, high, minInclusive, maxInclusive));
      } 
  } else {
    return new TermRangeQuery(field.getName(), low, high, minInclusive, maxInclusive);
  }
}
项目:search    文件:SolrIndexSearcher.java   
/**
 * Returns the number of documents that match both <code>a</code> and <code>b</code>.
 * <p>
 * This method is cache-aware and may check as well as modify the cache.
 *
 * @return the number of documents in the intersection between <code>a</code> and <code>b</code>.
 * @throws IOException If there is a low-level I/O error.
 */
public int numDocs(Query a, DocSet b) throws IOException {
  if (filterCache != null) {
    // Negative query if absolute value different from original
    Query absQ = QueryUtils.getAbs(a);
    DocSet positiveA = getPositiveDocSet(absQ);
    return a==absQ ? b.intersectionSize(positiveA) : b.andNotSize(positiveA);
  } else {
    // If there isn't a cache, then do a single filtered query
    // NOTE: we cannot use FilteredQuery, because BitDocSet assumes it will never 
    // have deleted documents, but UninvertedField's doNegative has sets with deleted docs
    TotalHitCountCollector collector = new TotalHitCountCollector();
    BooleanQuery bq = new BooleanQuery();
    bq.add(QueryUtils.makeQueryable(a), BooleanClause.Occur.MUST);
    bq.add(new ConstantScoreQuery(b.getTopFilter()), BooleanClause.Occur.MUST);
    super.search(bq, null, collector);
    return collector.getTotalHits();
  }
}
项目:search    文件:CollationField.java   
@Override
public Query getRangeQuery(QParser parser, SchemaField field, String part1, String part2, boolean minInclusive, boolean maxInclusive) {
  String f = field.getName();
  BytesRef low = part1 == null ? null : getCollationKey(f, part1);
  BytesRef high = part2 == null ? null : getCollationKey(f, part2);
  if (!field.indexed() && field.hasDocValues()) {
    if (field.multiValued()) {
        return new ConstantScoreQuery(DocTermOrdsRangeFilter.newBytesRefRange(
            field.getName(), low, high, minInclusive, maxInclusive));
      } else {
        return new ConstantScoreQuery(FieldCacheRangeFilter.newBytesRefRange(
            field.getName(), low, high, minInclusive, maxInclusive));
      } 
  } else {
    return new TermRangeQuery(field.getName(), low, high, minInclusive, maxInclusive);
  }
}
项目:read-open-source-code    文件:SpatialFileQueryMaker.java   
protected Query makeQueryFromShape(Shape shape) {
  SpatialArgs args = new SpatialArgs(operation, shape);
  if (!Double.isNaN(distErrPct))
    args.setDistErrPct(distErrPct);

  if (score) {
    ValueSource valueSource = strategy.makeDistanceValueSource(shape.getCenter());
    return new CustomScoreQuery(strategy.makeQuery(args), new FunctionQuery(valueSource));
  } else {
    //strategy.makeQuery() could potentially score (isn't well defined) so instead we call
    // makeFilter() and wrap

    Filter filter = strategy.makeFilter(args);
    if (filter instanceof QueryWrapperFilter) {
      return ((QueryWrapperFilter)filter).getQuery();
    } else {
      return new ConstantScoreQuery(filter);
    }
  }
}
项目:read-open-source-code    文件:DrillDownQuery.java   
/** Merges (ORs) a new path into an existing AND'd
 *  clause. */ 
private void merge(String dim, String[] path) {
  int index = drillDownDims.get(dim);
  if (query.getClauses().length == drillDownDims.size()+1) {
    index++;
  }
  ConstantScoreQuery q = (ConstantScoreQuery) query.clauses().get(index).getQuery();
  if ((q.getQuery() instanceof BooleanQuery) == false) {
    // App called .add(dim, customQuery) and then tried to
    // merge a facet label in:
    throw new RuntimeException("cannot merge with custom Query");
  }
  String indexedField = config.getDimConfig(dim).indexFieldName;

  BooleanQuery bq = (BooleanQuery) q.getQuery();
  bq.add(new TermQuery(term(indexedField, dim, path)), Occur.SHOULD);
}
项目:read-open-source-code    文件:DrillDownQuery.java   
/** Expert: add a custom drill-down subQuery.  Use this
 *  when you have a separate way to drill-down on the
 *  dimension than the indexed facet ordinals. */
public void add(String dim, Query subQuery) {

  if (drillDownDims.containsKey(dim)) {
    throw new IllegalArgumentException("dimension \"" + dim + "\" already has a drill-down");
  }
  // TODO: we should use FilteredQuery?

  // So scores of the drill-down query don't have an
  // effect:
  final ConstantScoreQuery drillDownQuery = new ConstantScoreQuery(subQuery);
  drillDownQuery.setBoost(0.0f);

  query.add(drillDownQuery, Occur.MUST);

  drillDownDims.put(dim, drillDownDims.size());
}
项目:read-open-source-code    文件:DrillDownQuery.java   
/** Expert: add a custom drill-down Filter, e.g. when
 *  drilling down after range faceting. */
public void add(String dim, Filter subFilter) {

  if (drillDownDims.containsKey(dim)) {
    throw new IllegalArgumentException("dimension \"" + dim + "\" already has a drill-down");
  }

  // TODO: we should use FilteredQuery?

  // So scores of the drill-down query don't have an
  // effect:
  final ConstantScoreQuery drillDownQuery = new ConstantScoreQuery(subFilter);
  drillDownQuery.setBoost(0.0f);

  query.add(drillDownQuery, Occur.MUST);

  drillDownDims.put(dim, drillDownDims.size());
}
项目:read-open-source-code    文件:SolrIndexSearcher.java   
/**
 * Returns the number of documents that match both <code>a</code> and <code>b</code>.
 * <p>
 * This method is cache-aware and may check as well as modify the cache.
 *
 * @return the number of documents in the intersection between <code>a</code> and <code>b</code>.
 * @throws IOException If there is a low-level I/O error.
 */
public int numDocs(Query a, DocSet b) throws IOException {
  if (filterCache != null) {
    // Negative query if absolute value different from original
    Query absQ = QueryUtils.getAbs(a);
    DocSet positiveA = getPositiveDocSet(absQ);
    return a==absQ ? b.intersectionSize(positiveA) : b.andNotSize(positiveA);
  } else {
    // If there isn't a cache, then do a single filtered query
    // NOTE: we cannot use FilteredQuery, because BitDocSet assumes it will never 
    // have deleted documents, but UninvertedField's doNegative has sets with deleted docs
    TotalHitCountCollector collector = new TotalHitCountCollector();
    BooleanQuery bq = new BooleanQuery();
    bq.add(QueryUtils.makeQueryable(a), BooleanClause.Occur.MUST);
    bq.add(new ConstantScoreQuery(b.getTopFilter()), BooleanClause.Occur.MUST);
    super.search(bq, null, collector);
    return collector.getTotalHits();
  }
}
项目:read-open-source-code    文件:CollationField.java   
@Override
public Query getRangeQuery(QParser parser, SchemaField field, String part1, String part2, boolean minInclusive, boolean maxInclusive) {
  String f = field.getName();
  BytesRef low = part1 == null ? null : getCollationKey(f, part1);
  BytesRef high = part2 == null ? null : getCollationKey(f, part2);
  if (!field.indexed() && field.hasDocValues()) {
    if (field.multiValued()) {
        return new ConstantScoreQuery(DocTermOrdsRangeFilter.newBytesRefRange(
            field.getName(), low, high, minInclusive, maxInclusive));
      } else {
        return new ConstantScoreQuery(FieldCacheRangeFilter.newBytesRefRange(
            field.getName(), low, high, minInclusive, maxInclusive));
      } 
  } else {
    return new TermRangeQuery(field.getName(), low, high, minInclusive, maxInclusive);
  }
}
项目:read-open-source-code    文件:ICUCollationField.java   
@Override
public Query getRangeQuery(QParser parser, SchemaField field, String part1, String part2, boolean minInclusive, boolean maxInclusive) {
  String f = field.getName();
  BytesRef low = part1 == null ? null : getCollationKey(f, part1);
  BytesRef high = part2 == null ? null : getCollationKey(f, part2);
  if (!field.indexed() && field.hasDocValues()) {
    if (field.multiValued()) {
        return new ConstantScoreQuery(DocTermOrdsRangeFilter.newBytesRefRange(
            field.getName(), low, high, minInclusive, maxInclusive));
      } else {
        return new ConstantScoreQuery(FieldCacheRangeFilter.newBytesRefRange(
            field.getName(), low, high, minInclusive, maxInclusive));
      } 
  } else {
    return new TermRangeQuery(field.getName(), low, high, minInclusive, maxInclusive);
  }
}
项目:read-open-source-code    文件:SpatialFileQueryMaker.java   
protected Query makeQueryFromShape(Shape shape) {
  SpatialArgs args = new SpatialArgs(operation, shape);
  if (!Double.isNaN(distErrPct))
    args.setDistErrPct(distErrPct);

  if (score) {
    ValueSource valueSource = strategy.makeDistanceValueSource(shape.getCenter());
    return new CustomScoreQuery(strategy.makeQuery(args), new FunctionQuery(valueSource));
  } else {
    //strategy.makeQuery() could potentially score (isn't well defined) so instead we call
    // makeFilter() and wrap

    Filter filter = strategy.makeFilter(args);
    if (filter instanceof QueryWrapperFilter) {
      return ((QueryWrapperFilter)filter).getQuery();
    } else {
      return new ConstantScoreQuery(filter);
    }
  }
}
项目:read-open-source-code    文件:DrillDownQuery.java   
/** Merges (ORs) a new path into an existing AND'd
 *  clause. */ 
private void merge(String dim, String[] path) {
  int index = drillDownDims.get(dim);
  if (query.getClauses().length == drillDownDims.size()+1) {
    index++;
  }
  ConstantScoreQuery q = (ConstantScoreQuery) query.clauses().get(index).getQuery();
  if ((q.getQuery() instanceof BooleanQuery) == false) {
    // App called .add(dim, customQuery) and then tried to
    // merge a facet label in:
    throw new RuntimeException("cannot merge with custom Query");
  }
  String indexedField = config.getDimConfig(dim).indexFieldName;

  BooleanQuery bq = (BooleanQuery) q.getQuery();
  bq.add(new TermQuery(term(indexedField, dim, path)), Occur.SHOULD);
}
项目:read-open-source-code    文件:DrillDownQuery.java   
/** Expert: add a custom drill-down subQuery.  Use this
 *  when you have a separate way to drill-down on the
 *  dimension than the indexed facet ordinals. */
public void add(String dim, Query subQuery) {

  if (drillDownDims.containsKey(dim)) {
    throw new IllegalArgumentException("dimension \"" + dim + "\" already has a drill-down");
  }
  // TODO: we should use FilteredQuery?

  // So scores of the drill-down query don't have an
  // effect:
  final ConstantScoreQuery drillDownQuery = new ConstantScoreQuery(subQuery);
  drillDownQuery.setBoost(0.0f);

  query.add(drillDownQuery, Occur.MUST);

  drillDownDims.put(dim, drillDownDims.size());
}
项目:read-open-source-code    文件:DrillDownQuery.java   
/** Expert: add a custom drill-down Filter, e.g. when
 *  drilling down after range faceting. */
public void add(String dim, Filter subFilter) {

  if (drillDownDims.containsKey(dim)) {
    throw new IllegalArgumentException("dimension \"" + dim + "\" already has a drill-down");
  }

  // TODO: we should use FilteredQuery?

  // So scores of the drill-down query don't have an
  // effect:
  final ConstantScoreQuery drillDownQuery = new ConstantScoreQuery(subFilter);
  drillDownQuery.setBoost(0.0f);

  query.add(drillDownQuery, Occur.MUST);

  drillDownDims.put(dim, drillDownDims.size());
}
项目:read-open-source-code    文件:DrillDownQuery.java   
/** Merges (ORs) a new path into an existing AND'd
 *  clause. */ 
private void merge(String dim, String[] path) {
  int index = drillDownDims.get(dim);
  if (query.getClauses().length == drillDownDims.size()+1) {
    index++;
  }
  ConstantScoreQuery q = (ConstantScoreQuery) query.clauses().get(index).getQuery();
  if ((q.getQuery() instanceof BooleanQuery) == false) {
    // App called .add(dim, customQuery) and then tried to
    // merge a facet label in:
    throw new RuntimeException("cannot merge with custom Query");
  }
  String indexedField = config.getDimConfig(dim).indexFieldName;

  BooleanQuery bq = (BooleanQuery) q.getQuery();
  bq.add(new TermQuery(term(indexedField, dim, path)), Occur.SHOULD);
}
项目:read-open-source-code    文件:DrillDownQuery.java   
/** Expert: add a custom drill-down subQuery.  Use this
 *  when you have a separate way to drill-down on the
 *  dimension than the indexed facet ordinals. */
public void add(String dim, Query subQuery) {

  if (drillDownDims.containsKey(dim)) {
    throw new IllegalArgumentException("dimension \"" + dim + "\" already has a drill-down");
  }
  // TODO: we should use FilteredQuery?

  // So scores of the drill-down query don't have an
  // effect:
  final ConstantScoreQuery drillDownQuery = new ConstantScoreQuery(subQuery);
  drillDownQuery.setBoost(0.0f);

  query.add(drillDownQuery, Occur.MUST);

  drillDownDims.put(dim, drillDownDims.size());
}
项目:read-open-source-code    文件:DrillDownQuery.java   
/** Expert: add a custom drill-down Filter, e.g. when
 *  drilling down after range faceting. */
public void add(String dim, Filter subFilter) {

  if (drillDownDims.containsKey(dim)) {
    throw new IllegalArgumentException("dimension \"" + dim + "\" already has a drill-down");
  }

  // TODO: we should use FilteredQuery?

  // So scores of the drill-down query don't have an
  // effect:
  final ConstantScoreQuery drillDownQuery = new ConstantScoreQuery(subFilter);
  drillDownQuery.setBoost(0.0f);

  query.add(drillDownQuery, Occur.MUST);

  drillDownDims.put(dim, drillDownDims.size());
}
项目:read-open-source-code    文件:SolrIndexSearcher.java   
/**
 * Returns the number of documents that match both <code>a</code> and <code>b</code>.
 * <p>
 * This method is cache-aware and may check as well as modify the cache.
 *
 * @return the number of documents in the intersection between <code>a</code> and <code>b</code>.
 * @throws IOException If there is a low-level I/O error.
 */
public int numDocs(Query a, DocSet b) throws IOException {
  if (filterCache != null) {
    // Negative query if absolute value different from original
    Query absQ = QueryUtils.getAbs(a);
    DocSet positiveA = getPositiveDocSet(absQ);
    return a==absQ ? b.intersectionSize(positiveA) : b.andNotSize(positiveA);
  } else {
    // If there isn't a cache, then do a single filtered query
    // NOTE: we cannot use FilteredQuery, because BitDocSet assumes it will never 
    // have deleted documents, but UninvertedField's doNegative has sets with deleted docs
    TotalHitCountCollector collector = new TotalHitCountCollector();
    BooleanQuery bq = new BooleanQuery();
    bq.add(QueryUtils.makeQueryable(a), BooleanClause.Occur.MUST);
    bq.add(new ConstantScoreQuery(b.getTopFilter()), BooleanClause.Occur.MUST);
    super.search(bq, null, collector);
    return collector.getTotalHits();
  }
}
项目:read-open-source-code    文件:CollationField.java   
@Override
public Query getRangeQuery(QParser parser, SchemaField field, String part1, String part2, boolean minInclusive, boolean maxInclusive) {
  String f = field.getName();
  BytesRef low = part1 == null ? null : getCollationKey(f, part1);
  BytesRef high = part2 == null ? null : getCollationKey(f, part2);
  if (!field.indexed() && field.hasDocValues()) {
    if (field.multiValued()) {
        return new ConstantScoreQuery(DocTermOrdsRangeFilter.newBytesRefRange(
            field.getName(), low, high, minInclusive, maxInclusive));
      } else {
        return new ConstantScoreQuery(FieldCacheRangeFilter.newBytesRefRange(
            field.getName(), low, high, minInclusive, maxInclusive));
      } 
  } else {
    return new TermRangeQuery(field.getName(), low, high, minInclusive, maxInclusive);
  }
}
项目:Maskana-Gestor-de-Conocimiento    文件:SpatialFileQueryMaker.java   
protected Query makeQueryFromShape(Shape shape) {
  SpatialArgs args = new SpatialArgs(operation, shape);
  if (!Double.isNaN(distErrPct))
    args.setDistErrPct(distErrPct);

  if (score) {
    ValueSource valueSource = strategy.makeDistanceValueSource(shape.getCenter());
    return new CustomScoreQuery(strategy.makeQuery(args), new FunctionQuery(valueSource));
  } else {
    //strategy.makeQuery() could potentially score (isn't well defined) so instead we call
    // makeFilter() and wrap

    Filter filter = strategy.makeFilter(args);
    if (filter instanceof QueryWrapperFilter) {
      return ((QueryWrapperFilter)filter).getQuery();
    } else {
      return new ConstantScoreQuery(filter);
    }
  }
}
项目:elasticsearch_my    文件:QueryAnalyzerTests.java   
public void testExtractQueryMetadata_constantScoreQuery() {
    TermQuery termQuery1 = new TermQuery(new Term("_field", "_term"));
    ConstantScoreQuery constantScoreQuery = new ConstantScoreQuery(termQuery1);
    Result result = analyze(constantScoreQuery);
    assertThat(result.verified, is(true));
    List<Term> terms = new ArrayList<>(result.terms);
    assertThat(terms.size(), equalTo(1));
    assertThat(terms.get(0).field(), equalTo(termQuery1.getTerm().field()));
    assertThat(terms.get(0).bytes(), equalTo(termQuery1.getTerm().bytes()));
}
项目:elasticsearch_my    文件:QueryPhase.java   
private static boolean returnsDocsInOrder(Query query, SortAndFormats sf) {
    if (sf == null || Sort.RELEVANCE.equals(sf.sort)) {
        // sort by score
        // queries that return constant scores will return docs in index
        // order since Lucene tie-breaks on the doc id
        return query.getClass() == ConstantScoreQuery.class
                || query.getClass() == MatchAllDocsQuery.class;
    } else {
        return Sort.INDEXORDER.equals(sf.sort);
    }
}
项目:elasticsearch_my    文件:NestedHelper.java   
/** Returns true if the given query might match nested documents. */
public boolean mightMatchNestedDocs(Query query) {
    if (query instanceof ConstantScoreQuery) {
        return mightMatchNestedDocs(((ConstantScoreQuery) query).getQuery());
    } else if (query instanceof BoostQuery) {
        return mightMatchNestedDocs(((BoostQuery) query).getQuery());
    } else if (query instanceof MatchAllDocsQuery) {
        return true;
    } else if (query instanceof MatchNoDocsQuery) {
        return false;
    } else if (query instanceof TermQuery) {
        // We only handle term queries and range queries, which should already
        // cover a high majority of use-cases
        return mightMatchNestedDocs(((TermQuery) query).getTerm().field());
    } else if (query instanceof PointRangeQuery) {
        return mightMatchNestedDocs(((PointRangeQuery) query).getField());
    } else if (query instanceof IndexOrDocValuesQuery) {
        return mightMatchNestedDocs(((IndexOrDocValuesQuery) query).getIndexQuery());
    } else if (query instanceof BooleanQuery) {
        final BooleanQuery bq = (BooleanQuery) query;
        final boolean hasRequiredClauses = bq.clauses().stream().anyMatch(BooleanClause::isRequired);
        if (hasRequiredClauses) {
            return bq.clauses().stream()
                    .filter(BooleanClause::isRequired)
                    .map(BooleanClause::getQuery)
                    .allMatch(this::mightMatchNestedDocs);
        } else {
            return bq.clauses().stream()
                    .filter(c -> c.getOccur() == Occur.SHOULD)
                    .map(BooleanClause::getQuery)
                    .anyMatch(this::mightMatchNestedDocs);
        }
    } else if (query instanceof ESToParentBlockJoinQuery) {
        return ((ESToParentBlockJoinQuery) query).getPath() != null;
    } else {
        return true;
    }
}
项目:elasticsearch_my    文件:NestedHelper.java   
/** Returns true if the given query might match parent documents or documents
 *  that are nested under a different path. */
public boolean mightMatchNonNestedDocs(Query query, String nestedPath) {
    if (query instanceof ConstantScoreQuery) {
        return mightMatchNonNestedDocs(((ConstantScoreQuery) query).getQuery(), nestedPath);
    } else if (query instanceof BoostQuery) {
        return mightMatchNonNestedDocs(((BoostQuery) query).getQuery(), nestedPath);
    } else if (query instanceof MatchAllDocsQuery) {
        return true;
    } else if (query instanceof MatchNoDocsQuery) {
        return false;
    } else if (query instanceof TermQuery) {
        return mightMatchNonNestedDocs(((TermQuery) query).getTerm().field(), nestedPath);
    } else if (query instanceof PointRangeQuery) {
        return mightMatchNonNestedDocs(((PointRangeQuery) query).getField(), nestedPath);
    } else if (query instanceof IndexOrDocValuesQuery) {
        return mightMatchNonNestedDocs(((IndexOrDocValuesQuery) query).getIndexQuery(), nestedPath);
    } else if (query instanceof BooleanQuery) {
        final BooleanQuery bq = (BooleanQuery) query;
        final boolean hasRequiredClauses = bq.clauses().stream().anyMatch(BooleanClause::isRequired);
        if (hasRequiredClauses) {
            return bq.clauses().stream()
                    .filter(BooleanClause::isRequired)
                    .map(BooleanClause::getQuery)
                    .allMatch(q -> mightMatchNonNestedDocs(q, nestedPath));
        } else {
            return bq.clauses().stream()
                    .filter(c -> c.getOccur() == Occur.SHOULD)
                    .map(BooleanClause::getQuery)
                    .anyMatch(q -> mightMatchNonNestedDocs(q, nestedPath));
        }
    } else {
        return true;
    }
}
项目:elasticsearch_my    文件:MappedFieldType.java   
/** Build a constant-scoring query that matches all values. The default implementation uses a
 * {@link ConstantScoreQuery} around a {@link BooleanQuery} whose {@link Occur#SHOULD} clauses
 * are generated with {@link #termQuery}. */
public Query termsQuery(List<?> values, @Nullable QueryShardContext context) {
    BooleanQuery.Builder builder = new BooleanQuery.Builder();
    for (Object value : values) {
        builder.add(termQuery(value, context), Occur.SHOULD);
    }
    return new ConstantScoreQuery(builder.build());
}
项目:elasticsearch_my    文件:Queries.java   
public static boolean isConstantMatchAllQuery(Query query) {
    if (query instanceof ConstantScoreQuery) {
        return isConstantMatchAllQuery(((ConstantScoreQuery) query).getQuery());
    } else if (query instanceof MatchAllDocsQuery) {
        return true;
    }
    return false;
}
项目:elasticsearch_my    文件:QueryPhaseTests.java   
private void countTestCase(boolean withDeletions) throws Exception {
    Directory dir = newDirectory();
    IndexWriterConfig iwc = newIndexWriterConfig().setMergePolicy(NoMergePolicy.INSTANCE);
    RandomIndexWriter w = new RandomIndexWriter(random(), dir, iwc);
    final int numDocs = scaledRandomIntBetween(100, 200);
    for (int i = 0; i < numDocs; ++i) {
        Document doc = new Document();
        if (randomBoolean()) {
            doc.add(new StringField("foo", "bar", Store.NO));
        }
        if (randomBoolean()) {
            doc.add(new StringField("foo", "baz", Store.NO));
        }
        if (withDeletions && (rarely() || i == 0)) {
            doc.add(new StringField("delete", "yes", Store.NO));
        }
        w.addDocument(doc);
    }
    if (withDeletions) {
        w.deleteDocuments(new Term("delete", "yes"));
    }
    final IndexReader reader = w.getReader();
    Query matchAll = new MatchAllDocsQuery();
    Query matchAllCsq = new ConstantScoreQuery(matchAll);
    Query tq = new TermQuery(new Term("foo", "bar"));
    Query tCsq = new ConstantScoreQuery(tq);
    BooleanQuery bq = new BooleanQuery.Builder()
        .add(matchAll, Occur.SHOULD)
        .add(tq, Occur.MUST)
        .build();

    countTestCase(matchAll, reader, false);
    countTestCase(matchAllCsq, reader, false);
    countTestCase(tq, reader, withDeletions);
    countTestCase(tCsq, reader, withDeletions);
    countTestCase(bq, reader, true);
    reader.close();
    w.close();
    dir.close();
}
项目:elasticsearch_my    文件:HasChildQueryBuilderTests.java   
static void assertLateParsingQuery(Query query, String type, String id) throws IOException {
    assertThat(query, instanceOf(HasChildQueryBuilder.LateParsingQuery.class));
    HasChildQueryBuilder.LateParsingQuery lateParsingQuery = (HasChildQueryBuilder.LateParsingQuery) query;
    assertThat(lateParsingQuery.getInnerQuery(), instanceOf(BooleanQuery.class));
    BooleanQuery booleanQuery = (BooleanQuery) lateParsingQuery.getInnerQuery();
    assertThat(booleanQuery.clauses().size(), equalTo(2));
    //check the inner ids query, we have to call rewrite to get to check the type it's executed against
    assertThat(booleanQuery.clauses().get(0).getOccur(), equalTo(BooleanClause.Occur.MUST));
    assertThat(booleanQuery.clauses().get(0).getQuery(), instanceOf(TermInSetQuery.class));
    TermInSetQuery termsQuery = (TermInSetQuery) booleanQuery.clauses().get(0).getQuery();
    Query rewrittenTermsQuery = termsQuery.rewrite(null);
    assertThat(rewrittenTermsQuery, instanceOf(ConstantScoreQuery.class));
    ConstantScoreQuery constantScoreQuery = (ConstantScoreQuery) rewrittenTermsQuery;
    assertThat(constantScoreQuery.getQuery(), instanceOf(BooleanQuery.class));
    BooleanQuery booleanTermsQuery = (BooleanQuery) constantScoreQuery.getQuery();
    assertThat(booleanTermsQuery.clauses().toString(), booleanTermsQuery.clauses().size(), equalTo(1));
    assertThat(booleanTermsQuery.clauses().get(0).getOccur(), equalTo(BooleanClause.Occur.SHOULD));
    assertThat(booleanTermsQuery.clauses().get(0).getQuery(), instanceOf(TermQuery.class));
    TermQuery termQuery = (TermQuery) booleanTermsQuery.clauses().get(0).getQuery();
    assertThat(termQuery.getTerm().field(), equalTo(UidFieldMapper.NAME));
    //we want to make sure that the inner ids query gets executed against the child type rather than the main type we initially set to the context
    BytesRef[] ids = Uid.createUidsForTypesAndIds(Collections.singletonList(type), Collections.singletonList(id));
    assertThat(termQuery.getTerm().bytes(), equalTo(ids[0]));
    //check the type filter
    assertThat(booleanQuery.clauses().get(1).getOccur(), equalTo(BooleanClause.Occur.FILTER));
    assertEquals(new TypeFieldMapper.TypesQuery(new BytesRef(type)), booleanQuery.clauses().get(1).getQuery());
}
项目:elasticsearch_my    文件:ConstantScoreQueryBuilderTests.java   
@Override
protected void doAssertLuceneQuery(ConstantScoreQueryBuilder queryBuilder, Query query, SearchContext context) throws IOException {
    Query innerQuery = queryBuilder.innerQuery().toQuery(context.getQueryShardContext());
    if (innerQuery == null) {
        assertThat(query, nullValue());
    } else {
        assertThat(query, instanceOf(ConstantScoreQuery.class));
        ConstantScoreQuery constantScoreQuery = (ConstantScoreQuery) query;
        assertThat(constantScoreQuery.getQuery(), instanceOf(innerQuery.getClass()));
    }
}
项目:elasticsearch_my    文件:GeoShapeQueryBuilderTests.java   
@Override
protected void doAssertLuceneQuery(GeoShapeQueryBuilder queryBuilder, Query query, SearchContext context) throws IOException {
    // Logic for doToQuery is complex and is hard to test here. Need to rely
    // on Integration tests to determine if created query is correct
    // TODO improve GeoShapeQueryBuilder.doToQuery() method to make it
    // easier to test here
    assertThat(query, anyOf(instanceOf(BooleanQuery.class), instanceOf(ConstantScoreQuery.class)));
}