Skip to content

Commit

Permalink
Merge pull request #66 from timatbw/bw_branch_7_7_3
Browse files Browse the repository at this point in the history
Fix UpsertCondition due to Solr 7 encoding of String types in javabin…
  • Loading branch information
timatbw authored Mar 27, 2024
2 parents 71ef3c4 + 9861ef9 commit 9139219
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 9139219

Please sign in to comment.