Apache Lucene open-source search software
SOLR-7072: Multiple mlt.fl does not work
Shalin Shekhar Mangar118a8263781b
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex 0e63ccd0c88..cc2fe070f05 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -103,6 +103,8 @@ Bug Fixes* SOLR-7046: NullPointerException when group.function uses query() function.(Jim Musil via Erick Erickson)+* SOLR-7072: Multiple mlt.fl does not work. (Constantin Mitocaru, shalin)+Optimizations----------------------* SOLR-7049: Move work done by the LIST Collections API call to the Collectionsdiff --git a/solr/core/src/java/org/apache/solr/handler/MoreLikeThisHandler.java b/solr/core/src/java/org/apache/solr/handler/MoreLikeThisHandler.javaindex 049cba869f9..ac010b26468 100644--- a/solr/core/src/java/org/apache/solr/handler/MoreLikeThisHandler.java+++ b/solr/core/src/java/org/apache/solr/handler/MoreLikeThisHandler.java@@ -27,6 +27,7 @@ import org.apache.lucene.search.BooleanQuery;import org.apache.lucene.search.Query;import org.apache.lucene.search.TermQuery;import org.apache.solr.common.SolrException;+import org.apache.solr.common.StringUtils;import org.apache.solr.common.params.CommonParams;import org.apache.solr.common.params.FacetParams;import org.apache.solr.common.params.MoreLikeThisParams;@@ -314,7 +315,19 @@ public class MoreLikeThisHandler extends RequestHandlerBasethis.needDocSet = params.getBool(FacetParams.FACET,false);SolrParams required = params.required();- String[] fields = splitList.split( required.get(MoreLikeThisParams.SIMILARITY_FIELDS) );+ String[] fl = required.getParams(MoreLikeThisParams.SIMILARITY_FIELDS);+ List<String> list = new ArrayList<>();+ for (String f : fl) {+ if (!StringUtils.isEmpty(f)) {+ String[] strings = splitList.split(f);+ for (String string : strings) {+ if (!StringUtils.isEmpty(string)) {+ list.add(string);+ }+ }+ }+ }+ String[] fields = list.toArray(new String[list.size()]);if( fields.length < 1 ) {throw new SolrException( SolrException.ErrorCode.BAD_REQUEST,"MoreLikeThis requires at least one similarity field: "+MoreLikeThisParams.SIMILARITY_FIELDS );diff --git a/solr/core/src/test/org/apache/solr/handler/component/DistributedMLTComponentTest.java b/solr/core/src/test/org/apache/solr/handler/component/DistributedMLTComponentTest.javaindex 6bf513676d6..f67d43d18d4 100644--- a/solr/core/src/test/org/apache/solr/handler/component/DistributedMLTComponentTest.java+++ b/solr/core/src/test/org/apache/solr/handler/component/DistributedMLTComponentTest.java@@ -17,9 +17,15 @@ package org.apache.solr.handler.component;* limitations under the License.*/+import java.util.HashMap;+import java.util.Map;+import org.apache.lucene.util.LuceneTestCase.Slow;import org.apache.solr.BaseDistributedSearchTestCase;+import org.apache.solr.client.solrj.response.QueryResponse;+import org.apache.solr.common.SolrDocumentList;import org.apache.solr.common.params.MoreLikeThisParams;+import org.apache.solr.common.util.NamedList;import org.junit.Ignore;import org.junit.Test;@@ -31,7 +37,6 @@ import org.junit.Test;* @see org.apache.solr.handler.component.MoreLikeThisComponent*/@Slow-@Ignore("fails on freebsd")public class DistributedMLTComponentTest extends BaseDistributedSearchTestCase {private String requestHandlerName;@@ -51,30 +56,30 @@ public class DistributedMLTComponentTest extends BaseDistributedSearchTestCase {@ShardsFixed(num = 3)public void test() throws Exception {del("*:*");- index(id, "1", "lowerfilt", "toyota");- index(id, "2", "lowerfilt", "chevrolet");- index(id, "3", "lowerfilt", "suzuki");- index(id, "4", "lowerfilt", "ford");- index(id, "5", "lowerfilt", "ferrari");- index(id, "6", "lowerfilt", "jaguar");- index(id, "7", "lowerfilt", "mclaren moon or the moon and moon moon shine and the moon but moon was good foxes too");- index(id, "8", "lowerfilt", "sonata");- index(id, "9", "lowerfilt", "The quick red fox jumped over the lazy big and large brown dogs.");- index(id, "10", "lowerfilt", "blue");- index(id, "12", "lowerfilt", "glue");- index(id, "13", "lowerfilt", "The quote red fox jumped over the lazy brown dogs.");- index(id, "14", "lowerfilt", "The quote red fox jumped over the lazy brown dogs.");- index(id, "15", "lowerfilt", "The fat red fox jumped over the lazy brown dogs.");- index(id, "16", "lowerfilt", "The slim red fox jumped over the lazy brown dogs.");- index(id, "17", "lowerfilt", "The quote red fox jumped moon over the lazy brown dogs moon. Of course moon. Foxes and moon come back to the foxes and moon");- index(id, "18", "lowerfilt", "The quote red fox jumped over the lazy brown dogs.");- index(id, "19", "lowerfilt", "The hose red fox jumped over the lazy brown dogs.");- index(id, "20", "lowerfilt", "The quote red fox jumped over the lazy brown dogs.");- index(id, "21", "lowerfilt", "The court red fox jumped over the lazy brown dogs.");- index(id, "22", "lowerfilt", "The quote red fox jumped over the lazy brown dogs.");- index(id, "23", "lowerfilt", "The quote red fox jumped over the lazy brown dogs.");- index(id, "24", "lowerfilt", "The file red fox jumped over the lazy brown dogs.");- index(id, "25", "lowerfilt", "rod fix");+ index(id, "1", "lowerfilt", "toyota", "lowerfilt1", "x");+ index(id, "2", "lowerfilt", "chevrolet", "lowerfilt1", "x");+ index(id, "3", "lowerfilt", "suzuki", "lowerfilt1", "x");+ index(id, "4", "lowerfilt", "ford", "lowerfilt1", "x");+ index(id, "5", "lowerfilt", "ferrari", "lowerfilt1", "x");+ index(id, "6", "lowerfilt", "jaguar", "lowerfilt1", "x");+ index(id, "7", "lowerfilt", "mclaren moon or the moon and moon moon shine and the moon but moon was good foxes too", "lowerfilt1", "x");+ index(id, "8", "lowerfilt", "sonata", "lowerfilt1", "x");+ index(id, "9", "lowerfilt", "The quick red fox jumped over the lazy big and large brown dogs.", "lowerfilt1", "x");+ index(id, "10", "lowerfilt", "blue", "lowerfilt1", "x");+ index(id, "12", "lowerfilt", "glue", "lowerfilt1", "x");+ index(id, "13", "lowerfilt", "The quote red fox jumped over the lazy brown dogs.", "lowerfilt1", "y");+ index(id, "14", "lowerfilt", "The quote red fox jumped over the lazy brown dogs.", "lowerfilt1", "y");+ index(id, "15", "lowerfilt", "The fat red fox jumped over the lazy brown dogs.", "lowerfilt1", "y");+ index(id, "16", "lowerfilt", "The slim red fox jumped over the lazy brown dogs.", "lowerfilt1", "y");+ index(id, "17", "lowerfilt", "The quote red fox jumped moon over the lazy brown dogs moon. Of course moon. Foxes and moon come back to the foxes and moon", "lowerfilt1", "y");+ index(id, "18", "lowerfilt", "The quote red fox jumped over the lazy brown dogs.", "lowerfilt1", "y");+ index(id, "19", "lowerfilt", "The hose red fox jumped over the lazy brown dogs.", "lowerfilt1", "y");+ index(id, "20", "lowerfilt", "The quote red fox jumped over the lazy brown dogs.", "lowerfilt1", "y");+ index(id, "21", "lowerfilt", "The court red fox jumped over the lazy brown dogs.", "lowerfilt1", "y");+ index(id, "22", "lowerfilt", "The quote red fox jumped over the lazy brown dogs.", "lowerfilt1", "y");+ index(id, "23", "lowerfilt", "The quote red fox jumped over the lazy brown dogs.", "lowerfilt1", "y");+ index(id, "24", "lowerfilt", "The file red fox jumped over the lazy brown dogs.", "lowerfilt1", "y");+ index(id, "25", "lowerfilt", "rod fix", "lowerfilt1", "y");commit();@@ -132,5 +137,30 @@ public class DistributedMLTComponentTest extends BaseDistributedSearchTestCase {MoreLikeThisParams.MIN_DOC_FREQ, 1, "sort", "id desc", "mlt", "true","mlt.fl", "lowerfilt", "qt", requestHandlerName, "shards.qt",requestHandlerName, "mlt.count", "20");++ // let's query by specifying multiple mlt.fl as comma-separated values+ QueryResponse response = query("q", "lowerfilt:moon", "fl", id, MoreLikeThisParams.MIN_TERM_FREQ, 2,+ MoreLikeThisParams.MIN_DOC_FREQ, 1, "sort", "id desc", "mlt", "true",+ "mlt.fl", "lowerfilt1,lowerfilt", "qt", requestHandlerName, "shards.qt",+ requestHandlerName, "mlt.count", "20");+ NamedList<Object> moreLikeThis = (NamedList<Object>) response.getResponse().get("moreLikeThis");+ Map<String, Long> idVsMLTCount = new HashMap<>();+ for (Map.Entry<String, Object> entry : moreLikeThis) {+ SolrDocumentList docList = (SolrDocumentList) entry.getValue();+ idVsMLTCount.put(entry.getKey(), docList.getNumFound());+ }++ // let's query by specifying multiple mlt.fl as multiple request parameters+ response = query("q", "lowerfilt:moon", "fl", id, MoreLikeThisParams.MIN_TERM_FREQ, 2,+ MoreLikeThisParams.MIN_DOC_FREQ, 1, "sort", "id desc", "mlt", "true",+ "mlt.fl", "lowerfilt1", "mlt.fl", "lowerfilt", "qt", requestHandlerName, "shards.qt",+ requestHandlerName, "mlt.count", "20");+ moreLikeThis = (NamedList<Object>) response.getResponse().get("moreLikeThis");+ for (Map.Entry<String, Object> entry : moreLikeThis) {+ String key = entry.getKey();+ Long expected = idVsMLTCount.get(key);+ Long actual = ((SolrDocumentList) entry.getValue()).getNumFound();+ assertEquals("MLT mismatch for id=" + key, expected, actual);+ }}}diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/SolrExampleTests.java b/solr/solrj/src/test/org/apache/solr/client/solrj/SolrExampleTests.javaindex 9dacb4bfd52..cf0991b8d88 100644--- a/solr/solrj/src/test/org/apache/solr/client/solrj/SolrExampleTests.java+++ b/solr/solrj/src/test/org/apache/solr/client/solrj/SolrExampleTests.java@@ -1644,6 +1644,59 @@ abstract public class SolrExampleTests extends SolrExampleTestsBaseassertEquals("All requested fields were not returned", 4, response.getResults().get(0).getFieldNames().size());}+ @Test+ public void testMoreLikeThis() throws Exception {+ SolrClient client = getSolrClient();+ client.deleteByQuery("*:*");+ for (int i=0; i<20; i++) {+ SolrInputDocument doc = new SolrInputDocument();+ doc.addField("id", "testMoreLikeThis" + i);+ doc.addField("x_s", "x_" + i);+ doc.addField("y_s", "y_" + (i % 3));+ doc.addField("z_s", "z_" + i);+ client.add(doc);+ }+ client.commit();++ // test with mlt.fl having comma separated values+ SolrQuery q = new SolrQuery("*:*");+ q.setRows(20);+ q.setParam("mlt", "true");+ q.setParam("mlt.mintf", "0");+ q.setParam("mlt.count", "2");+ q.setParam("mlt.fl", "x_s,y_s,z_s");+ QueryResponse response = client.query(q);+ System.out.printf("Results: " + response.getResponse());+ assertEquals(20, response.getResults().getNumFound());+ NamedList<Object> moreLikeThis = (NamedList<Object>) response.getResponse().get("moreLikeThis");+ assertNotNull("MoreLikeThis response should not have been null", moreLikeThis);+ for (int i=0; i<20; i++) {+ String id = "testMoreLikeThis" + i;+ SolrDocumentList mltResp = (SolrDocumentList) moreLikeThis.get(id);+ assertNotNull("MoreLikeThis response for id=" + id + " should not be null", mltResp);+ assertTrue("MoreLikeThis response for id=" + id + " had numFound=0", mltResp.getNumFound() > 0);+ }++ // now test with multiple mlt.fl parameters+ q = new SolrQuery("*:*");+ q.setRows(20);+ q.setParam("mlt", "true");+ q.setParam("mlt.mintf", "0");+ q.setParam("mlt.count", "2");+ q.setParam("mlt.fl", "x_s", "y_s", "z_s");+ response = client.query(q);+ System.out.printf("Results: " + response.getResponse());+ assertEquals(20, response.getResults().getNumFound());+ moreLikeThis = (NamedList<Object>) response.getResponse().get("moreLikeThis");+ assertNotNull("MoreLikeThis response should not have been null", moreLikeThis);+ for (int i=0; i<20; i++) {+ String id = "testMoreLikeThis" + i;+ SolrDocumentList mltResp = (SolrDocumentList) moreLikeThis.get(id);+ assertNotNull("MoreLikeThis response for id=" + id + " should not be null", mltResp);+ assertTrue("MoreLikeThis response for id=" + id + " had numFound=0", mltResp.getNumFound() > 0);+ }+ }+/*** Depth first search of a SolrInputDocument looking for a decendent by id,* returns null if it's not a decendent