Apache Lucene open-source search software
SOLR-5598: LanguageIdentifierUpdateProcessor ignores all but the first value of multiValued string fields
Shalin Shekhar Mangare63b46137de6
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex 628d923cdb8..9e1cfde86b3 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -267,6 +267,9 @@ Bug Fixes* SOLR-5526: Fixed NPE that could arrise when explicitly configuring some builtin QParserPlugins (Nikolay Khitrin, Vitaliy Zhovtyuk, hossman)+* SOLR-5598: LanguageIdentifierUpdateProcessor ignores all but the first value+ of multiValued string fields. (Andreas Hubold, Vitaliy Zhovtyuk via shalin)+Optimizations----------------------diff --git a/solr/contrib/langid/src/java/org/apache/solr/update/processor/LanguageIdentifierUpdateProcessor.java b/solr/contrib/langid/src/java/org/apache/solr/update/processor/LanguageIdentifierUpdateProcessor.javaindex f035f0c0b7d..1717c486783 100644--- a/solr/contrib/langid/src/java/org/apache/solr/update/processor/LanguageIdentifierUpdateProcessor.java+++ b/solr/contrib/langid/src/java/org/apache/solr/update/processor/LanguageIdentifierUpdateProcessor.java@@ -33,6 +33,7 @@ import org.slf4j.LoggerFactory;import java.io.IOException;import java.util.ArrayList;import java.util.Arrays;+import java.util.Collection;import java.util.HashMap;import java.util.HashSet;import java.util.List;@@ -291,12 +292,16 @@ public abstract class LanguageIdentifierUpdateProcessor extends UpdateRequestProfor (String fieldName : inputFields) {log.debug("Appending field "+fieldName);if (doc.containsKey(fieldName)) {- Object content = doc.getFieldValue(fieldName);- if(content instanceof String) {- sb.append((String) doc.getFieldValue(fieldName));- sb.append(" ");- } else {- log.warn("Field "+fieldName+" not a String value, not including in detection");+ Collection<Object> fieldValues = doc.getFieldValues(fieldName);+ if (fieldValues != null) {+ for (Object content : fieldValues) {+ if (content instanceof String) {+ sb.append((String) content);+ sb.append(" ");+ } else {+ log.warn("Field " + fieldName + " not a String value, not including in detection");+ }+ }}}}diff --git a/solr/contrib/langid/src/test/org/apache/solr/update/processor/LanguageIdentifierUpdateProcessorFactoryTestCase.java b/solr/contrib/langid/src/test/org/apache/solr/update/processor/LanguageIdentifierUpdateProcessorFactoryTestCase.javaindex 550d10fc0c8..d845d182aac 100644--- a/solr/contrib/langid/src/test/org/apache/solr/update/processor/LanguageIdentifierUpdateProcessorFactoryTestCase.java+++ b/solr/contrib/langid/src/test/org/apache/solr/update/processor/LanguageIdentifierUpdateProcessorFactoryTestCase.java@@ -19,7 +19,6 @@ package org.apache.solr.update.processor;import java.util.ArrayList;import java.util.List;-import org.apache.solr.SolrTestCaseJ4;import org.apache.solr.common.SolrInputDocument;import org.apache.solr.common.params.ModifiableSolrParams;@@ -87,7 +86,6 @@ public abstract class LanguageIdentifierUpdateProcessorFactoryTestCase extends SassertLang("uk", "id", "20uk", "name", "Ukrainian", "subject", "Народно-господарський комплекс країни включає такі види промисловості як важке машинобудування, чорна та кольорова металургія, суднобудування, виробництво автобусів, легкових та вантажних автомобілів, тракторів та іншої сільськогосподарської техніки, тепловозів, верстатів, турбін, авіаційних двигунів та літаків, обладнання для електростанцій, нафто-газової та хімічної промисловості тощо. Крім того, Україна є потужним виробником електроенергії. Україна має розвинуте сільське господарство і займає одне з провідних місць серед експортерів деяких видів сільськогосподарської продукції і продовольства (зокрема, соняшникової олії).");}-@Testpublic void testMapFieldName() throws Exception {parameters = new ModifiableSolrParams();@@ -153,6 +151,58 @@ public abstract class LanguageIdentifierUpdateProcessorFactoryTestCase extends SassertNotNull(liProcessor.process(doc).getFieldValue("text_no"));}+ /**+ * Test not only 1st value taken into account (empty string),+ * but all other values of 'text_multivalue' field ('en').+ */+ @Test+ public void testPreExistingMultiValue() throws Exception {+ SolrInputDocument doc;+ parameters = new ModifiableSolrParams();+ parameters.add("langid.fl", "text_multivalue");+ parameters.add("langid.langField", "language");+ parameters.add("langid.langsField", "languages");+ parameters.add("langid.enforceSchema", "false");+ parameters.add("langid.map", "true");+ liProcessor = createLangIdProcessor(parameters);++ doc = englishDoc();+ assertEquals("en", liProcessor.process(doc).getFieldValue("language"));+ assertEquals("en", liProcessor.process(doc).getFieldValue("languages"));++ doc = englishDoc();+ doc.setField("language", "no");+ assertEquals("no", liProcessor.process(doc).getFieldValue("language"));+ assertEquals("no", liProcessor.process(doc).getFieldValue("languages"));+ assertNotNull(liProcessor.process(doc).getFieldValue("text_multivalue_no"));+ }++ /**+ * Test not only 1st value taken into account (ru text),+ * but all values of 'text_multivalue' field ('ru' and 'en').+ */+ @Test+ public void testPreExistingMultiValueMixedLang() throws Exception {+ SolrInputDocument doc;+ parameters = new ModifiableSolrParams();+ parameters.add("langid.fl", "text_multivalue");+ parameters.add("langid.langField", "language");+ parameters.add("langid.langsField", "languages");+ parameters.add("langid.enforceSchema", "false");+ parameters.add("langid.map", "true");+ liProcessor = createLangIdProcessor(parameters);++ doc = mixedEnglishRussianDoc();+ assertEquals("en", liProcessor.process(doc).getFieldValue("language"));+ assertEquals("en", liProcessor.process(doc).getFieldValue("languages"));++ doc = mixedEnglishRussianDoc();+ doc.setField("language", "no");+ assertEquals("no", liProcessor.process(doc).getFieldValue("language"));+ assertEquals("no", liProcessor.process(doc).getFieldValue("languages"));+ assertNotNull(liProcessor.process(doc).getFieldValue("text_multivalue_no"));+ }+@Testpublic void testDefaultFallbackEmptyString() throws Exception {SolrInputDocument doc;@@ -216,6 +266,20 @@ public abstract class LanguageIdentifierUpdateProcessorFactoryTestCase extends Sprivate SolrInputDocument englishDoc() {SolrInputDocument doc = new SolrInputDocument();doc.addField("text", "Apache Lucene is a free/open source information retrieval software library, originally created in Java by Doug Cutting. It is supported by the Apache Software Foundation and is released under the Apache Software License.");+ doc.addField("text_multivalue", new String[]{"", "Apache Lucene is a free/open source information retrieval software library, originally created in Java by Doug Cutting. It is supported by the Apache Software Foundation and is released under the Apache Software License."});+ return doc;+ }++ /**+ * Construct document containing multi-value fields in different languages.+ * @return solr input document+ */+ private SolrInputDocument mixedEnglishRussianDoc() {+ SolrInputDocument doc = new SolrInputDocument();+ doc.addField("text_multivalue", new String[]{"The Apache Lucene — это свободная библиотека для высокоскоростного полнотекстового поиска, написанная на Java. Может быть использована для поиска в интернете и других областях компьютерной лингвистики (аналитическая философия).",+ "Apache Lucene is a free/open source information retrieval software library, originally created in Java by Doug Cutting. It is supported by the Apache Software Foundation and is released under the Apache Software License.",+ "Solr (pronounced \"solar\") is an open source enterprise search platform from the Apache Lucene project. Its major features include full-text search, hit highlighting, faceted search, dynamic clustering, database integration, and rich document (e.g., Word, PDF) handling."+ });return doc;}