Move applying alias filter to ContextSearch#preProcess()

This commit is contained in:
Igor Motov 2013-03-27 09:23:54 -04:00
parent 17f83f33bb
commit 5bb75f9da3
4 changed files with 40 additions and 41 deletions

View File

@ -178,9 +178,9 @@ public class FacetPhase implements SearchPhase {
for (Map.Entry<Filter, List<Collector>> entry : filtersByCollector.entrySet()) { for (Map.Entry<Filter, List<Collector>> entry : filtersByCollector.entrySet()) {
Filter filter = entry.getKey(); Filter filter = entry.getKey();
Query query = new XConstantScoreQuery(filter); Query query = new XConstantScoreQuery(filter);
Filter searchFilter = context.mapperService().searchFilter(context.types()); Filter searchFilter = context.searchFilter(context.types());
if (searchFilter != null) { if (searchFilter != null) {
query = new XFilteredQuery(query, context.filterCache().cache(searchFilter)); query = new XFilteredQuery(query, searchFilter);
} }
try { try {
context.searcher().search(query, MultiCollector.wrap(entry.getValue().toArray(new Collector[entry.getValue().size()]))); context.searcher().search(query, MultiCollector.wrap(entry.getValue().toArray(new Collector[entry.getValue().size()])));

View File

@ -19,12 +19,10 @@
package org.elasticsearch.search.internal; package org.elasticsearch.search.internal;
import com.google.common.collect.ImmutableList;
import org.apache.lucene.index.AtomicReaderContext; import org.apache.lucene.index.AtomicReaderContext;
import org.apache.lucene.search.*; import org.apache.lucene.search.*;
import org.elasticsearch.common.lucene.MinimumScoreCollector; import org.elasticsearch.common.lucene.MinimumScoreCollector;
import org.elasticsearch.common.lucene.MultiCollector; import org.elasticsearch.common.lucene.MultiCollector;
import org.elasticsearch.common.lucene.search.AndFilter;
import org.elasticsearch.common.lucene.search.FilteredCollector; import org.elasticsearch.common.lucene.search.FilteredCollector;
import org.elasticsearch.common.lucene.search.XCollector; import org.elasticsearch.common.lucene.search.XCollector;
import org.elasticsearch.common.lucene.search.XFilteredQuery; import org.elasticsearch.common.lucene.search.XFilteredQuery;
@ -126,40 +124,6 @@ public class ContextIndexSearcher extends IndexSearcher {
return super.createNormalizedWeight(query); return super.createNormalizedWeight(query);
} }
private Filter combinedFilter(Filter filter) {
Filter combinedFilter;
if (filter == null) {
combinedFilter = searchContext.aliasFilter();
} else {
if (searchContext.aliasFilter() != null) {
combinedFilter = new AndFilter(ImmutableList.of(filter, searchContext.aliasFilter()));
} else {
combinedFilter = filter;
}
}
return combinedFilter;
}
@Override
public void search(Query query, Collector results) throws IOException {
Filter filter = combinedFilter(null);
if (filter != null) {
super.search(wrapFilter(query, filter), results);
} else {
super.search(query, results);
}
}
@Override
public TopDocs search(Query query, Filter filter, int n) throws IOException {
return super.search(query, combinedFilter(filter), n);
}
@Override
public TopFieldDocs search(Query query, Filter filter, int n, Sort sort, boolean doDocScores, boolean doMaxScore) throws IOException {
return super.search(query, combinedFilter(filter), n, sort, doDocScores, doMaxScore);
}
@Override @Override
public void search(List<AtomicReaderContext> leaves, Weight weight, Collector collector) throws IOException { public void search(List<AtomicReaderContext> leaves, Weight weight, Collector collector) throws IOException {
if (searchContext.timeoutInMillis() != -1) { if (searchContext.timeoutInMillis() != -1) {

View File

@ -28,6 +28,7 @@ import org.elasticsearch.ElasticSearchException;
import org.elasticsearch.action.search.SearchType; import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.lease.Releasable; import org.elasticsearch.common.lease.Releasable;
import org.elasticsearch.common.lucene.search.AndFilter;
import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.common.lucene.search.Queries;
import org.elasticsearch.common.lucene.search.XConstantScoreQuery; import org.elasticsearch.common.lucene.search.XConstantScoreQuery;
import org.elasticsearch.common.lucene.search.XFilteredQuery; import org.elasticsearch.common.lucene.search.XFilteredQuery;
@ -228,18 +229,32 @@ public class SearchContext implements Releasable {
if (queryBoost() != 1.0f) { if (queryBoost() != 1.0f) {
parsedQuery(new ParsedQuery(new FunctionScoreQuery(query(), new BoostScoreFunction(queryBoost)), parsedQuery())); parsedQuery(new ParsedQuery(new FunctionScoreQuery(query(), new BoostScoreFunction(queryBoost)), parsedQuery()));
} }
Filter searchFilter = mapperService().searchFilter(types()); Filter searchFilter = searchFilter(types());
if (searchFilter != null) { if (searchFilter != null) {
if (Queries.isConstantMatchAllQuery(query())) { if (Queries.isConstantMatchAllQuery(query())) {
Query q = new XConstantScoreQuery(filterCache().cache(searchFilter)); Query q = new XConstantScoreQuery(searchFilter);
q.setBoost(query().getBoost()); q.setBoost(query().getBoost());
parsedQuery(new ParsedQuery(q, parsedQuery())); parsedQuery(new ParsedQuery(q, parsedQuery()));
} else { } else {
parsedQuery(new ParsedQuery(new XFilteredQuery(query(), filterCache().cache(searchFilter)), parsedQuery())); parsedQuery(new ParsedQuery(new XFilteredQuery(query(), searchFilter), parsedQuery()));
} }
} }
} }
public Filter searchFilter(String[] types) {
Filter filter = mapperService().searchFilter(types);
if (filter == null) {
return aliasFilter;
} else {
filter = filterCache().cache(filter);
if (aliasFilter != null) {
return new AndFilter(ImmutableList.of(filter, aliasFilter));
}
return filter;
}
}
public long id() { public long id() {
return this.id; return this.id;
} }

View File

@ -38,6 +38,8 @@ import org.elasticsearch.indices.IndexMissingException;
import org.elasticsearch.node.internal.InternalNode; import org.elasticsearch.node.internal.InternalNode;
import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits; import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.facet.FacetBuilders;
import org.elasticsearch.search.facet.terms.TermsFacet;
import org.elasticsearch.search.sort.SortOrder; import org.elasticsearch.search.sort.SortOrder;
import org.elasticsearch.test.integration.AbstractNodesTests; import org.elasticsearch.test.integration.AbstractNodesTests;
import org.testng.annotations.AfterClass; import org.testng.annotations.AfterClass;
@ -234,6 +236,24 @@ public class IndexAliasesTests extends AbstractNodesTests {
searchResponse = client1.prepareSearch("tests").setQuery(QueryBuilders.matchAllQuery()).addSort("_uid", SortOrder.ASC).execute().actionGet(); searchResponse = client1.prepareSearch("tests").setQuery(QueryBuilders.matchAllQuery()).addSort("_uid", SortOrder.ASC).execute().actionGet();
assertHits(searchResponse.getHits(), "1", "2", "3"); assertHits(searchResponse.getHits(), "1", "2", "3");
logger.info("--> checking single filtering alias search with global facets");
searchResponse = client1.prepareSearch("tests").setQuery(QueryBuilders.matchQuery("name", "bar"))
.addFacet(FacetBuilders.termsFacet("test").field("name").global(true))
.execute().actionGet();
assertThat(((TermsFacet) searchResponse.getFacets().facet("test")).getEntries().size(), equalTo(4));
logger.info("--> checking single filtering alias search with global facets and sort");
searchResponse = client1.prepareSearch("tests").setQuery(QueryBuilders.matchQuery("name", "bar"))
.addFacet(FacetBuilders.termsFacet("test").field("name").global(true))
.addSort("_uid", SortOrder.ASC).execute().actionGet();
assertThat(((TermsFacet) searchResponse.getFacets().facet("test")).getEntries().size(), equalTo(4));
logger.info("--> checking single filtering alias search with non-global facets");
searchResponse = client1.prepareSearch("tests").setQuery(QueryBuilders.matchQuery("name", "bar"))
.addFacet(FacetBuilders.termsFacet("test").field("name").global(false))
.addSort("_uid", SortOrder.ASC).execute().actionGet();
assertThat(((TermsFacet) searchResponse.getFacets().facet("test")).getEntries().size(), equalTo(2));
searchResponse = client1.prepareSearch("foos", "bars").setQuery(QueryBuilders.matchAllQuery()).execute().actionGet(); searchResponse = client1.prepareSearch("foos", "bars").setQuery(QueryBuilders.matchAllQuery()).execute().actionGet();
assertHits(searchResponse.getHits(), "1", "2"); assertHits(searchResponse.getHits(), "1", "2");