Made p/c override the clone() method. This is necessary since by default clone will make a shallow copy of the original object, while for p/c queries we need to make sure that the wrapped queries are also cloned.

This commit is contained in:
Martijn van Groningen 2014-03-17 11:55:39 +07:00
parent a1192044f2
commit c501d9960a
10 changed files with 167 additions and 4 deletions

View File

@ -86,6 +86,18 @@ public class ChildrenConstantScoreQuery extends Query {
rewrittenChildQuery.extractTerms(terms);
}
@Override
public Query clone() {
ChildrenConstantScoreQuery q = new ChildrenConstantScoreQuery(parentChildIndexFieldData, originalChildQuery.clone(),
parentType, childType, parentFilter, shortCircuitParentDocSet, nonNestedDocsFilter);
q.setBoost(getBoost());
if (q.rewrittenChildQuery != null) {
q.rewrittenChildQuery = rewrittenChildQuery.clone();
q.rewriteIndexReader = rewriteIndexReader;
}
return q;
}
@Override
public Weight createWeight(IndexSearcher searcher) throws IOException {
SearchContext searchContext = SearchContext.current();

View File

@ -129,6 +129,18 @@ public class ChildrenQuery extends Query {
return this;
}
@Override
public Query clone() {
ChildrenQuery q = new ChildrenQuery(parentChildIndexFieldData, parentType, childType, parentFilter,
originalChildQuery, scoreType, shortCircuitParentDocSet, nonNestedDocsFilter);
q.setBoost(getBoost());
if (q.rewrittenChildQuery != null) {
q.rewrittenChildQuery = rewrittenChildQuery.clone();
q.rewriteIndexReader = rewriteIndexReader;
}
return q;
}
@Override
public void extractTerms(Set<Term> terms) {
rewrittenChildQuery.extractTerms(terms);

View File

@ -75,6 +75,18 @@ public class ParentConstantScoreQuery extends Query {
rewrittenParentQuery.extractTerms(terms);
}
@Override
public Query clone() {
ParentConstantScoreQuery q = new ParentConstantScoreQuery(parentChildIndexFieldData, originalParentQuery,
parentType, childrenFilter);
q.setBoost(getBoost());
if (q.rewrittenParentQuery != null) {
q.rewrittenParentQuery = rewrittenParentQuery.clone();
q.rewriteIndexReader = rewriteIndexReader;
}
return q;
}
@Override
public Weight createWeight(IndexSearcher searcher) throws IOException {
SearchContext searchContext = SearchContext.current();

View File

@ -120,6 +120,17 @@ public class ParentQuery extends Query {
rewrittenParentQuery.extractTerms(terms);
}
@Override
public Query clone() {
ParentQuery q = new ParentQuery(parentChildIndexFieldData, originalParentQuery, parentType, childrenFilter);
q.setBoost(getBoost());
if (q.rewrittenParentQuery != null) {
q.rewrittenParentQuery = rewrittenParentQuery.clone();
q.rewriteIndexReader = rewriteIndexReader;
}
return q;
}
@Override
public Weight createWeight(IndexSearcher searcher) throws IOException {
SearchContext searchContext = SearchContext.current();

View File

@ -106,6 +106,20 @@ public class TopChildrenQuery extends Query {
return this;
}
@Override
public Query clone() {
TopChildrenQuery q = new TopChildrenQuery(
parentChildIndexFieldData, originalChildQuery.clone(), childType, parentType,
scoreType, factor, incrementalFactor, cacheRecycler, nonNestedDocsFilter
);
q.setBoost(getBoost());
if (q.rewrittenChildQuery != null) {
q.rewrittenChildQuery = rewrittenChildQuery.clone();
q.rewriteIndexReader = rewriteIndexReader;
}
return q;
}
@Override
public void extractTerms(Set<Term> terms) {
rewrittenChildQuery.extractTerms(terms);

View File

@ -85,6 +85,16 @@ public class ChildrenConstantScoreQueryTests extends ElasticsearchLuceneTestCase
SearchContext.removeCurrent();
}
@Test
public void testBasicQuerySanities() {
Query childQuery = new TermQuery(new Term("field", "value"));
ParentFieldMapper parentFieldMapper = SearchContext.current().mapperService().documentMapper("child").parentFieldMapper();
ParentChildIndexFieldData parentChildIndexFieldData = SearchContext.current().fieldData().getForField(parentFieldMapper);
Filter parentFilter = new TermFilter(new Term(TypeFieldMapper.NAME, "parent"));
Query query = new ChildrenConstantScoreQuery(parentChildIndexFieldData, childQuery, "parent", "child", parentFilter, 12, NonNestedDocsFilter.INSTANCE);
QueryUtils.check(query);
}
@Test
public void testSimple() throws Exception {
Directory directory = newDirectory();

View File

@ -67,6 +67,17 @@ public class ChildrenQueryTests extends ElasticsearchLuceneTestCase {
SearchContext.removeCurrent();
}
@Test
public void testBasicQuerySanities() {
Query childQuery = new TermQuery(new Term("field", "value"));
ScoreType scoreType = ScoreType.values()[random().nextInt(ScoreType.values().length)];
ParentFieldMapper parentFieldMapper = SearchContext.current().mapperService().documentMapper("child").parentFieldMapper();
ParentChildIndexFieldData parentChildIndexFieldData = SearchContext.current().fieldData().getForField(parentFieldMapper);
Filter parentFilter = new TermFilter(new Term(TypeFieldMapper.NAME, "parent"));
Query query = new ChildrenQuery(parentChildIndexFieldData, "parent", "child", parentFilter, childQuery, scoreType, 12, NonNestedDocsFilter.INSTANCE);
QueryUtils.check(query);
}
@Test
public void testRandom() throws Exception {
Directory directory = newDirectory();

View File

@ -25,10 +25,7 @@ import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.index.*;
import org.apache.lucene.queries.TermFilter;
import org.apache.lucene.search.Filter;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.*;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.FixedBitSet;
import org.elasticsearch.common.lucene.search.NotFilter;
@ -69,6 +66,16 @@ public class ParentConstantScoreQueryTests extends ElasticsearchLuceneTestCase {
SearchContext.removeCurrent();
}
@Test
public void testBasicQuerySanities() {
Query parentQuery = new TermQuery(new Term("field", "value"));
ParentFieldMapper parentFieldMapper = SearchContext.current().mapperService().documentMapper("child").parentFieldMapper();
ParentChildIndexFieldData parentChildIndexFieldData = SearchContext.current().fieldData().getForField(parentFieldMapper);
Filter childrenFilter = new TermFilter(new Term(TypeFieldMapper.NAME, "child"));
Query query = new ParentConstantScoreQuery(parentChildIndexFieldData, parentQuery, "parent", childrenFilter);
QueryUtils.check(query);
}
@Test
public void testRandom() throws Exception {
Directory directory = newDirectory();

View File

@ -66,6 +66,16 @@ public class ParentQueryTests extends ElasticsearchLuceneTestCase {
SearchContext.removeCurrent();
}
@Test
public void testBasicQuerySanities() {
Query parentQuery = new TermQuery(new Term("field", "value"));
ParentFieldMapper parentFieldMapper = SearchContext.current().mapperService().documentMapper("child").parentFieldMapper();
ParentChildIndexFieldData parentChildIndexFieldData = SearchContext.current().fieldData().getForField(parentFieldMapper);
Filter childrenFilter = new TermFilter(new Term(TypeFieldMapper.NAME, "child"));
Query query = new ParentQuery(parentChildIndexFieldData, parentQuery, "parent", childrenFilter);
QueryUtils.check(query);
}
@Test
public void testRandom() throws Exception {
Directory directory = newDirectory();

View File

@ -0,0 +1,64 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.index.search.child;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.QueryUtils;
import org.apache.lucene.search.TermQuery;
import org.elasticsearch.index.fielddata.plain.ParentChildIndexFieldData;
import org.elasticsearch.index.mapper.internal.ParentFieldMapper;
import org.elasticsearch.index.search.nested.NonNestedDocsFilter;
import org.elasticsearch.search.internal.SearchContext;
import org.elasticsearch.test.ElasticsearchLuceneTestCase;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import java.io.IOException;
import static org.elasticsearch.index.search.child.ChildrenConstantScoreQueryTests.createSearchContext;
/**
*/
public class TopChildrenQueryTests extends ElasticsearchLuceneTestCase {
@BeforeClass
public static void before() throws IOException {
forceDefaultCodec();
SearchContext.setCurrent(createSearchContext("test", "parent", "child"));
}
@AfterClass
public static void after() throws IOException {
SearchContext.removeCurrent();
}
@Test
public void testBasicQuerySanities() {
Query childQuery = new TermQuery(new Term("field", "value"));
ScoreType scoreType = ScoreType.values()[random().nextInt(ScoreType.values().length)];
ParentFieldMapper parentFieldMapper = SearchContext.current().mapperService().documentMapper("child").parentFieldMapper();
ParentChildIndexFieldData parentChildIndexFieldData = SearchContext.current().fieldData().getForField(parentFieldMapper);
Query query = new TopChildrenQuery(parentChildIndexFieldData, childQuery, "child", "parent", scoreType, 1, 1, SearchContext.current().cacheRecycler(), NonNestedDocsFilter.INSTANCE);
QueryUtils.check(query);
}
}