Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
Signed-off-by: panguixin <[email protected]>
  • Loading branch information
bugmakerrrrrr committed Jan 8, 2025
1 parent 520387a commit dca3469
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ public Query rangeQuery(Object lowerTerm, Object upperTerm, boolean includeLower
}
failIfNotIndexedAndNoDocValues();

if (lowerTerm != null && upperTerm != null) {
if ((lowerTerm != null && upperTerm != null)) {
return valueFieldType().rangeQuery(
rewriteSearchValue(lowerTerm),
rewriteSearchValue(upperTerm),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,18 @@ public void testRangeQuery() {
hasDocValue
);

if (searchable == false && hasDocValue == false) {
IllegalArgumentException e = expectThrows(
IllegalArgumentException.class,
() -> ft.rangeQuery(new BytesRef("2"), new BytesRef("10"), true, true, MOCK_QSC_ENABLE_INDEX_DOC_VALUES)
);
assertEquals(
"Cannot search on field [field] since it is both not indexed, and does not have doc_values " + "enabled.",
e.getMessage()
);
continue;
}

Query indexQuery = new TermRangeQuery("field" + VALUE_SUFFIX, new BytesRef("2"), new BytesRef("10"), true, true);
Query dvQuery = new TermRangeQuery(
"field" + VALUE_SUFFIX,
Expand All @@ -810,7 +822,6 @@ public void testRangeQuery() {
}
}

// test isSearchable=true, hasDocValues=true, mappedFieldTypeName!=null
{
for (boolean searchable : new boolean[] { true, false }) {
for (boolean hasDocValue : new boolean[] { true, false }) {
Expand All @@ -820,6 +831,20 @@ public void testRangeQuery() {
searchable,
hasDocValue
);

if (searchable == false && hasDocValue == false) {
IllegalArgumentException e = expectThrows(
IllegalArgumentException.class,
() -> ft.rangeQuery(new BytesRef("2"), new BytesRef("10"), true, true, MOCK_QSC_ENABLE_INDEX_DOC_VALUES)
);
assertEquals(
"Cannot search on field [field.field1] since it is both not indexed, and does not have doc_values "
+ "enabled.",
e.getMessage()
);
continue;
}

Query indexQuery = new TermRangeQuery(
"field" + VALUE_AND_PATH_SUFFIX,
new BytesRef("field.field1=2"),
Expand All @@ -846,42 +871,6 @@ public void testRangeQuery() {
}
}

// test isSearchable=false, hasDocValues=false, mappedFieldTypeName=null
{
FlatObjectFieldMapper.FlatObjectFieldType ft = (FlatObjectFieldMapper.FlatObjectFieldType) getFlatParentFieldType(
"field",
null,
false,
false
);
IllegalArgumentException e = expectThrows(
IllegalArgumentException.class,
() -> ft.rangeQuery(new BytesRef("2"), new BytesRef("10"), true, true, MOCK_QSC_ENABLE_INDEX_DOC_VALUES)
);
assertEquals(
"Cannot search on field [field] since it is both not indexed, and does not have doc_values " + "enabled.",
e.getMessage()
);
}

// test isSearchable=false, hasDocValues=false, mappedFieldTypeName!=null
{
FlatObjectFieldMapper.FlatObjectFieldType ft = (FlatObjectFieldMapper.FlatObjectFieldType) getFlatParentFieldType(
"field.field1",
"field",
false,
false
);
IllegalArgumentException e = expectThrows(
IllegalArgumentException.class,
() -> ft.rangeQuery(new BytesRef("2"), new BytesRef("10"), true, true, MOCK_QSC_ENABLE_INDEX_DOC_VALUES)
);
assertEquals(
"Cannot search on field [field.field1] since it is both not indexed, and does not have doc_values " + "enabled.",
e.getMessage()
);
}

{
for (boolean searchable : new boolean[] { true, false }) {
for (boolean hasDocValue : new boolean[] { true, false }) {
Expand All @@ -891,20 +880,33 @@ public void testRangeQuery() {
searchable,
hasDocValue
);

if (searchable == false && hasDocValue == false) {
IllegalArgumentException e = expectThrows(
IllegalArgumentException.class,
() -> ft.rangeQuery(new BytesRef("2"), new BytesRef("10"), true, true, MOCK_QSC_ENABLE_INDEX_DOC_VALUES)
);
assertEquals(
"Cannot search on field [field] since it is both not indexed, and does not have doc_values " + "enabled.",
e.getMessage()
);
continue;
}
boolean nullLowerTerm = randomBoolean();
boolean nullUpperTerm = nullLowerTerm == false || randomBoolean();

Automaton a1 = PrefixQuery.toAutomaton(new BytesRef("field."));
Automaton a2 = TermRangeQuery.toAutomaton(
nullLowerTerm ? null : new BytesRef("field.2"),
nullLowerTerm ? new BytesRef("field.10") : null,
nullUpperTerm ? null : new BytesRef("field.10"),
true,
true
);
Automaton dvAutomaton = Operations.intersection(a1, a2);
Query indexQuery = new TermRangeQuery(
"field" + VALUE_SUFFIX,
nullLowerTerm ? null : new BytesRef("2"),
nullLowerTerm ? new BytesRef("10") : null,
nullUpperTerm ? null : new BytesRef("10"),
true,
true
);
Expand All @@ -922,7 +924,7 @@ public void testRangeQuery() {
expected,
ft.rangeQuery(
nullLowerTerm ? null : new BytesRef("2"),
nullLowerTerm ? new BytesRef("10") : null,
nullUpperTerm ? null : new BytesRef("10"),
true,
true,
MOCK_QSC_ENABLE_INDEX_DOC_VALUES
Expand All @@ -941,12 +943,25 @@ public void testRangeQuery() {
searchable,
hasDocValue
);
boolean nullLowerTerm = randomBoolean();
if (searchable == false && hasDocValue == false) {
IllegalArgumentException e = expectThrows(
IllegalArgumentException.class,
() -> ft.rangeQuery(new BytesRef("2"), new BytesRef("10"), true, true, MOCK_QSC_ENABLE_INDEX_DOC_VALUES)
);
assertEquals(
"Cannot search on field [field.field1] since it is both not indexed, and does not have doc_values "
+ "enabled.",
e.getMessage()
);
continue;
}
boolean nullLowerTerm = true;// randomBoolean();
boolean nullUpperTerm = true;// nullLowerTerm == false || randomBoolean();

Automaton a1 = PrefixQuery.toAutomaton(new BytesRef("field.field1="));
Automaton a2 = TermRangeQuery.toAutomaton(
nullLowerTerm ? null : new BytesRef("field.field1=2"),
nullLowerTerm ? new BytesRef("field.field1=10") : null,
nullUpperTerm ? null : new BytesRef("field.field1=10"),
true,
true
);
Expand All @@ -955,7 +970,7 @@ public void testRangeQuery() {
Automaton dvA1 = PrefixQuery.toAutomaton(new BytesRef("field.field.field1="));
Automaton dvA2 = TermRangeQuery.toAutomaton(
nullLowerTerm ? null : new BytesRef("field.field.field1=2"),
nullLowerTerm ? new BytesRef("field.field.field1=10") : null,
nullUpperTerm ? null : new BytesRef("field.field.field1=10"),
true,
true
);
Expand All @@ -980,7 +995,7 @@ public void testRangeQuery() {
expected,
ft.rangeQuery(
nullLowerTerm ? null : new BytesRef("2"),
nullLowerTerm ? new BytesRef("10") : null,
nullUpperTerm ? null : new BytesRef("10"),
true,
true,
MOCK_QSC_ENABLE_INDEX_DOC_VALUES
Expand Down

0 comments on commit dca3469

Please sign in to comment.