Skip to content

Commit

Permalink
Core: Cleanup non nested filter to not flip the bits in the FixedBitS…
Browse files Browse the repository at this point in the history
…et returned by the wrapped filter.

If the filter producing the FBS were to be cached then it would flip bits each time the NonNestedDocsFilter was executed.
In our case we cache the NonNestedDocsFilter itself so that didn't happen each time this filter was used.
However the hashcode of NonNestedDocsFilter and NestedDocsFilter was the same, since NonNestedDocsFilter directly used NestedDocsFilter#hashCode()

Closes #8227
Closes #8232
  • Loading branch information
martijnvg committed Oct 27, 2014
1 parent eef2fc1 commit 6eb9550
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 20 deletions.
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

0 comments on commit 6eb9550

Please sign in to comment.