Skip to content

Commit

Permalink
Fix UpsertCondition due to Solr 7 encoding of String types in javabin…
Browse files Browse the repository at this point in the history
… format.

This relates to a change in behaviour introduced in SOLR-12983 which affected how javabin encodes
strings, they are ByteArrayUtf8CharSequence and not String objects (but both are CharSequence). A
similar problem with UpdateRequestProcessors was noted and fixed in SOLR-13255 but that did not
fix the case of Atomic Updates where the field is a Map from operation->value and hence the value
was not a String anymore, but a ByteArrayUtf8CharSequence.
  • Loading branch information
timatbw committed Mar 27, 2024
1 parent fe7729f commit 9861ef9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -399,13 +399,13 @@ private static String getFieldFromDoc(String fieldName, SolrInputDocument doc) {
return null;
}
Object fieldValue = doc.getFieldValue(fieldName);
if (fieldValue instanceof String) {
return (String)fieldValue;
if (fieldValue instanceof CharSequence) {
return fieldValue.toString();
}
if (fieldValue instanceof Map) {
final Object setValue = ((Map)fieldValue).get("set");
if (setValue instanceof String) {
return (String)setValue;
if (setValue instanceof CharSequence) {
return setValue.toString();
}
}
// Cannot support non-String types or collection (multi-valued field) types
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.google.common.collect.ListMultimap;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.util.ByteArrayUtf8CharSequence;
import org.apache.solr.common.util.NamedList;
import org.junit.Test;

Expand Down Expand Up @@ -872,6 +873,19 @@ public void givenConcatWithOldDoc_whenRunning() {
assertThat(newDoc.getFieldValue("sku"), is("MacbookSilver"));
}

{
// Same test but simulating how the javabin format encodes strings
SolrInputDocument newDoc = new SolrInputDocument();
newDoc.setField("model_name", new ByteArrayUtf8CharSequence("Macbook"));
SolrInputDocument oldDoc = new SolrInputDocument();
oldDoc.setField("model_name", "Powerbook");
oldDoc.setField("colour", "Silver");
oldDoc.setField("sku", "PowerbookSilver");
assertTrue(condition.matches(oldDoc, newDoc));
assertThat(condition.run(oldDoc, newDoc), is(UpsertCondition.ActionType.CONCAT));
assertThat(newDoc.getFieldValue("sku"), is("MacbookSilver"));
}

{
SolrInputDocument newDoc = new SolrInputDocument();
newDoc.setField("model_name", "Macbook");
Expand Down Expand Up @@ -966,6 +980,20 @@ public void givenConcatWithAtomicUpdates_whenRunning() {
assertThat(newDoc.getFieldValue("sku"), is("MacbookSilver"));
}

{
// Same test but simulating how the javabin format encodes strings
SolrInputDocument newDoc = new SolrInputDocument();
newDoc.setField("model_name",
Collections.singletonMap("set", new ByteArrayUtf8CharSequence("Macbook")));
SolrInputDocument oldDoc = new SolrInputDocument();
oldDoc.setField("model_name", "Powerbook");
oldDoc.setField("colour", "Silver");
oldDoc.setField("sku", "PowerbookSilver");
assertTrue(condition.matches(oldDoc, newDoc));
assertThat(condition.run(oldDoc, newDoc), is(UpsertCondition.ActionType.CONCAT));
assertThat(newDoc.getFieldValue("sku"), is("MacbookSilver"));
}

{
SolrInputDocument newDoc = new SolrInputDocument();
newDoc.setField("unrelated_field", Collections.singletonMap("set", "English"));
Expand Down

0 comments on commit 9861ef9

Please sign in to comment.