Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup non nested filter to not flip the FixedBitSet returned by the wrapped filter. #8232

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -30,16 +30,19 @@

import java.io.IOException;

/**
* Filter that returns all nested documents.
* A nested document is a sub documents that belong to a root document.
* Nested documents share the unique id and type and optionally the _source with root documents.
*/
public class NestedDocsFilter extends Filter {

public static final NestedDocsFilter INSTANCE = new NestedDocsFilter();

private final PrefixFilter filter = new PrefixFilter(new Term(TypeFieldMapper.NAME, new BytesRef("__")));

private final Filter filter = nestedFilter();
private final int hashCode = filter.hashCode();

private NestedDocsFilter() {

}

@Override
Expand All @@ -56,4 +59,9 @@ public int hashCode() {
public boolean equals(Object obj) {
return obj == INSTANCE;
}

static Filter nestedFilter() {
return new PrefixFilter(new Term(TypeFieldMapper.NAME, new BytesRef("__")));
}

}
Expand Up @@ -20,40 +20,30 @@
package org.elasticsearch.index.search.nested;

import org.apache.lucene.index.AtomicReaderContext;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.DocIdSet;
import org.apache.lucene.search.Filter;
import org.apache.lucene.search.PrefixFilter;
import org.apache.lucene.util.Bits;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.FixedBitSet;
import org.elasticsearch.common.lucene.docset.DocIdSets;
import org.elasticsearch.index.mapper.internal.TypeFieldMapper;
import org.elasticsearch.common.lucene.search.NotFilter;

import java.io.IOException;

/**
* A filter that returns all root (non nested) documents.
* Root documents have an unique id, a type and optionally have a _source and other indexed and stored fields.
*/
public class NonNestedDocsFilter extends Filter {

public static final NonNestedDocsFilter INSTANCE = new NonNestedDocsFilter();

private final PrefixFilter filter = new PrefixFilter(new Term(TypeFieldMapper.NAME, new BytesRef("__")));

private final Filter filter = new NotFilter(NestedDocsFilter.nestedFilter());
private final int hashCode = filter.hashCode();

private NonNestedDocsFilter() {

}

@Override
public DocIdSet getDocIdSet(AtomicReaderContext context, Bits acceptDocs) throws IOException {
DocIdSet docSet = filter.getDocIdSet(context, acceptDocs);
if (DocIdSets.isEmpty(docSet)) {
// will almost never happen, and we need an OpenBitSet for the parent filter in
// BlockJoinQuery, we cache it anyhow...
docSet = new FixedBitSet(context.reader().maxDoc());
}
((FixedBitSet) docSet).flip(0, context.reader().maxDoc());
return docSet;
return filter.getDocIdSet(context, acceptDocs);
}

@Override
Expand Down