Apache Solr open-source search software
SOLR-2939: Clustering of multilingual search results
Stanisław Osiński4d5638474469
diff --git a/solr/contrib/clustering/CHANGES.txt b/solr/contrib/clustering/CHANGES.txtindex eb47253cada..debc9489b0d 100644--- a/solr/contrib/clustering/CHANGES.txt+++ b/solr/contrib/clustering/CHANGES.txt@@ -22,6 +22,10 @@ $Id$carrot.snippet can now take comma- or space-separated lists offield names to cluster (Stanislaw Osinski).+* SOLR-2939: Clustering of multilingual search results. The document's+ language field be passed in the carrot.lang parameter, the carrot.lcmap+ parameter enables mapping of language codes to ISO 639 (Stanislaw Osinski).+================== Release 3.5.0 ==================(No Changes)diff --git a/solr/contrib/clustering/src/java/org/apache/solr/handler/clustering/carrot2/CarrotClusteringEngine.java b/solr/contrib/clustering/src/java/org/apache/solr/handler/clustering/carrot2/CarrotClusteringEngine.javaindex ff44347943b..74b36b8fb72 100644--- a/solr/contrib/clustering/src/java/org/apache/solr/handler/clustering/carrot2/CarrotClusteringEngine.java+++ b/solr/contrib/clustering/src/java/org/apache/solr/handler/clustering/carrot2/CarrotClusteringEngine.java@@ -29,6 +29,7 @@ import java.util.Map;import java.util.Set;import org.apache.commons.io.IOUtils;+import org.apache.commons.lang.ObjectUtils;import org.apache.commons.lang.StringUtils;import org.apache.lucene.search.Query;import org.apache.solr.common.SolrDocument;@@ -55,6 +56,7 @@ import org.carrot2.core.Controller;import org.carrot2.core.ControllerFactory;import org.carrot2.core.Document;import org.carrot2.core.IClusteringAlgorithm;+import org.carrot2.core.LanguageCode;import org.carrot2.core.attribute.AttributeNames;import org.carrot2.text.linguistic.DefaultLexicalDataFactoryDescriptor;import org.carrot2.text.preprocessing.pipeline.BasicPreprocessingPipelineDescriptor;@@ -65,6 +67,7 @@ import org.carrot2.util.resource.ResourceLookup;import org.slf4j.Logger;import org.slf4j.LoggerFactory;+import com.google.common.base.Function;import com.google.common.collect.Lists;import com.google.common.collect.Maps;import com.google.common.collect.Sets;@@ -103,7 +106,7 @@ public class CarrotClusteringEngine extends SearchClusteringEngine {*/private Controller controller = ControllerFactory.createPooling();private Class<? extends IClusteringAlgorithm> clusteringAlgorithmClass;-+private static class SolrResourceLocator implements IResourceLocator {private final SolrResourceLoader resourceLoader;private final String carrot2ResourcesDir;@@ -320,6 +323,22 @@ public class CarrotClusteringEngine extends SearchClusteringEngine {String urlField = solrParams.get(CarrotParams.URL_FIELD_NAME, "url");String titleFieldSpec = solrParams.get(CarrotParams.TITLE_FIELD_NAME, "title");String snippetFieldSpec = solrParams.get(CarrotParams.SNIPPET_FIELD_NAME, titleFieldSpec);+ String languageField = solrParams.get(CarrotParams.LANGUAGE_FIELD_NAME, null);++ // Parse language code map string into a map+ Map<String, String> languageCodeMap = Maps.newHashMap();+ if (StringUtils.isNotBlank(languageField)) {+ for (String pair : solrParams.get(CarrotParams.LANGUAGE_CODE_MAP, "")+ .split("[, ]")) {+ final String[] split = pair.split(":");+ if (split.length == 2 && StringUtils.isNotBlank(split[0]) && StringUtils.isNotBlank(split[1])) {+ languageCodeMap.put(split[0], split[1]);+ } else {+ log.warn("Unsupported format for " + CarrotParams.LANGUAGE_CODE_MAP+ + ": '" + pair + "'. Skipping this mapping.");+ }+ }+ }// Get the documentsboolean produceSummary = solrParams.getBool(CarrotParams.PRODUCE_SUMMARY, false);@@ -392,9 +411,42 @@ public class CarrotClusteringEngine extends SearchClusteringEngine {snippet = getConcatenated(sdoc, snippetFieldSpec);}+ // Create a Carrot2 documentDocument carrotDocument = new Document(getConcatenated(sdoc, titleFieldSpec),snippet, (String)sdoc.getFieldValue(urlField));++ // Store Solr id of the document, we need it to map document instances+ // found in clusters back to identifiers.carrotDocument.setField(SOLR_DOCUMENT_ID, sdoc.getFieldValue(idFieldName));++ // Set language+ if (StringUtils.isNotBlank(languageField)) {+ Collection<Object> languages = sdoc.getFieldValues(languageField);+ if (languages != null) {++ // Use the first Carrot2-supported language+ for (Object l : languages) {+ String lang = ObjectUtils.toString(l, "");++ if (languageCodeMap.containsKey(lang)) {+ lang = languageCodeMap.get(lang);+ }++ // Language detection Library for Java uses dashes to separate+ // language variants, such as 'zh-cn', but Carrot2 uses underscores.+ if (lang.indexOf('-') > 0) {+ lang = lang.replace('-', '_');+ }++ // If the language is supported by Carrot2, we'll get a non-null value+ final LanguageCode carrot2Language = LanguageCode.forISOCode(lang);+ if (carrot2Language != null) {+ carrotDocument.setLanguage(carrot2Language);+ break;+ }+ }+ }+ }result.add(carrotDocument);}diff --git a/solr/contrib/clustering/src/java/org/apache/solr/handler/clustering/carrot2/CarrotParams.java b/solr/contrib/clustering/src/java/org/apache/solr/handler/clustering/carrot2/CarrotParams.javaindex ca376f56f4c..b5e8b94c25c 100644--- a/solr/contrib/clustering/src/java/org/apache/solr/handler/clustering/carrot2/CarrotParams.java+++ b/solr/contrib/clustering/src/java/org/apache/solr/handler/clustering/carrot2/CarrotParams.java@@ -27,18 +27,23 @@ public interface CarrotParams {String CARROT_PREFIX = "carrot.";String ALGORITHM = CARROT_PREFIX + "algorithm";+String TITLE_FIELD_NAME = CARROT_PREFIX + "title";String URL_FIELD_NAME = CARROT_PREFIX + "url";String SNIPPET_FIELD_NAME = CARROT_PREFIX + "snippet";+ String LANGUAGE_FIELD_NAME = CARROT_PREFIX + "lang";+String PRODUCE_SUMMARY = CARROT_PREFIX + "produceSummary";- String NUM_DESCRIPTIONS = CARROT_PREFIX + "numDescriptions";- String OUTPUT_SUB_CLUSTERS = CARROT_PREFIX + "outputSubClusters";String SUMMARY_FRAGSIZE = CARROT_PREFIX + "fragSize";String SUMMARY_SNIPPETS = CARROT_PREFIX + "summarySnippets";+ String NUM_DESCRIPTIONS = CARROT_PREFIX + "numDescriptions";+ String OUTPUT_SUB_CLUSTERS = CARROT_PREFIX + "outputSubClusters";String LEXICAL_RESOURCES_DIR = CARROT_PREFIX + "lexicalResourcesDir";+ String LANGUAGE_CODE_MAP = CARROT_PREFIX + "lcmap";public static final Set<String> CARROT_PARAM_NAMES = ImmutableSet.of(- ALGORITHM, TITLE_FIELD_NAME, URL_FIELD_NAME, SNIPPET_FIELD_NAME,- PRODUCE_SUMMARY, NUM_DESCRIPTIONS, OUTPUT_SUB_CLUSTERS, SUMMARY_FRAGSIZE);+ ALGORITHM, TITLE_FIELD_NAME, URL_FIELD_NAME, SNIPPET_FIELD_NAME, LANGUAGE_FIELD_NAME,+ PRODUCE_SUMMARY, SUMMARY_FRAGSIZE, SUMMARY_SNIPPETS, NUM_DESCRIPTIONS, OUTPUT_SUB_CLUSTERS,+ LEXICAL_RESOURCES_DIR);}diff --git a/solr/contrib/clustering/src/test-files/clustering/solr/conf/schema.xml b/solr/contrib/clustering/src/test-files/clustering/solr/conf/schema.xmlindex 915b42f50ec..64ca87a3be5 100644--- a/solr/contrib/clustering/src/test-files/clustering/solr/conf/schema.xml+++ b/solr/contrib/clustering/src/test-files/clustering/solr/conf/schema.xml@@ -280,6 +280,7 @@<field name="id" type="string" indexed="true" stored="true" required="true" /><field name="url" type="string" indexed="true" stored="true" required="true" />+ <field name="lang" type="string" indexed="true" stored="true" required="false" multiValued="true" /><field name="title" type="text" indexed="true" stored="true" multiValued="true"/><field name="heading" type="text" indexed="true" stored="true" multiValued="true"/>diff --git a/solr/contrib/clustering/src/test/org/apache/solr/handler/clustering/AbstractClusteringTestCase.java b/solr/contrib/clustering/src/test/org/apache/solr/handler/clustering/AbstractClusteringTestCase.javaindex c87e83bd4da..77827c6f5e1 100644--- a/solr/contrib/clustering/src/test/org/apache/solr/handler/clustering/AbstractClusteringTestCase.java+++ b/solr/contrib/clustering/src/test/org/apache/solr/handler/clustering/AbstractClusteringTestCase.java@@ -56,6 +56,23 @@ public abstract class AbstractClusteringTestCase extends SolrTestCaseJ4 {multiFieldDoc.addField("body", "Body field: this is the contents of the body field that will get clustered together with snippet.");assertNull(h.validateUpdate(adoc(multiFieldDoc)));+ // Add a document with one language supported by Carrot2+ final SolrInputDocument docWithOneSupprtedLanguage = new SolrInputDocument();+ docWithOneSupprtedLanguage.addField("id", numberOfDocs++);+ docWithOneSupprtedLanguage.addField("title", "");+ docWithOneSupprtedLanguage.addField("url", "one_supported_language");+ docWithOneSupprtedLanguage.addField("lang", "zh-cn");+ assertNull(h.validateUpdate(adoc(docWithOneSupprtedLanguage)));++ // Add a document with more languages, one supported by Carrot2+ final SolrInputDocument docWithOneSupprtedLanguageOfMany = new SolrInputDocument();+ docWithOneSupprtedLanguageOfMany.addField("id", numberOfDocs++);+ docWithOneSupprtedLanguageOfMany.addField("url", "one_supported_language_of_many");+ docWithOneSupprtedLanguageOfMany.addField("lang", "zh-tw");+ docWithOneSupprtedLanguageOfMany.addField("lang", "POLISH");+ docWithOneSupprtedLanguageOfMany.addField("lang", "de");+ assertNull(h.validateUpdate(adoc(docWithOneSupprtedLanguageOfMany)));+assertNull(h.validateUpdate(commit()));}diff --git a/solr/contrib/clustering/src/test/org/apache/solr/handler/clustering/carrot2/CarrotClusteringEngineTest.java b/solr/contrib/clustering/src/test/org/apache/solr/handler/clustering/carrot2/CarrotClusteringEngineTest.javaindex 738a0bd9d8d..801c2ecbffa 100644--- a/solr/contrib/clustering/src/test/org/apache/solr/handler/clustering/carrot2/CarrotClusteringEngineTest.java+++ b/solr/contrib/clustering/src/test/org/apache/solr/handler/clustering/carrot2/CarrotClusteringEngineTest.java@@ -39,6 +39,7 @@ import org.apache.solr.search.DocList;import org.apache.solr.search.SolrIndexSearcher;import org.apache.solr.util.RefCounted;import org.apache.solr.util.SolrPluginUtils;+import org.carrot2.core.LanguageCode;import org.carrot2.util.attribute.AttributeUtils;import org.junit.Test;@@ -89,7 +90,7 @@ public class CarrotClusteringEngineTest extends AbstractClusteringTestCase {private List<NamedList<Object>> clusterWithHighlighting(boolean enableHighlighting, int fragSize) throws IOException {// Some documents don't have mining in the snippet- return clusterWithHighlighting(enableHighlighting, fragSize, 1, "mine", numberOfDocs - 4);+ return clusterWithHighlighting(enableHighlighting, fragSize, 1, "mine", numberOfDocs - 6);}private List<NamedList<Object>> clusterWithHighlighting(@@ -295,6 +296,43 @@ public class CarrotClusteringEngineTest extends AbstractClusteringTestCase {}+ @Test+ public void oneCarrot2SupportedLanguage() throws Exception {+ final ModifiableSolrParams params = new ModifiableSolrParams();+ params.add(CarrotParams.LANGUAGE_FIELD_NAME, "lang");++ final List<String> labels = getLabels(checkEngine(+ getClusteringEngine("echo"), 1, 1, new TermQuery(new Term("url",+ "one_supported_language")), params).get(0));+ assertEquals(3, labels.size());+ assertEquals("Correct Carrot2 language", LanguageCode.CHINESE_SIMPLIFIED.name(), labels.get(2));+ }++ @Test+ public void oneCarrot2SupportedLanguageOfMany() throws Exception {+ final ModifiableSolrParams params = new ModifiableSolrParams();+ params.add(CarrotParams.LANGUAGE_FIELD_NAME, "lang");++ final List<String> labels = getLabels(checkEngine(+ getClusteringEngine("echo"), 1, 1, new TermQuery(new Term("url",+ "one_supported_language_of_many")), params).get(0));+ assertEquals(3, labels.size());+ assertEquals("Correct Carrot2 language", LanguageCode.GERMAN.name(), labels.get(2));+ }++ @Test+ public void languageCodeMapping() throws Exception {+ final ModifiableSolrParams params = new ModifiableSolrParams();+ params.add(CarrotParams.LANGUAGE_FIELD_NAME, "lang");+ params.add(CarrotParams.LANGUAGE_CODE_MAP, "POLISH:pl");++ final List<String> labels = getLabels(checkEngine(+ getClusteringEngine("echo"), 1, 1, new TermQuery(new Term("url",+ "one_supported_language_of_many")), params).get(0));+ assertEquals(3, labels.size());+ assertEquals("Correct Carrot2 language", LanguageCode.POLISH.name(), labels.get(2));+ }+private CarrotClusteringEngine getClusteringEngine(String engineName) {ClusteringComponent comp = (ClusteringComponent) h.getCore().getSearchComponent("clustering");@@ -367,7 +405,7 @@ public class CarrotClusteringEngineTest extends AbstractClusteringTestCase {List<Object> docs = getDocs(cluster);assertNotNull("docs is null and it shouldn't be", docs);for (int j = 0; j < docs.size(); j++) {- String id = (String) docs.get(j);+ Object id = docs.get(j);assertNotNull("id is null and it shouldn't be", id);}diff --git a/solr/contrib/clustering/src/test/org/apache/solr/handler/clustering/carrot2/EchoClusteringAlgorithm.java b/solr/contrib/clustering/src/test/org/apache/solr/handler/clustering/carrot2/EchoClusteringAlgorithm.javaindex 88e75dfa868..4084f265899 100644--- a/solr/contrib/clustering/src/test/org/apache/solr/handler/clustering/carrot2/EchoClusteringAlgorithm.java+++ b/solr/contrib/clustering/src/test/org/apache/solr/handler/clustering/carrot2/EchoClusteringAlgorithm.java@@ -55,6 +55,9 @@ public class EchoClusteringAlgorithm extends ProcessingComponentBase implementsfor (Document document : documents) {final Cluster cluster = new Cluster();cluster.addPhrases(document.getTitle(), document.getSummary());+ if (document.getLanguage() != null) {+ cluster.addPhrases(document.getLanguage().name());+ }cluster.addDocuments(document);clusters.add(cluster);}