Skip to content

Commit

Permalink
Use ConstantScoreQuery as root query of ValueEncoding provided queries.
Browse files Browse the repository at this point in the history
Since we do not use scores for query result evaluation, switching to constant score queries
to avoid useless evaluations.
  • Loading branch information
MishaDemianenko committed Feb 3, 2016
1 parent 9f775de commit abfddb9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
Expand Up @@ -23,6 +23,7 @@
import org.apache.lucene.document.Field; import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField; import org.apache.lucene.document.StringField;
import org.apache.lucene.index.Term; import org.apache.lucene.index.Term;
import org.apache.lucene.search.ConstantScoreQuery;
import org.apache.lucene.search.NumericRangeQuery; import org.apache.lucene.search.NumericRangeQuery;
import org.apache.lucene.search.Query; import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery; import org.apache.lucene.search.TermQuery;
Expand Down Expand Up @@ -64,10 +65,11 @@ void setFieldValue( Object value, Field field )
} }


@Override @Override
NumericRangeQuery<Double> encodeQuery( Object value ) Query encodeQuery( Object value )
{ {
Double doubleValue = ((Number) value).doubleValue(); Double doubleValue = ((Number) value).doubleValue();
return NumericRangeQuery.newDoubleRange( key(), doubleValue, doubleValue, true, true ); return new ConstantScoreQuery( NumericRangeQuery.newDoubleRange( key(), doubleValue, doubleValue,
true, true ) );
} }
}, },
Array Array
Expand Down Expand Up @@ -97,9 +99,9 @@ void setFieldValue( Object value, Field field )
} }


@Override @Override
TermQuery encodeQuery( Object value ) Query encodeQuery( Object value )
{ {
return new TermQuery( new Term( key(), ArrayEncoder.encode( value ) ) ); return new ConstantScoreQuery( new TermQuery( new Term( key(), ArrayEncoder.encode( value ) ) ) );
} }
}, },
Bool Bool
Expand Down Expand Up @@ -129,9 +131,9 @@ void setFieldValue( Object value, Field field )
} }


@Override @Override
TermQuery encodeQuery( Object value ) Query encodeQuery( Object value )
{ {
return new TermQuery( new Term( key(), value.toString() ) ); return new ConstantScoreQuery( new TermQuery( new Term( key(), value.toString() ) ) );
} }
}, },
String String
Expand Down Expand Up @@ -162,9 +164,9 @@ void setFieldValue( Object value, Field field )
} }


@Override @Override
TermQuery encodeQuery( Object value ) Query encodeQuery( Object value )
{ {
return new TermQuery( new Term( key(), value.toString() ) ); return new ConstantScoreQuery( new TermQuery( new Term( key(), value.toString() ) ) );
} }
}; };


Expand Down
Expand Up @@ -22,6 +22,7 @@
import org.apache.commons.lang3.RandomStringUtils; import org.apache.commons.lang3.RandomStringUtils;
import org.apache.lucene.document.Document; import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.search.ConstantScoreQuery;
import org.apache.lucene.search.NumericRangeQuery; import org.apache.lucene.search.NumericRangeQuery;
import org.apache.lucene.search.PrefixQuery; import org.apache.lucene.search.PrefixQuery;
import org.apache.lucene.search.TermQuery; import org.apache.lucene.search.TermQuery;
Expand All @@ -32,6 +33,7 @@
import static junit.framework.Assert.assertNull; import static junit.framework.Assert.assertNull;
import static junit.framework.TestCase.assertEquals; import static junit.framework.TestCase.assertEquals;
import static org.neo4j.kernel.api.impl.schema.LuceneDocumentStructure.NODE_ID_KEY; import static org.neo4j.kernel.api.impl.schema.LuceneDocumentStructure.NODE_ID_KEY;
import static org.neo4j.kernel.api.impl.schema.LuceneDocumentStructure.newSeekQuery;
import static org.neo4j.kernel.api.impl.schema.ValueEncoding.Array; import static org.neo4j.kernel.api.impl.schema.ValueEncoding.Array;
import static org.neo4j.kernel.api.impl.schema.ValueEncoding.Bool; import static org.neo4j.kernel.api.impl.schema.ValueEncoding.Bool;
import static org.neo4j.kernel.api.impl.schema.ValueEncoding.Number; import static org.neo4j.kernel.api.impl.schema.ValueEncoding.Number;
Expand All @@ -50,7 +52,7 @@ public void tooLongStringShouldBeSkipped()
@Test @Test
public void tooLongArrayShouldBeSkipped() public void tooLongArrayShouldBeSkipped()
{ {
byte[] bytes = RandomStringUtils.randomAscii( IndexWriter.MAX_TERM_LENGTH + 10).getBytes(); byte[] bytes = RandomStringUtils.randomAscii( IndexWriter.MAX_TERM_LENGTH + 10 ).getBytes();
Document document = LuceneDocumentStructure.documentRepresentingProperty( 123, bytes ); Document document = LuceneDocumentStructure.documentRepresentingProperty( 123, bytes );
assertNull( document.getField( Array.key() ) ); assertNull( document.getField( Array.key() ) );
} }
Expand Down Expand Up @@ -111,7 +113,8 @@ public void shouldBuildDocumentRepresentingArrayProperty() throws Exception
public void shouldBuildQueryRepresentingBoolProperty() throws Exception public void shouldBuildQueryRepresentingBoolProperty() throws Exception
{ {
// given // given
TermQuery query = (TermQuery) LuceneDocumentStructure.newSeekQuery( true ); ConstantScoreQuery constantScoreQuery = (ConstantScoreQuery) newSeekQuery( true );
TermQuery query = (TermQuery) constantScoreQuery.getQuery();


// then // then
assertEquals( "true", query.getTerm().text() ); assertEquals( "true", query.getTerm().text() );
Expand All @@ -121,18 +124,19 @@ public void shouldBuildQueryRepresentingBoolProperty() throws Exception
public void shouldBuildQueryRepresentingStringProperty() throws Exception public void shouldBuildQueryRepresentingStringProperty() throws Exception
{ {
// given // given
TermQuery query = (TermQuery) LuceneDocumentStructure.newSeekQuery( "Characters" ); ConstantScoreQuery query = (ConstantScoreQuery) newSeekQuery( "Characters" );


// then // then
assertEquals( "Characters", query.getTerm().text() ); assertEquals( "Characters", ((TermQuery) query.getQuery()).getTerm().text() );
} }


@SuppressWarnings( "unchecked" ) @SuppressWarnings( "unchecked" )
@Test @Test
public void shouldBuildQueryRepresentingNumberProperty() throws Exception public void shouldBuildQueryRepresentingNumberProperty() throws Exception
{ {
// given // given
NumericRangeQuery<Double> query = (NumericRangeQuery<Double>) LuceneDocumentStructure.newSeekQuery( 12 ); ConstantScoreQuery constantScoreQuery = (ConstantScoreQuery) newSeekQuery( 12 );
NumericRangeQuery<Double> query = (NumericRangeQuery<Double>) constantScoreQuery.getQuery();


// then // then
assertEquals( 12.0, query.getMin() ); assertEquals( 12.0, query.getMin() );
Expand All @@ -143,7 +147,9 @@ public void shouldBuildQueryRepresentingNumberProperty() throws Exception
public void shouldBuildQueryRepresentingArrayProperty() throws Exception public void shouldBuildQueryRepresentingArrayProperty() throws Exception
{ {
// given // given
TermQuery query = (TermQuery) LuceneDocumentStructure.newSeekQuery( new Integer[]{1, 2, 3} ); ConstantScoreQuery constantScoreQuery = (ConstantScoreQuery)
newSeekQuery( new Integer[]{1, 2, 3} );
TermQuery query = (TermQuery) constantScoreQuery.getQuery();


// then // then
assertEquals( "D1.0|2.0|3.0|", query.getTerm().text() ); assertEquals( "D1.0|2.0|3.0|", query.getTerm().text() );
Expand All @@ -167,7 +173,8 @@ public void shouldBuildRangeSeekByNumberQueryForStrings() throws Exception
public void shouldBuildRangeSeekByStringQueryForStrings() throws Exception public void shouldBuildRangeSeekByStringQueryForStrings() throws Exception
{ {
// given // given
TermRangeQuery query = (TermRangeQuery) LuceneDocumentStructure.newRangeSeekByStringQuery( "foo", false, null, true ); TermRangeQuery query =
(TermRangeQuery) LuceneDocumentStructure.newRangeSeekByStringQuery( "foo", false, null, true );


// then // then
assertEquals( "string", query.getField() ); assertEquals( "string", query.getField() );
Expand Down

0 comments on commit abfddb9

Please sign in to comment.