org.apache.lucene.search.join.BitSetProducer Java Examples

The following examples show how to use org.apache.lucene.search.join.BitSetProducer. 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 File: InnerHitsContext.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public TopDocs topDocs(SearchContext context, FetchSubPhase.HitContext hitContext) throws IOException {
    Query rawParentFilter;
    if (parentObjectMapper == null) {
        rawParentFilter = Queries.newNonNestedFilter();
    } else {
        rawParentFilter = parentObjectMapper.nestedTypeFilter();
    }
    BitSetProducer parentFilter = context.bitsetFilterCache().getBitSetProducer(rawParentFilter);
    Query childFilter = childObjectMapper.nestedTypeFilter();
    Query q = Queries.filtered(query.query(), new NestedChildrenQuery(parentFilter, childFilter, hitContext));

    if (size() == 0) {
        return new TopDocs(context.searcher().count(q), Lucene.EMPTY_SCORE_DOCS, 0);
    } else {
        int topN = Math.min(from() + size(), context.searcher().getIndexReader().maxDoc());
        TopDocsCollector topDocsCollector;
        if (sort() != null) {
            try {
                topDocsCollector = TopFieldCollector.create(sort(), topN, true, trackScores(), trackScores());
            } catch (IOException e) {
                throw ExceptionsHelper.convertToElastic(e);
            }
        } else {
            topDocsCollector = TopScoreDocCollector.create(topN);
        }
        try {
            context.searcher().search(q, topDocsCollector);
        } finally {
            clearReleasables(Lifetime.COLLECTION);
        }
        return topDocsCollector.topDocs(from(), size());
    }
}
 
Example #2
Source File: BlockJoinParentQParser.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public static BitSetProducer getCachedBitSetProducer(final SolrQueryRequest request, Query query) {
  @SuppressWarnings("unchecked")
  SolrCache<Query, BitSetProducer> parentCache = request.getSearcher().getCache(CACHE_NAME);
  // lazily retrieve from solr cache
  if (parentCache != null) {
    return parentCache.computeIfAbsent(query, QueryBitSetProducer::new);
  } else {
    return new QueryBitSetProducer(query);
  }
}
 
Example #3
Source File: Engine.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public DeleteByQuery(Query query, BytesReference source, @Nullable String[] filteringAliases, @Nullable Query aliasFilter, BitSetProducer parentFilter, Operation.Origin origin, long startTime, String... types) {
    this.query = query;
    this.source = source;
    this.types = types;
    this.filteringAliases = filteringAliases;
    this.aliasFilter = aliasFilter;
    this.parentFilter = parentFilter;
    this.startTime = startTime;
    this.origin = origin;
}
 
Example #4
Source File: ChildFieldValueSourceParser.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public ValueSource parse(FunctionQParser fp) throws SyntaxError {
  
  final String sortFieldName = fp.parseArg();
  final Query query;
  if (fp.hasMoreArguments()){
    query = fp.parseNestedQuery();
  } else {
    query = fp.subQuery(fp.getParam(CommonParams.Q), null).getQuery();
  }
  
  BitSetProducer parentFilter;
  BitSetProducer childFilter;
  SchemaField sf;
  try {
    AllParentsAware bjQ;
    if (!(query instanceof AllParentsAware)) {
      throw new SyntaxError("expect a reference to block join query "+
            AllParentsAware.class.getSimpleName()+" in "+fp.getString());
    }
    bjQ = (AllParentsAware) query;
    
    parentFilter = BlockJoinParentQParser.getCachedBitSetProducer(fp.getReq(), bjQ.getParentQuery());
    childFilter = BlockJoinParentQParser.getCachedBitSetProducer(fp.getReq(), bjQ.getChildQuery());

    if (sortFieldName==null || sortFieldName.equals("")) {
      throw new SyntaxError ("field is omitted in "+fp.getString());
    }
    
    sf = fp.getReq().getSchema().getFieldOrNull(sortFieldName);
    if (null == sf) {
      throw new SyntaxError
        (NAME+" sort param field \""+ sortFieldName+"\" can't be found in schema");
    }
  } catch (SyntaxError e) {
    log.error("can't parse {}", fp.getString(), e);
    throw e;
  }
  return new BlockJoinSortFieldValueSource(childFilter, parentFilter, sf);
}
 
Example #5
Source File: ChildrenQuery.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public ChildrenQuery(ParentChildIndexFieldData ifd, String parentType, String childType, Filter parentFilter, Query childQuery, ScoreType scoreType, int minChildren, int maxChildren, int shortCircuitParentDocSet, BitSetProducer nonNestedDocsFilter) {
    this.ifd = ifd;
    this.parentType = parentType;
    this.childType = childType;
    this.parentFilter = parentFilter;
    this.childQuery = childQuery;
    this.scoreType = scoreType;
    this.shortCircuitParentDocSet = shortCircuitParentDocSet;
    this.nonNestedDocsFilter = nonNestedDocsFilter;
    assert maxChildren == 0 || minChildren <= maxChildren;
    this.minChildren = minChildren > 1 ? minChildren : 0;
    this.maxChildren = maxChildren;
}
 
Example #6
Source File: ChildrenConstantScoreQuery.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public ChildrenConstantScoreQuery(IndexParentChildFieldData parentChildIndexFieldData, Query childQuery, String parentType, String childType, Filter parentFilter, int shortCircuitParentDocSet, BitSetProducer nonNestedDocsFilter) {
    this.parentChildIndexFieldData = parentChildIndexFieldData;
    this.parentFilter = parentFilter;
    this.parentType = parentType;
    this.childType = childType;
    this.childQuery = childQuery;
    this.shortCircuitParentDocSet = shortCircuitParentDocSet;
    this.nonNestedDocsFilter = nonNestedDocsFilter;
}
 
Example #7
Source File: ChildDocTransformer.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
ChildDocTransformer(String name, BitSetProducer parentsFilter, DocSet childDocSet,
                    SolrReturnFields returnFields, boolean isNestedSchema, int limit) {
  this.name = name;
  this.parentsFilter = parentsFilter;
  this.childDocSet = childDocSet;
  this.limit = limit;
  this.isNestedSchema = isNestedSchema;
  this.childReturnFields = returnFields!=null? returnFields: new SolrReturnFields();
}
 
Example #8
Source File: TranslogRecoveryPerformer.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private static Engine.DeleteByQuery prepareDeleteByQuery(IndexQueryParserService queryParserService, MapperService mapperService, IndexAliasesService indexAliasesService, IndexCache indexCache, BytesReference source, @Nullable String[] filteringAliases, Engine.Operation.Origin origin, String... types) {
    long startTime = System.nanoTime();
    if (types == null) {
        types = Strings.EMPTY_ARRAY;
    }
    Query query;
    try {
        query = queryParserService.parseQuery(source).query();
    } catch (QueryParsingException ex) {
        // for BWC we try to parse directly the query since pre 1.0.0.Beta2 we didn't require a top level query field
        if (queryParserService.getIndexCreatedVersion().onOrBefore(Version.V_1_0_0_Beta2)) {
            try {
                XContentParser parser = XContentHelper.createParser(source);
                ParsedQuery parse = queryParserService.parse(parser);
                query = parse.query();
            } catch (Throwable t) {
                ex.addSuppressed(t);
                throw ex;
            }
        } else {
            throw ex;
        }
    }
    Query searchFilter = mapperService.searchFilter(types);
    if (searchFilter != null) {
        query = Queries.filtered(query, searchFilter);
    }

    Query aliasFilter = indexAliasesService.aliasFilter(filteringAliases);
    BitSetProducer parentFilter = mapperService.hasNested() ? indexCache.bitsetFilterCache().getBitSetProducer(Queries.newNonNestedFilter()) : null;
    return new Engine.DeleteByQuery(query, source, filteringAliases, aliasFilter, parentFilter, origin, startTime, types);
}
 
Example #9
Source File: BlockJoinParentQParser.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public BitSetProducerQuery(BitSetProducer bitSetProducer) {
  this.bitSetProducer = bitSetProducer;
  setCache(false); // because we assume the bitSetProducer is itself cached
}
 
Example #10
Source File: DlsQueryParser.java    From deprecated-security-advanced-modules with Apache License 2.0 4 votes vote down vote up
private static void handleNested(final QueryShardContext queryShardContext,
        final BooleanQuery.Builder dlsQueryBuilder,
        final Query parentQuery) {
    final BitSetProducer parentDocumentsFilter = queryShardContext.bitsetFilter(NON_NESTED_QUERY);
    dlsQueryBuilder.add(new ToChildBlockJoinQuery(parentQuery, parentDocumentsFilter), Occur.SHOULD);
}
 
Example #11
Source File: BlockJoinParentQParser.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private AllParentsAware(Query childQuery, BitSetProducer parentsFilter, ScoreMode scoreMode,
    Query parentList) {
  super(childQuery, parentsFilter, scoreMode);
  parentQuery = parentList;
}
 
Example #12
Source File: BlockJoinParentQParser.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
BitSetProducer getBitSetProducer(Query query) {
  return getCachedBitSetProducer(req, query);
}
 
Example #13
Source File: ChildFieldValueSourceParser.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private String query(BitSetProducer bits) {
  return (bits instanceof QueryBitSetProducer) ? ((QueryBitSetProducer) bits).getQuery().toString()
      : bits.toString();
}
 
Example #14
Source File: ChildFieldValueSourceParser.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private BlockJoinSortFieldValueSource(BitSetProducer childFilter, BitSetProducer parentFilter,
    SchemaField childField) {
  this.childFilter = childFilter;
  this.parentFilter = parentFilter;
  this.childField = childField;
}
 
Example #15
Source File: TestHierarchicalDocBuilder.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private BitSetProducer createParentFilter(String type) {
  BooleanQuery.Builder parentQuery = new BooleanQuery.Builder();
  parentQuery.add(new TermQuery(new Term("type_s", type)), Occur.MUST);
  return new QueryBitSetProducer(parentQuery.build());
}
 
Example #16
Source File: BitsetFilterCache.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public BitSetProducer getBitSetProducer(Query query) {
    return new QueryWrapperBitSetProducer(query);
}
 
Example #17
Source File: ParentIdsFilter.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
private ParentIdsFilter(String parentType, BitSetProducer nonNestedDocsFilter, BytesRefHash parentIds) {
    this.nonNestedDocsFilter = nonNestedDocsFilter;
    this.parentTypeBr = new BytesRef(parentType);
    this.parentIds = parentIds;
}
 
Example #18
Source File: IncludeNestedDocsQuery.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
IncludeNestedDocsWeight(Query query, Query parentQuery, Weight parentWeight, BitSetProducer parentsFilter) {
    super(query);
    this.parentQuery = parentQuery;
    this.parentWeight = parentWeight;
    this.parentsFilter = parentsFilter;
}
 
Example #19
Source File: IncludeNestedDocsQuery.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public IncludeNestedDocsQuery(Query parentQuery, BitSetProducer parentFilter) {
    this.origParentQuery = parentQuery;
    this.parentQuery = parentQuery;
    this.parentFilter = parentFilter;
}
 
Example #20
Source File: Engine.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public BitSetProducer parentFilter() {
    return parentFilter;
}
 
Example #21
Source File: IndexFieldData.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public Nested(BitSetProducer rootFilter, Weight innerFilter) {
    this.rootFilter = rootFilter;
    this.innerFilter = innerFilter;
}
 
Example #22
Source File: QueryParseContext.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public BitSetProducer bitsetFilter(Query filter) {
    return indexQueryParser.bitsetFilterCache.getBitSetProducer(filter);
}
 
Example #23
Source File: SortParseElement.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
private void addSortField(SearchContext context, List<SortField> sortFields, String fieldName, boolean reverse, String unmappedType, @Nullable final String missing, MultiValueMode sortMode, NestedInnerQueryParseSupport nestedHelper) throws IOException {
    if (SCORE_FIELD_NAME.equals(fieldName)) {
        if (reverse) {
            sortFields.add(SORT_SCORE_REVERSE);
        } else {
            sortFields.add(SORT_SCORE);
        }
    } else if (DOC_FIELD_NAME.equals(fieldName)) {
        if (reverse) {
            sortFields.add(SORT_DOC_REVERSE);
        } else {
            sortFields.add(SORT_DOC);
        }
    } else {
        MappedFieldType fieldType = context.smartNameFieldType(fieldName);
        if (fieldType == null) {
            if (unmappedType != null) {
                fieldType = context.mapperService().unmappedFieldType(unmappedType);
            } else {
                throw new SearchParseException(context, "No mapping found for [" + fieldName + "] in order to sort on", null);
            }
        }

        if (!fieldType.isSortable()) {
            throw new SearchParseException(context, "Sorting not supported for field[" + fieldName + "]", null);
        }

        // Enable when we also know how to detect fields that do tokenize, but only emit one token
        /*if (fieldMapper instanceof StringFieldMapper) {
            StringFieldMapper stringFieldMapper = (StringFieldMapper) fieldMapper;
            if (stringFieldMapper.fieldType().tokenized()) {
                // Fail early
                throw new SearchParseException(context, "Can't sort on tokenized string field[" + fieldName + "]");
            }
        }*/

        // We only support AVG and SUM on number based fields
        if (fieldType.isNumeric() == false && (sortMode == MultiValueMode.SUM || sortMode == MultiValueMode.AVG)) {
            sortMode = null;
        }
        if (sortMode == null) {
            sortMode = resolveDefaultSortMode(reverse);
        }

        final Nested nested;
        if (nestedHelper != null && nestedHelper.getPath() != null) {
            BitSetProducer rootDocumentsFilter = context.bitsetFilterCache().getBitSetProducer(Queries.newNonNestedFilter());
            Query innerDocumentsFilter;
            if (nestedHelper.filterFound()) {
                // TODO: use queries instead
                innerDocumentsFilter = nestedHelper.getInnerFilter();
            } else {
                innerDocumentsFilter = nestedHelper.getNestedObjectMapper().nestedTypeFilter();
            }
            nested = new Nested(rootDocumentsFilter,  context.searcher().createNormalizedWeight(innerDocumentsFilter, false));
        } else {
            nested = null;
        }

        IndexFieldData.XFieldComparatorSource fieldComparatorSource = context.fieldData().getForField(fieldType)
                .comparatorSource(missing, sortMode, nested);
        sortFields.add(new SortField(fieldType.names().indexName(), fieldComparatorSource, reverse));
    }
}
 
Example #24
Source File: InnerHitsContext.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
NestedChildrenQuery(BitSetProducer parentFilter, Query childFilter, FetchSubPhase.HitContext hitContext) {
    this.parentFilter = parentFilter;
    this.childFilter = childFilter;
    this.docId = hitContext.docId();
    this.leafReader = hitContext.readerContext().reader();
}