lucene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ans...@apache.org
Subject svn commit: r1636784 - in /lucene/dev/trunk: lucene/queries/src/java/org/apache/lucene/queries/mlt/MoreLikeThis.java solr/core/src/java/org/apache/solr/search/mlt/CloudMLTQParser.java solr/core/src/test/org/apache/solr/search/mlt/CloudMLTQParserTest.java
Date Wed, 05 Nov 2014 00:21:42 GMT
Author: anshum
Date: Wed Nov  5 00:21:42 2014
New Revision: 1636784

URL: http://svn.apache.org/r1636784
Log:
SOLR-6248: Fixing an exception in case of missing qf

Modified:
    lucene/dev/trunk/lucene/queries/src/java/org/apache/lucene/queries/mlt/MoreLikeThis.java
    lucene/dev/trunk/solr/core/src/java/org/apache/solr/search/mlt/CloudMLTQParser.java
    lucene/dev/trunk/solr/core/src/test/org/apache/solr/search/mlt/CloudMLTQParserTest.java

Modified: lucene/dev/trunk/lucene/queries/src/java/org/apache/lucene/queries/mlt/MoreLikeThis.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/queries/src/java/org/apache/lucene/queries/mlt/MoreLikeThis.java?rev=1636784&r1=1636783&r2=1636784&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/queries/src/java/org/apache/lucene/queries/mlt/MoreLikeThis.java
(original)
+++ lucene/dev/trunk/lucene/queries/src/java/org/apache/lucene/queries/mlt/MoreLikeThis.java
Wed Nov  5 00:21:42 2014
@@ -591,7 +591,7 @@ public final class MoreLikeThis {
    * @param filteredDocument Document with field values extracted for selected fields.
    * @return More Like This query for the passed document.
    */
-  public Query like(Map<String, ArrayList<String>> filteredDocument) throws IOException
{
+  public Query like(Map<String, Collection<Object>> filteredDocument) throws
IOException {
     if (fieldNames == null) {
       // gather list of valid fields from lucene
       Collection<String> fields = MultiFields.getIndexedFields(ir);
@@ -754,16 +754,16 @@ public final class MoreLikeThis {
   }
 
 
-  private PriorityQueue<ScoreTerm> retrieveTerms(Map<String, ArrayList<String>>
fields) throws 
+  private PriorityQueue<ScoreTerm> retrieveTerms(Map<String, Collection<Object>>
fields) throws 
       IOException {
     HashMap<String,Int> termFreqMap = new HashMap();
     for (String fieldName : fieldNames) {
 
       for (String field : fields.keySet()) {
-        ArrayList<String> fieldValues = fields.get(field);
-        for(String fieldValue:fieldValues) {
+        Collection<Object> fieldValues = fields.get(field);
+        for(Object fieldValue:fieldValues) {
           if (fieldValue != null) {
-            addTermFrequencies(new StringReader(fieldValue), termFreqMap,
+            addTermFrequencies(new StringReader(String.valueOf(fieldValue)), termFreqMap,
                 fieldName);
           }
         }

Modified: lucene/dev/trunk/solr/core/src/java/org/apache/solr/search/mlt/CloudMLTQParser.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/core/src/java/org/apache/solr/search/mlt/CloudMLTQParser.java?rev=1636784&r1=1636783&r2=1636784&view=diff
==============================================================================
--- lucene/dev/trunk/solr/core/src/java/org/apache/solr/search/mlt/CloudMLTQParser.java (original)
+++ lucene/dev/trunk/solr/core/src/java/org/apache/solr/search/mlt/CloudMLTQParser.java Wed
Nov  5 00:21:42 2014
@@ -27,11 +27,15 @@ import org.apache.solr.core.SolrCore;
 import org.apache.solr.request.SolrQueryRequest;
 import org.apache.solr.request.SolrQueryRequestBase;
 import org.apache.solr.response.SolrQueryResponse;
+import org.apache.solr.schema.SchemaField;
 import org.apache.solr.search.QParser;
 import org.apache.solr.search.QueryParsing;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -42,6 +46,8 @@ public class CloudMLTQParser extends QPa
     super(qstr, localParams, params, req);
   }
 
+  private static Logger log = LoggerFactory
+      .getLogger(CloudMLTQParser.class);
   public Query parse() {
     String id = localParams.get(QueryParsing.V);
     // Do a Real Time Get for the document
@@ -61,17 +67,27 @@ public class CloudMLTQParser extends QPa
     mlt.setAnalyzer(req.getSchema().getIndexAnalyzer());
 
     String[] qf = localParams.getParams("qf");
-    Map<String, ArrayList<String>> filteredDocument = new HashMap();
+    Map<String, Collection<Object>> filteredDocument = new HashMap();
 
     if (qf != null) {
       mlt.setFieldNames(qf);
       for (String field : qf) {
-        filteredDocument.put(field, (ArrayList<String>) doc.get(field));
+        filteredDocument.put(field, doc.getFieldValues(field));
       }
     } else {
+      Map<String, SchemaField> fields = req.getSchema().getFields();
+      ArrayList<String> fieldNames = new ArrayList();
       for (String field : doc.getFieldNames()) {
-        filteredDocument.put(field, (ArrayList<String>) doc.get(field));
+        // Only use fields that are stored and have an explicit analyzer.
+        // This makes sense as the query uses tf/idf/.. for query construction.
+        // We might want to relook and change this in the future though.
+        if(fields.get(field).stored() 
+            && fields.get(field).getType().isExplicitAnalyzer()) {
+          fieldNames.add(field);
+          filteredDocument.put(field, doc.getFieldValues(field));
+        }
       }
+      mlt.setFieldNames(fieldNames.toArray(new String[fieldNames.size()]));
     }
 
     try {

Modified: lucene/dev/trunk/solr/core/src/test/org/apache/solr/search/mlt/CloudMLTQParserTest.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/core/src/test/org/apache/solr/search/mlt/CloudMLTQParserTest.java?rev=1636784&r1=1636783&r2=1636784&view=diff
==============================================================================
--- lucene/dev/trunk/solr/core/src/test/org/apache/solr/search/mlt/CloudMLTQParserTest.java
(original)
+++ lucene/dev/trunk/solr/core/src/test/org/apache/solr/search/mlt/CloudMLTQParserTest.java
Wed Nov  5 00:21:42 2014
@@ -129,6 +129,20 @@ public class CloudMLTQParserTest extends
           compareParsedQueryStrings(expectedQueryString,
           actualParsedQueries.get(counter)));
     }
+
+    // Assert that {!mlt}id does not throw an exception i.e. implicitly, only fields that
are stored + have explicit
+    // analyzer are used for MLT Query construction.
+    params = new ModifiableSolrParams();
+    params.set(CommonParams.Q, "{!mlt}20");
+
+    queryResponse = queryServer(params);
+    solrDocuments = queryResponse.getResults();
+    expectedIds = new int[]{18, 23, 13, 14, 20, 22, 19, 21, 15, 16};
+    i = 0;
+    for (SolrDocument solrDocument : solrDocuments) {
+      actualIds[i++] =  Integer.valueOf(String.valueOf(solrDocument.getFieldValue("id")));
+    }
+    assertArrayEquals(expectedIds, actualIds);
   }
   
   private boolean compareParsedQueryStrings(String expected, String actual) {



Mime
View raw message