Author: catholicon
Date: Sun Nov 22 23:28:45 2015
New Revision: 1715716
URL: http://svn.apache.org/viewvc?rev=1715716&view=rev
Log:
OAK-3509 Lucene suggestion results should have 1 row per suggestion with
appropriate column names
* Suggestions and spellcheck results returned per row with weight coming up as
jcr:score
* Adjusted test cases accordingly (no extra tests as multiple
suggestion/spellcheck were there)
Modified:
jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LucenePropertyIndex.java
jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/jcr/query/SpellcheckTest.java
jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/jcr/query/SuggestTest.java
jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexSuggestionTest.java
jackrabbit/oak/trunk/oak-lucene/src/test/resources/org/apache/jackrabbit/oak/query/sql1.txt
Modified:
jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LucenePropertyIndex.java
URL:
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LucenePropertyIndex.java?rev=1715716&r1=1715715&r2=1715716&view=diff
==============================================================================
---
jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LucenePropertyIndex.java
(original)
+++
jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LucenePropertyIndex.java
Sun Nov 22 23:28:45 2015
@@ -25,7 +25,6 @@ import javax.jcr.PropertyType;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
-import java.util.Collections;
import java.util.Deque;
import java.util.Iterator;
import java.util.List;
@@ -411,7 +410,6 @@ public class LucenePropertyIndex impleme
SuggestWord[] suggestWords =
SpellcheckHelper.getSpellcheck(spellcheckQuery);
// ACL filter spellchecks
- Collection<String> suggestedWords = new
ArrayList<String>(suggestWords.length);
QueryParser qp = new QueryParser(Version.LUCENE_47,
FieldNames.FULLTEXT, indexNode.getDefinition().getAnalyzer());
for (SuggestWord suggestion : suggestWords) {
Query query =
qp.createPhraseQuery(FieldNames.FULLTEXT, suggestion.string);
@@ -420,14 +418,13 @@ public class LucenePropertyIndex impleme
for (ScoreDoc doc : topDocs.scoreDocs) {
Document retrievedDoc =
searcher.doc(doc.doc);
if
(filter.isAccessible(retrievedDoc.get(FieldNames.PATH))) {
- suggestedWords.add(suggestion.string);
+ queue.add(new
LuceneResultRow(suggestion.string));
break;
}
}
}
}
- queue.add(new LuceneResultRow(suggestedWords));
noDocs = true;
} else if (luceneRequestFacade.getLuceneRequest()
instanceof SuggestHelper.SuggestQuery) {
SuggestHelper.SuggestQuery suggestQuery =
(SuggestHelper.SuggestQuery) luceneRequestFacade.getLuceneRequest();
@@ -435,7 +432,6 @@ public class LucenePropertyIndex impleme
List<Lookup.LookupResult> lookupResults =
SuggestHelper.getSuggestions(suggestQuery);
// ACL filter suggestions
- Collection<String> suggestedWords = new
ArrayList<String>(lookupResults.size());
QueryParser qp = new QueryParser(Version.LUCENE_47,
FieldNames.SUGGEST, indexNode.getDefinition().getAnalyzer());
for (Lookup.LookupResult suggestion : lookupResults) {
Query query =
qp.createPhraseQuery(FieldNames.SUGGEST, suggestion.key.toString());
@@ -444,14 +440,13 @@ public class LucenePropertyIndex impleme
for (ScoreDoc doc : topDocs.scoreDocs) {
Document retrievedDoc =
searcher.doc(doc.doc);
if
(filter.isAccessible(retrievedDoc.get(FieldNames.PATH))) {
- suggestedWords.add("{term=" +
suggestion.key + ",weight=" + suggestion.value + "}");
+ queue.add(new
LuceneResultRow(suggestion.key.toString(), suggestion.value));
break;
}
}
}
}
- queue.add(new LuceneResultRow(suggestedWords));
noDocs = true;
}
} catch (IOException e) {
@@ -1323,7 +1318,7 @@ public class LucenePropertyIndex impleme
static class LuceneResultRow {
final String path;
final double score;
- final Iterable<String> suggestWords;
+ final String suggestion;
final boolean isVirutal;
final String excerpt;
@@ -1332,17 +1327,21 @@ public class LucenePropertyIndex impleme
this.isVirutal = false;
this.path = path;
this.score = score;
- this.suggestWords = Collections.emptySet();
+ this.suggestion = null;
}
- LuceneResultRow(Iterable<String> suggestWords) {
+ LuceneResultRow(String suggestion, long weight) {
this.isVirutal = true;
this.path = "/";
- this.score = 1.0d;
- this.suggestWords = suggestWords;
+ this.score = weight;
+ this.suggestion = suggestion;
this.excerpt = null;
}
+ LuceneResultRow(String suggestion) {
+ this(suggestion, 1);
+ }
+
@Override
public String toString() {
return String.format("%s (%1.2f)", path, score);
@@ -1424,7 +1423,7 @@ public class LucenePropertyIndex impleme
return PropertyValues.newDouble(currentRow.score);
}
if (QueryImpl.REP_SPELLCHECK.equals(columnName) ||
QueryImpl.REP_SUGGEST.equals(columnName)) {
- return
PropertyValues.newString(Iterables.toString(currentRow.suggestWords));
+ return PropertyValues.newString(currentRow.suggestion);
}
if (QueryImpl.REP_EXCERPT.equals(columnName)) {
return PropertyValues.newString(currentRow.excerpt);
Modified:
jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/jcr/query/SpellcheckTest.java
URL:
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/jcr/query/SpellcheckTest.java?rev=1715716&r1=1715715&r2=1715716&view=diff
==============================================================================
---
jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/jcr/query/SpellcheckTest.java
(original)
+++
jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/jcr/query/SpellcheckTest.java
Sun Nov 22 23:28:45 2015
@@ -27,8 +27,11 @@ import javax.jcr.query.QueryResult;
import javax.jcr.query.Row;
import javax.jcr.query.RowIterator;
+import com.google.common.collect.Lists;
import org.apache.jackrabbit.core.query.AbstractQueryTest;
+import java.util.List;
+
/**
* Tests the spellcheck support.
*/
@@ -45,9 +48,9 @@ public class SpellcheckTest extends Abst
String sql = "SELECT [rep:spellcheck()] FROM nt:base WHERE [jcr:path]
= '/' AND SPELLCHECK('helo')";
Query q = qm.createQuery(sql, Query.SQL);
- String result = getResult(q.execute(), "rep:spellcheck()");
+ List<String> result = getResult(q.execute(), "rep:spellcheck()");
assertNotNull(result);
- assertEquals("[hello, hold]", result);
+ assertEquals("[hello, hold]", result.toString());
}
public void testSpellcheckXPath() throws Exception {
@@ -61,9 +64,9 @@ public class SpellcheckTest extends Abst
String xpath = "/jcr:root[rep:spellcheck('helo')]/(rep:spellcheck())";
Query q = qm.createQuery(xpath, Query.XPATH);
- String result = getResult(q.execute(), "rep:spellcheck()");
+ List<String> result = getResult(q.execute(), "rep:spellcheck()");
assertNotNull(result);
- assertEquals("[hello, hold]", result);
+ assertEquals("[hello, hold]", result.toString());
}
public void testSpellcheckMultipleWords() throws Exception {
@@ -81,22 +84,19 @@ public class SpellcheckTest extends Abst
String xpath = "/jcr:root[rep:spellcheck('votin in
ontari')]/(rep:spellcheck())";
Query q = qm.createQuery(xpath, Query.XPATH);
- String result = getResult(q.execute(), "rep:spellcheck()");
+ List<String> result = getResult(q.execute(), "rep:spellcheck()");
assertNotNull(result);
- assertEquals("[voting in ontario]", result);
+ assertEquals("[voting in ontario]", result.toString());
}
- static String getResult(QueryResult result, String propertyName) throws
RepositoryException {
- StringBuilder buff = new StringBuilder();
+ static List<String> getResult(QueryResult result, String propertyName)
throws RepositoryException {
+ List<String> results = Lists.newArrayList();
RowIterator it = result.getRows();
while (it.hasNext()) {
- if (buff.length() > 0) {
- buff.append(", ");
- }
Row row = it.nextRow();
- buff.append(row.getValue(propertyName).getString());
+ results.add(row.getValue(propertyName).getString());
}
- return buff.toString();
+ return results;
}
}
\ No newline at end of file
Modified:
jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/jcr/query/SuggestTest.java
URL:
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/jcr/query/SuggestTest.java?rev=1715716&r1=1715715&r2=1715716&view=diff
==============================================================================
---
jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/jcr/query/SuggestTest.java
(original)
+++
jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/jcr/query/SuggestTest.java
Sun Nov 22 23:28:45 2015
@@ -27,10 +27,13 @@ import javax.jcr.query.QueryResult;
import javax.jcr.query.Row;
import javax.jcr.query.RowIterator;
+import com.google.common.collect.Lists;
import org.apache.jackrabbit.core.query.AbstractQueryTest;
import org.junit.After;
import org.junit.Before;
+import java.util.List;
+
/**
* Tests the suggest support.
*/
@@ -64,10 +67,10 @@ public class SuggestTest extends Abstrac
String sql = "SELECT [rep:suggest()] FROM nt:base WHERE [jcr:path] =
'/' AND SUGGEST('in 201')";
Query q = qm.createQuery(sql, Query.SQL);
- String result = getResult(q.execute(), "rep:suggest()");
+ List<String> result = getResult(q.execute(), "rep:suggest()");
assertNotNull(result);
- assertTrue(result.contains("[{term=in 2015 a red fox is still a
fox,weight="));
- assertTrue(result.contains("{term=in 2015 my fox is red, like mike's
fox and john's fox,weight="));
+ assertTrue(result.contains("in 2015 a red fox is still a fox"));
+ assertTrue(result.contains("in 2015 my fox is red, like mike's fox and
john's fox"));
}
public void testSuggestXPath() throws Exception {
@@ -81,10 +84,10 @@ public class SuggestTest extends Abstrac
String xpath = "/jcr:root[rep:suggest('in 201')]/(rep:suggest())";
Query q = qm.createQuery(xpath, Query.XPATH);
- String result = getResult(q.execute(), "rep:suggest()");
+ List<String> result = getResult(q.execute(), "rep:suggest()");
assertNotNull(result);
- assertTrue(result.contains("[{term=in 2015 a red fox is still a
fox,weight="));
- assertTrue(result.contains("{term=in 2015 my fox is red, like mike's
fox and john's fox,weight="));
+ assertTrue(result.contains("in 2015 a red fox is still a fox"));
+ assertTrue(result.contains("in 2015 my fox is red, like mike's fox and
john's fox"));
}
public void testNoSuggestions() throws Exception {
@@ -98,22 +101,19 @@ public class SuggestTest extends Abstrac
String sql = "SELECT [rep:suggest()] FROM nt:base WHERE [jcr:path] =
'/' AND SUGGEST('blablabla')";
Query q = qm.createQuery(sql, Query.SQL);
- String result = getResult(q.execute(), "rep:suggest()");
- assertNotNull(result);
- assertEquals("[]", result);
+ List<String> results = getResult(q.execute(), "rep:suggest()");
+ assertNotNull(results);
+ assertEquals(0, results.size());
}
- static String getResult(QueryResult result, String propertyName) throws
RepositoryException {
- StringBuilder buff = new StringBuilder();
+ static List<String> getResult(QueryResult result, String propertyName)
throws RepositoryException {
+ List<String> results = Lists.newArrayList();
RowIterator it = result.getRows();
while (it.hasNext()) {
- if (buff.length() > 0) {
- buff.append(", ");
- }
Row row = it.nextRow();
- buff.append(row.getValue(propertyName).getString());
+ results.add(row.getValue(propertyName).getString());
}
- return buff.toString();
+ return results;
}
}
\ No newline at end of file
Modified:
jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexSuggestionTest.java
URL:
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexSuggestionTest.java?rev=1715716&r1=1715715&r2=1715716&view=diff
==============================================================================
---
jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexSuggestionTest.java
(original)
+++
jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexSuggestionTest.java
Sun Nov 22 23:28:45 2015
@@ -119,8 +119,10 @@ public class LuceneIndexSuggestionTest {
throws Exception {
createSuggestIndex("lucene-suggest", indexNodeType, indexPropName,
addFullText);
- Node indexedNode = root.addNode("indexedNode", queryNodeType);
- indexedNode.setProperty(indexPropName, indexPropValue);
+ Node indexedNode = root.addNode("indexedNode1", queryNodeType);
+ indexedNode.setProperty(indexPropName, indexPropValue + " 1");
+ indexedNode = root.addNode("indexedNode2", queryNodeType);
+ indexedNode.setProperty(indexPropName, indexPropValue + " 2");
if (useUserSession) {
session.getUserManager().createUser(TEST_USER_NAME,
TEST_USER_NAME);
}
@@ -140,51 +142,20 @@ public class LuceneIndexSuggestionTest {
RowIterator rows = result.getRows();
String value = null;
- if (rows.hasNext()) {
+ while (rows.hasNext()) {
Row firstRow = rows.nextRow();
value = firstRow.getValue("suggestion").getString();
}
- Suggestion suggestion = Suggestion.fromString(value);
if (shouldSuggest) {
- assertNotNull("There should be some suggestion",
suggestion.getSuggestion());
+ assertNotNull("There should be some suggestion", value);
} else {
- assertNull("There shouldn't be any suggestion",
suggestion.getSuggestion());
+ assertNull("There shouldn't be any suggestion", value);
}
}
private String createSuggestQuery(String nodeTypeName, String suggestFor) {
- return "SELECT [rep:suggest()] as suggestion FROM [" + nodeTypeName +
"] WHERE suggest('" + suggestFor + "')";
- }
-
- static class Suggestion {
- private final long weight;
- private final String suggestion;
- Suggestion(String suggestion, long weight) {
- this.suggestion = suggestion;
- this.weight = weight;
- }
-
- long getWeight() {
- return weight;
- }
-
- String getSuggestion() {
- return suggestion;
- }
-
- static Suggestion fromString(String suggestionResultStr) {
- if (suggestionResultStr==null || "".equals(suggestionResultStr) ||
"[]".equals(suggestionResultStr)) {
- return new Suggestion(null, -1);
- }
-
- String [] parts = suggestionResultStr.split(",weight=", 2);
- int endWeightIdx = parts[1].lastIndexOf("}");
- long weight = Long.parseLong(parts[1].substring(0, endWeightIdx));
- String suggestion = parts[0].split("=", 2)[1];
-
- return new Suggestion(suggestion, weight);
- }
+ return "SELECT [rep:suggest()] as suggestion, [jcr:score] as score
FROM [" + nodeTypeName + "] WHERE suggest('" + suggestFor + "')";
}
//OAK-3157
@@ -239,6 +210,21 @@ public class LuceneIndexSuggestionTest {
final String nodeType = "oak:Unstructured";
final String indexPropName = "description";
final String indexPropValue = "this is just a sample text which should
get some response in suggestions";
+ final String suggestQueryText = "th";
+ final boolean shouldSuggest = true;
+
+ checkSuggestions(nodeType,
+ indexPropName, indexPropValue,
+ true, false,
+ suggestQueryText, shouldSuggest);
+ }
+
+ //OAK-3509
+ @Test
+ public void testMultipleSuggestions() throws Exception {
+ final String nodeType = "oak:Unstructured";
+ final String indexPropName = "description";
+ final String indexPropValue = "this is just a sample text which should
get some response in suggestions";
final String suggestQueryText = "th";
final boolean shouldSuggest = true;
Modified:
jackrabbit/oak/trunk/oak-lucene/src/test/resources/org/apache/jackrabbit/oak/query/sql1.txt
URL:
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-lucene/src/test/resources/org/apache/jackrabbit/oak/query/sql1.txt?rev=1715716&r1=1715715&r2=1715716&view=diff
==============================================================================
---
jackrabbit/oak/trunk/oak-lucene/src/test/resources/org/apache/jackrabbit/oak/query/sql1.txt
(original)
+++
jackrabbit/oak/trunk/oak-lucene/src/test/resources/org/apache/jackrabbit/oak/query/sql1.txt
Sun Nov 22 23:28:45 2015
@@ -27,7 +27,7 @@
# sql-1 query (nt:unstructured needs to be escaped in sql-2)
sql1 SELECT [rep:spellcheck()] FROM nt:base WHERE [jcr:path] = '/' AND
SPELLCHECK('jackrabit')
-[jackrabbit], /, 1.0
+jackrabbit, /, 1.0
sql1 select prop1 from nt:unstructured where prop1 is not null order by prop1
asc