Skip to content

Commit

Permalink
SOLR-16360: Atomic update on boolean fields doesn't reflect when valu…
Browse files Browse the repository at this point in the history
…e starts with "1", "t" or "T" (#1816)

* StrUtils.parseBoolean() can accept a CharSequence

---------

Co-authored-by: Rahul Goswami <rgoswami@commvault.com>
  • Loading branch information
rahulgoswami and rgoswami-cv authored Aug 9, 2023
1 parent 326753c commit 9b88eb8
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
2 changes: 2 additions & 0 deletions solr/CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ Bug Fixes

* SOLR-16922: Scripts wrongly prohibit embedded zookeeper when solr port is between 55535 and 64535 (Tiziano Degaetano, Colvin Cowie)

* SOLR-16360: Atomic update on boolean fields doesn't reflect when value starts with "1", "t" or "T" (Rahul Goswami, Justin Sweeney, David Smiley)

Dependency Upgrades
---------------------
(No changes)
Expand Down
6 changes: 3 additions & 3 deletions solr/core/src/java/org/apache/solr/schema/BoolField.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.apache.lucene.util.mutable.MutableValue;
import org.apache.lucene.util.mutable.MutableValueBool;
import org.apache.solr.analysis.SolrAnalyzer;
import org.apache.solr.common.util.StrUtils;
import org.apache.solr.response.TextResponseWriter;
import org.apache.solr.search.QParser;
import org.apache.solr.search.function.OrdFieldSource;
Expand Down Expand Up @@ -123,8 +124,7 @@ public Analyzer getQueryAnalyzer() {

@Override
public String toInternal(String val) {
char ch = (val != null && val.length() > 0) ? val.charAt(0) : 0;
return (ch == '1' || ch == 't' || ch == 'T') ? "T" : "F";
return StrUtils.parseBoolean(val) ? "T" : "F";
}

@Override
Expand Down Expand Up @@ -211,7 +211,7 @@ public List<IndexableField> createFields(SchemaField field, Object value) {
@Override
public Object toNativeType(Object val) {
if (val instanceof CharSequence) {
return Boolean.valueOf(val.toString());
return Boolean.valueOf(StrUtils.parseBoolean((CharSequence) val));
}
return super.toNativeType(val);
}
Expand Down
14 changes: 13 additions & 1 deletion solr/core/src/test/org/apache/solr/schema/BooleanFieldTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@

package org.apache.solr.schema;

import java.util.Map;
import org.apache.solr.SolrTestCaseJ4;
import org.junit.BeforeClass;
import org.junit.Test;

public class BooleanFieldTest extends SolrTestCaseJ4 {
@BeforeClass
public static void beforeClass() throws Exception {
initCore("solrconfig-basic.xml", "schema15.xml");
initCore("solrconfig-tlog.xml", "schema15.xml");
}

// Note, docValues-based boolean tests are tested elsewhere referring to more appropriate schemas
Expand Down Expand Up @@ -108,5 +109,16 @@ public void testBoolField() {
"//lst[@name='bindsto']/int[@name='true'][.='2']",
"//lst[@name='bindstom']/int[@name='false'][.='2']",
"//lst[@name='bindstom']/int[@name='true'][.='3']");

// SOLR-16360
assertU(adoc("id", "7", "bindsto", "false"));
assertU(commit());
// do atomic update
assertU(adoc(sdoc("id", "7", "bindsto", Map.of("set", "1"))));
assertQ(
req("qt", "/get", "id", "7"),
"count(//doc)=1",
"//doc/str[@name='id'][.='7']",
"//doc/bool[@name='bindsto'][.='true']");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ public static List<String> toLower(List<String> strings) {
}

/** Return if a string starts with '1', 't', or 'T' and return false otherwise. */
public static boolean parseBoolean(String s) {
public static boolean parseBoolean(CharSequence s) {
char ch = s.length() > 0 ? s.charAt(0) : 0;
return (ch == '1' || ch == 't' || ch == 'T');
}
Expand Down

0 comments on commit 9b88eb8

Please sign in to comment.