Author: ogrisel
Date: Wed May 18 17:43:08 2011
New Revision: 1124347
URL: http://svn.apache.org/viewvc?rev=1124347&view=rev
Log:
STANBOL-202: extend SolrYard to handle similarity queries
Added:
incubator/stanbol/trunk/entityhub/generic/servicesapi/src/main/java/org/apache/stanbol/entityhub/servicesapi/query/SimilarityConstraint.java
incubator/stanbol/trunk/entityhub/yard/solr/src/main/java/org/apache/stanbol/entityhub/yard/solr/impl/StreamQueryRequest.java
Removed:
incubator/stanbol/trunk/entityhub/generic/servicesapi/src/main/java/org/apache/stanbol/entityhub/servicesapi/query/MoreLikeThisQuery.java
Modified:
incubator/stanbol/trunk/entityhub/generic/core/src/main/java/org/apache/stanbol/entityhub/core/query/FieldQueryImpl.java
incubator/stanbol/trunk/entityhub/generic/servicesapi/src/main/java/org/apache/stanbol/entityhub/servicesapi/query/Constraint.java
incubator/stanbol/trunk/entityhub/generic/servicesapi/src/main/java/org/apache/stanbol/entityhub/servicesapi/query/FieldQuery.java
incubator/stanbol/trunk/entityhub/yard/solr/pom.xml
incubator/stanbol/trunk/entityhub/yard/solr/src/main/java/org/apache/stanbol/entityhub/yard/solr/impl/SolrQueryFactory.java
incubator/stanbol/trunk/entityhub/yard/solr/src/main/java/org/apache/stanbol/entityhub/yard/solr/impl/SolrYard.java
incubator/stanbol/trunk/entityhub/yard/solr/src/main/resources/solr/conf/entityhub/conf/solrconfig.xml
incubator/stanbol/trunk/entityhub/yard/solr/src/main/resources/solr/core/conf/schema.xml
incubator/stanbol/trunk/entityhub/yard/solr/src/main/resources/solr/core/conf/solrconfig.xml
incubator/stanbol/trunk/entityhub/yard/solr/src/test/java/org/apache/stanbol/entityhub/yard/solr/SolrYardTest.java
Modified:
incubator/stanbol/trunk/entityhub/generic/core/src/main/java/org/apache/stanbol/entityhub/core/query/FieldQueryImpl.java
URL:
http://svn.apache.org/viewvc/incubator/stanbol/trunk/entityhub/generic/core/src/main/java/org/apache/stanbol/entityhub/core/query/FieldQueryImpl.java?rev=1124347&r1=1124346&r2=1124347&view=diff
==============================================================================
---
incubator/stanbol/trunk/entityhub/generic/core/src/main/java/org/apache/stanbol/entityhub/core/query/FieldQueryImpl.java
(original)
+++
incubator/stanbol/trunk/entityhub/generic/core/src/main/java/org/apache/stanbol/entityhub/core/query/FieldQueryImpl.java
Wed May 18 17:43:08 2011
@@ -22,8 +22,8 @@ import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
-import java.util.Set;
import java.util.Map.Entry;
+import java.util.Set;
import org.apache.stanbol.entityhub.servicesapi.query.Constraint;
import org.apache.stanbol.entityhub.servicesapi.query.FieldQuery;
@@ -209,4 +209,5 @@ public class FieldQueryImpl implements C
return false;
}
}
+
}
Modified:
incubator/stanbol/trunk/entityhub/generic/servicesapi/src/main/java/org/apache/stanbol/entityhub/servicesapi/query/Constraint.java
URL:
http://svn.apache.org/viewvc/incubator/stanbol/trunk/entityhub/generic/servicesapi/src/main/java/org/apache/stanbol/entityhub/servicesapi/query/Constraint.java?rev=1124347&r1=1124346&r2=1124347&view=diff
==============================================================================
---
incubator/stanbol/trunk/entityhub/generic/servicesapi/src/main/java/org/apache/stanbol/entityhub/servicesapi/query/Constraint.java
(original)
+++
incubator/stanbol/trunk/entityhub/generic/servicesapi/src/main/java/org/apache/stanbol/entityhub/servicesapi/query/Constraint.java
Wed May 18 17:43:08 2011
@@ -48,8 +48,12 @@ public abstract class Constraint {
/**
* Constraints a field to have a lexical value
*/
- text
- //TODO: value Type for checking non lexical values
+ text,
+ /**
+ * Constraints a field to have a lexical value along with statistics
to be able to compute a
+ * similarity metric (e.g. using the MoreLikeThis Solr handler)
+ */
+ similarity
}
private final ConstraintType type;
protected Constraint(ConstraintType type){
Modified:
incubator/stanbol/trunk/entityhub/generic/servicesapi/src/main/java/org/apache/stanbol/entityhub/servicesapi/query/FieldQuery.java
URL:
http://svn.apache.org/viewvc/incubator/stanbol/trunk/entityhub/generic/servicesapi/src/main/java/org/apache/stanbol/entityhub/servicesapi/query/FieldQuery.java?rev=1124347&r1=1124346&r2=1124347&view=diff
==============================================================================
---
incubator/stanbol/trunk/entityhub/generic/servicesapi/src/main/java/org/apache/stanbol/entityhub/servicesapi/query/FieldQuery.java
(original)
+++
incubator/stanbol/trunk/entityhub/generic/servicesapi/src/main/java/org/apache/stanbol/entityhub/servicesapi/query/FieldQuery.java
Wed May 18 17:43:08 2011
@@ -100,7 +100,7 @@ public interface FieldQuery extends Quer
* @return the constraint or <code>null</code> if none is defined.
*/
Constraint getConstraint(String field);
-
+
/**
* Getter for the unmodifiable list of query elements for the given Path.
Use
* the add/remove constraint methods to change query elements for an path
Added:
incubator/stanbol/trunk/entityhub/generic/servicesapi/src/main/java/org/apache/stanbol/entityhub/servicesapi/query/SimilarityConstraint.java
URL:
http://svn.apache.org/viewvc/incubator/stanbol/trunk/entityhub/generic/servicesapi/src/main/java/org/apache/stanbol/entityhub/servicesapi/query/SimilarityConstraint.java?rev=1124347&view=auto
==============================================================================
---
incubator/stanbol/trunk/entityhub/generic/servicesapi/src/main/java/org/apache/stanbol/entityhub/servicesapi/query/SimilarityConstraint.java
(added)
+++
incubator/stanbol/trunk/entityhub/generic/servicesapi/src/main/java/org/apache/stanbol/entityhub/servicesapi/query/SimilarityConstraint.java
Wed May 18 17:43:08 2011
@@ -0,0 +1,39 @@
+package org.apache.stanbol.entityhub.servicesapi.query;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Ensure that results have fields that is contextually similar. The
implementation is typically based on a
+ * cosine similarity score a normalized vector space of term frequencies -
inverse document frequencies as
+ * done by the MoreLikeThis feature of Solr for instance.
+ *
+ * This type of constraint might not be supported by all the yard
implementations. If it is not supported it
+ * is just ignored.
+ */
+public class SimilarityConstraint extends Constraint {
+
+ protected final String context;
+
+ protected final List<String> additionalFields = new ArrayList<String>();
+
+ public SimilarityConstraint(String context) {
+ super(ConstraintType.similarity);
+ this.context = context;
+ }
+
+ public SimilarityConstraint(String context, List<String> additionalFields)
{
+ super(ConstraintType.similarity);
+ this.context = context;
+ this.additionalFields.addAll(additionalFields);
+ }
+
+ public List<String> getAdditionalFields() {
+ return additionalFields;
+ }
+
+ public String getContext() {
+ return context;
+ }
+
+}
Modified: incubator/stanbol/trunk/entityhub/yard/solr/pom.xml
URL:
http://svn.apache.org/viewvc/incubator/stanbol/trunk/entityhub/yard/solr/pom.xml?rev=1124347&r1=1124346&r2=1124347&view=diff
==============================================================================
--- incubator/stanbol/trunk/entityhub/yard/solr/pom.xml (original)
+++ incubator/stanbol/trunk/entityhub/yard/solr/pom.xml Wed May 18 17:43:08 2011
@@ -177,6 +177,10 @@
<artifactId>solr-commons-csv</artifactId>
</dependency>
<dependency>
+ <groupId>commons-lang</groupId>
+ <artifactId>commons-lang</artifactId>
+ </dependency>
+ <dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
</dependency>
Modified:
incubator/stanbol/trunk/entityhub/yard/solr/src/main/java/org/apache/stanbol/entityhub/yard/solr/impl/SolrQueryFactory.java
URL:
http://svn.apache.org/viewvc/incubator/stanbol/trunk/entityhub/yard/solr/src/main/java/org/apache/stanbol/entityhub/yard/solr/impl/SolrQueryFactory.java?rev=1124347&r1=1124346&r2=1124347&view=diff
==============================================================================
---
incubator/stanbol/trunk/entityhub/yard/solr/src/main/java/org/apache/stanbol/entityhub/yard/solr/impl/SolrQueryFactory.java
(original)
+++
incubator/stanbol/trunk/entityhub/yard/solr/src/main/java/org/apache/stanbol/entityhub/yard/solr/impl/SolrQueryFactory.java
Wed May 18 17:43:08 2011
@@ -24,18 +24,22 @@ import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
-import java.util.Set;
import java.util.Map.Entry;
+import java.util.Set;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.common.SolrDocument;
+import org.apache.solr.common.params.CommonParams;
+import org.apache.solr.common.params.MoreLikeThisParams;
import org.apache.stanbol.entityhub.servicesapi.model.Representation;
import org.apache.stanbol.entityhub.servicesapi.model.Text;
import org.apache.stanbol.entityhub.servicesapi.model.ValueFactory;
import org.apache.stanbol.entityhub.servicesapi.query.Constraint;
+import
org.apache.stanbol.entityhub.servicesapi.query.Constraint.ConstraintType;
import org.apache.stanbol.entityhub.servicesapi.query.FieldQuery;
import org.apache.stanbol.entityhub.servicesapi.query.Query;
import org.apache.stanbol.entityhub.servicesapi.query.RangeConstraint;
+import org.apache.stanbol.entityhub.servicesapi.query.SimilarityConstraint;
import org.apache.stanbol.entityhub.servicesapi.query.TextConstraint;
import org.apache.stanbol.entityhub.servicesapi.query.ValueConstraint;
import org.apache.stanbol.entityhub.yard.solr.defaults.IndexDataTypeEnum;
@@ -86,6 +90,7 @@ import org.slf4j.LoggerFactory;
*/
public class SolrQueryFactory {
+ public static final String MLT_QUERY_TYPE = "/" + MoreLikeThisParams.MLT;
/**
* Allows to limit the maximum Numbers of Query Results for any kind of
Query. For now it is set to 1024.
*/
@@ -145,22 +150,49 @@ public class SolrQueryFactory {
setSelected(query, fieldQuery.getSelectedFields(), select);
StringBuilder queryString = new StringBuilder();
for (Entry<String,Constraint> fieldConstraint : fieldQuery) {
- IndexConstraint indexConstraint =
createIndexConstraint(fieldConstraint);
- if (indexConstraint.isInvalid()) {
- log.warn(String
- .format(
- "Unable to create IndexConstraint for Constraint
%s (type: %s) and Field %s (Reosens: %s)",
- fieldConstraint.getValue(),
fieldConstraint.getValue().getType(),
- fieldConstraint.getKey(),
indexConstraint.getInvalidMessages()));
+ if (fieldConstraint.getValue().getType() ==
ConstraintType.similarity) {
+ // TODO: log make the FieldQuery ensure that there is no more
than one instead of similarity
+ // constraint per query
+ List<String> fields = Arrays.asList(fieldConstraint.getKey());
+ SimilarityConstraint simConstraint = (SimilarityConstraint)
fieldConstraint.getValue();
+ IndexValue indexValue =
indexValueFactory.createIndexValue(simConstraint.getContext());
+ fields.addAll(simConstraint.getAdditionalFields());
+ query.setQueryType(MLT_QUERY_TYPE);
+ query.set(MoreLikeThisParams.MATCH_INCLUDE, false);
+ query.set(MoreLikeThisParams.MIN_DOC_FREQ, 1);
+ query.set(MoreLikeThisParams.MIN_TERM_FREQ, 1);
+ query.set(MoreLikeThisParams.INTERESTING_TERMS, "details");
+
+ // TODO: right now we ignore the fields and fallback to the
hardcoded "_text" field
+ //Collection<String> mappedFields =
fieldMapper.getFieldNames(fields, indexValue);
+ //query.set(MoreLikeThisParams.SIMILARITY_FIELDS,
StringUtils.join(mappedFields, ","));
+ query.set(MoreLikeThisParams.SIMILARITY_FIELDS, "_text");
+ query.set(CommonParams.STREAM_BODY, indexValue.getValue());
} else {
- if (queryString.length() > 0) {
- queryString.append(" AND ");
+ IndexConstraint indexConstraint =
createIndexConstraint(fieldConstraint);
+ if (indexConstraint.isInvalid()) {
+ log.warn(String
+ .format(
+ "Unable to create IndexConstraint for
Constraint %s (type: %s) and Field %s (Reosens: %s)",
+ fieldConstraint.getValue(),
fieldConstraint.getValue().getType(),
+ fieldConstraint.getKey(),
indexConstraint.getInvalidMessages()));
+ } else {
+ if (queryString.length() > 0) {
+ queryString.append(" AND ");
+ }
+ indexConstraint.encode(queryString);
}
- indexConstraint.encode(queryString);
}
}
- log.info("QueryStirng: " + queryString.toString());
- query.setQuery(queryString.toString());
+ if (queryString.length() > 0) {
+ String qs = queryString.toString();
+ log.info("QueryString: " + qs);
+ if (MLT_QUERY_TYPE.equals(query.getQueryType())) {
+ query.set(CommonParams.FQ, qs);
+ } else {
+ query.setQuery(qs);
+ }
+ }
return query;
}
@@ -218,6 +250,9 @@ public class SolrQueryFactory {
case range:
initIndexConstraint(indexConstraint, (RangeConstraint)
fieldConstraint.getValue());
break;
+ case similarity:
+ // handled by the caller directly pass
+ break;
default:
indexConstraint.setInvalid(String.format("ConstraintType %s
not supported by!",
fieldConstraint.getValue().getType()));
Modified:
incubator/stanbol/trunk/entityhub/yard/solr/src/main/java/org/apache/stanbol/entityhub/yard/solr/impl/SolrYard.java
URL:
http://svn.apache.org/viewvc/incubator/stanbol/trunk/entityhub/yard/solr/src/main/java/org/apache/stanbol/entityhub/yard/solr/impl/SolrYard.java?rev=1124347&r1=1124346&r2=1124347&view=diff
==============================================================================
---
incubator/stanbol/trunk/entityhub/yard/solr/src/main/java/org/apache/stanbol/entityhub/yard/solr/impl/SolrYard.java
(original)
+++
incubator/stanbol/trunk/entityhub/yard/solr/src/main/java/org/apache/stanbol/entityhub/yard/solr/impl/SolrYard.java
Wed May 18 17:43:08 2011
@@ -114,9 +114,9 @@ import org.slf4j.LoggerFactory;
*
*/
@Component(metatype = true, configurationFactory = true, policy =
ConfigurationPolicy.REQUIRE, // the ID and
-
// SOLR_SERVER_LOCATION
-
// are
-
// required!
+// SOLR_SERVER_LOCATION
+// are
+// required!
specVersion = "1.1")
@Service
@Properties(value = {
@@ -479,8 +479,6 @@ public class SolrYard extends AbstractYa
private QueryResultList<Representation> find(final FieldQuery parsedQuery,
SELECT select) throws YardException {
log.debug("find " + parsedQuery);
long start = System.currentTimeMillis();
- SolrQuery query = solrQueryFactoy.parseFieldQuery(parsedQuery, select);
- long queryGeneration = System.currentTimeMillis();
final Set<String> selected;
if (select == SELECT.QUERY) {
// if query set the fields to add to the result Representations
@@ -491,17 +489,27 @@ public class SolrYard extends AbstractYa
// otherwise add all fields
selected = null;
}
- QueryResponse respone;
+
+ SolrQuery query = solrQueryFactoy.parseFieldQuery(parsedQuery, select);
+ long queryGeneration = System.currentTimeMillis();
+
+ QueryResponse response;
try {
- respone = server.query(query, METHOD.POST);
+ StreamQueryRequest request = new StreamQueryRequest(query);
+ response = request.process(server);
} catch (SolrServerException e) {
- throw new YardException("Error while performing Query on
SolrServer!", e);
+ if ("unknown handler: /mlt".equals(e.getCause().getMessage())) {
+ throw new YardException("Solr is missing '<requestHandler
name=\"/mlt\""
+ + " class=\"solr.MoreLikeThisHandler\"
startup=\"lazy\" />'"
+ + " in 'solrconfig.xml'", e);
+ }
+ throw new YardException("Error while performing Query on
SolrServer: " + query.getQuery(), e);
}
long queryTime = System.currentTimeMillis();
// return a queryResultList
QueryResultListImpl<Representation> resultList = new
QueryResultListImpl<Representation>(parsedQuery,
// by adapting SolrDocuments to Representations
- new
AdaptingIterator<SolrDocument,Representation>(respone.getResults().iterator(),
+ new
AdaptingIterator<SolrDocument,Representation>(response.getResults().iterator(),
// inline Adapter Implementation
new
AdaptingIterator.Adapter<SolrDocument,Representation>() {
@Override
Added:
incubator/stanbol/trunk/entityhub/yard/solr/src/main/java/org/apache/stanbol/entityhub/yard/solr/impl/StreamQueryRequest.java
URL:
http://svn.apache.org/viewvc/incubator/stanbol/trunk/entityhub/yard/solr/src/main/java/org/apache/stanbol/entityhub/yard/solr/impl/StreamQueryRequest.java?rev=1124347&view=auto
==============================================================================
---
incubator/stanbol/trunk/entityhub/yard/solr/src/main/java/org/apache/stanbol/entityhub/yard/solr/impl/StreamQueryRequest.java
(added)
+++
incubator/stanbol/trunk/entityhub/yard/solr/src/main/java/org/apache/stanbol/entityhub/yard/solr/impl/StreamQueryRequest.java
Wed May 18 17:43:08 2011
@@ -0,0 +1,39 @@
+package org.apache.stanbol.entityhub.yard.solr.impl;
+
+import java.util.Arrays;
+import java.util.Collection;
+
+import org.apache.commons.lang.StringUtils;
+import org.apache.solr.client.solrj.SolrQuery;
+import org.apache.solr.client.solrj.request.QueryRequest;
+import org.apache.solr.common.params.CommonParams;
+import org.apache.solr.common.util.ContentStream;
+import org.apache.solr.common.util.ContentStreamBase.StringStream;
+
+/**
+ * Extend the default Solr QueryRequest to make it possible to pass
MoreLikeThis the payload as a content
+ * stream.
+ *
+ * This is temporary solution waiting for a proper support of
MoreLikeThisHandler in SolrJ.
+ * https://issues.apache.org/jira/browse/SOLR-1085
+ */
+public class StreamQueryRequest extends QueryRequest {
+
+ private static final long serialVersionUID = 1L;
+
+ protected ContentStream contentStream;
+
+ public StreamQueryRequest(SolrQuery q) {
+ super(q, METHOD.POST);
+ String[] bodies = q.remove(CommonParams.STREAM_BODY);
+ if (bodies!= null && bodies.length > 0) {
+ String body = StringUtils.join(bodies, " ");
+ this.contentStream = new StringStream(body);
+ }
+ }
+
+ @Override
+ public Collection<ContentStream> getContentStreams() {
+ return Arrays.asList(contentStream);
+ }
+}
Modified:
incubator/stanbol/trunk/entityhub/yard/solr/src/main/resources/solr/conf/entityhub/conf/solrconfig.xml
URL:
http://svn.apache.org/viewvc/incubator/stanbol/trunk/entityhub/yard/solr/src/main/resources/solr/conf/entityhub/conf/solrconfig.xml?rev=1124347&r1=1124346&r2=1124347&view=diff
==============================================================================
---
incubator/stanbol/trunk/entityhub/yard/solr/src/main/resources/solr/conf/entityhub/conf/solrconfig.xml
(original)
+++
incubator/stanbol/trunk/entityhub/yard/solr/src/main/resources/solr/conf/entityhub/conf/solrconfig.xml
Wed May 18 17:43:08 2011
@@ -609,6 +609,8 @@
</lst>
</requestHandler>
+ <!-- Request Handler for similarity queries and topic classification -->
+ <requestHandler name="/mlt" class="solr.MoreLikeThisHandler" startup="lazy"
/>
<!--
Search components are registered to SolrCore and used by Search Handlers
Modified:
incubator/stanbol/trunk/entityhub/yard/solr/src/main/resources/solr/core/conf/schema.xml
URL:
http://svn.apache.org/viewvc/incubator/stanbol/trunk/entityhub/yard/solr/src/main/resources/solr/core/conf/schema.xml?rev=1124347&r1=1124346&r2=1124347&view=diff
==============================================================================
---
incubator/stanbol/trunk/entityhub/yard/solr/src/main/resources/solr/core/conf/schema.xml
(original)
+++
incubator/stanbol/trunk/entityhub/yard/solr/src/main/resources/solr/core/conf/schema.xml
Wed May 18 17:43:08 2011
@@ -267,7 +267,8 @@
(via copyField). This is used as default search field.
The type may be changed.
-->
- <field name="_text" type="textgen" indexed="true" stored="false"
multiValued="true"/>
+ <field name="_text" type="textgen" indexed="true" stored="false"
+ multiValued="true" termVectors="true"/>
<!--
used to store all references of the document (via copyField).
This field may be used to search for related entities.
@@ -400,7 +401,8 @@
This field need not to be stored. The type can be changed to alternatives
as described in the types section of this configuration.
-->
- <dynamicField name="_!@*" type="textgen" indexed="true" stored="false"
multiValued="true"/>
+ <dynamicField name="_!@*" type="textgen" indexed="true" stored="false"
+ multiValued="true" />
<!--
fields starting with "_config/" are used to store configurations about
how the
index was created within the index (e.g. used namespace prefixes).
Modified:
incubator/stanbol/trunk/entityhub/yard/solr/src/main/resources/solr/core/conf/solrconfig.xml
URL:
http://svn.apache.org/viewvc/incubator/stanbol/trunk/entityhub/yard/solr/src/main/resources/solr/core/conf/solrconfig.xml?rev=1124347&r1=1124346&r2=1124347&view=diff
==============================================================================
---
incubator/stanbol/trunk/entityhub/yard/solr/src/main/resources/solr/core/conf/solrconfig.xml
(original)
+++
incubator/stanbol/trunk/entityhub/yard/solr/src/main/resources/solr/core/conf/solrconfig.xml
Wed May 18 17:43:08 2011
@@ -609,6 +609,8 @@
</lst>
</requestHandler>
+ <!-- Request Handler for similarity queries and topic classification -->
+ <requestHandler name="/mlt" class="solr.MoreLikeThisHandler" startup="lazy"
/>
<!--
Search components are registered to SolrCore and used by Search Handlers
Modified:
incubator/stanbol/trunk/entityhub/yard/solr/src/test/java/org/apache/stanbol/entityhub/yard/solr/SolrYardTest.java
URL:
http://svn.apache.org/viewvc/incubator/stanbol/trunk/entityhub/yard/solr/src/test/java/org/apache/stanbol/entityhub/yard/solr/SolrYardTest.java?rev=1124347&r1=1124346&r2=1124347&view=diff
==============================================================================
---
incubator/stanbol/trunk/entityhub/yard/solr/src/test/java/org/apache/stanbol/entityhub/yard/solr/SolrYardTest.java
(original)
+++
incubator/stanbol/trunk/entityhub/yard/solr/src/test/java/org/apache/stanbol/entityhub/yard/solr/SolrYardTest.java
Wed May 18 17:43:08 2011
@@ -21,17 +21,19 @@ import static junit.framework.Assert.ass
import java.io.File;
import java.util.Arrays;
+import java.util.Iterator;
import org.apache.stanbol.entityhub.servicesapi.model.Representation;
import org.apache.stanbol.entityhub.servicesapi.query.FieldQuery;
import org.apache.stanbol.entityhub.servicesapi.query.QueryResultList;
+import org.apache.stanbol.entityhub.servicesapi.query.SimilarityConstraint;
import org.apache.stanbol.entityhub.servicesapi.query.TextConstraint;
import org.apache.stanbol.entityhub.servicesapi.yard.Yard;
import org.apache.stanbol.entityhub.servicesapi.yard.YardException;
import org.apache.stanbol.entityhub.test.yard.YardTest;
import org.apache.stanbol.entityhub.yard.solr.impl.SolrYard;
import org.apache.stanbol.entityhub.yard.solr.impl.SolrYardConfig;
-import org.junit.AfterClass;
+import org.junit.After;
import org.junit.BeforeClass;
import org.junit.Test;
import org.slf4j.Logger;
@@ -98,16 +100,16 @@ public class SolrYardTest extends YardTe
public void testSolrYardConfigInitWithNullID() {
new SolrYardConfig(null, TEST_SOLR_CORE_NAME);
}
-
+
@Test
public void testFieldQuery() throws YardException {
// NOTE: this does not test if the updated view of the representation
is
// stored, but only that the update method works correctly
Yard yard = getYard();
-
+
String id1 = "urn:yard.test.testFieldQuery:representation.id1";
String id2 = "urn:yard.test.testFieldQuery:representation.id2";
- String field = "urn:the.field:used.for.this.Test";
+ String field = "urn:the.field:used.for.testFieldQuery";
Representation test1 = create(id1, true);
Representation test2 = create(id2, true);
// change the representations to be sure to force an update even if the
@@ -139,14 +141,69 @@ public class SolrYardTest extends YardTe
assertEquals("This is the text content of a field with value2.",
result.getFirst(field));
}
+ @Test
+ public void testFieldQueryWithSimilarityConstraint() throws YardException {
+ // NOTE: this does not test if the updated view of the representation
is
+ // stored, but only that the update method works correctly
+ Yard yard = getYard();
+ String id1 =
"urn:yard.test.testFieldQueryWithSimilarityConstraint:representation.id1";
+ String id2 =
"urn:yard.test.testFieldQueryWithSimilarityConstraint:representation.id2";
+ String id3 =
"urn:yard.test.testFieldQueryWithSimilarityConstraint:representation.id3";
+ String similarityfield =
"urn:the.field:used.for.testFieldQueryWithSimilarityConstraint.similarity";
+ String filterfield =
"urn:the.field:used.for.testFieldQueryWithSimilarityConstraint.filter";
+ Representation test1 = create(id1, true);
+ Representation test2 = create(id2, true);
+ Representation test3 = create(id3, true);
+ // change the representations to be sure to force an update even if the
+ // implementation checks for changes before updating a representation
+ test1.add(similarityfield, "aaaa aaaa aaaa bbbb bbbb cccc cccc dddd
dddd");
+ test1.add(filterfield, "Some text content");
+
+ test2.add(similarityfield, "aaaa bbbb bbbb bbbb bbbb eeee");
+ test2.add(filterfield, "Some other content");
+
+ test3.add(similarityfield, "eeee eeee ffff gggg");
+ test3.add(filterfield, "Different content");
+
+ Iterable<Representation> updatedIterable =
yard.update(Arrays.asList(test1, test2, test3));
+ assertNotNull(updatedIterable);
+
+ // Perform a first similarity query that looks a lot like the first
document
+ FieldQuery query = yard.getQueryFactory().createFieldQuery();
+ query.setConstraint(similarityfield, new SimilarityConstraint("aaaa
aaaa aaaa aaaa zzzz yyyy"));
+ QueryResultList<Representation> results = yard.find(query);
+ assertEquals(2, results.size());
+ Iterator<Representation> it = results.iterator();
+ Representation first = it.next();
+
assertEquals("urn:yard.test.testFieldQueryWithSimilarityConstraint:representation.id1",
first.getId());
+ // assertEquals(0.99,
first.getFirst("http://www.iks-project.eu/ontology/rick/query/score"));
+
+ Representation second = it.next();
+
assertEquals("urn:yard.test.testFieldQueryWithSimilarityConstraint:representation.id2",
+ second.getId());
+ // assertEquals(0.80,
first.getFirst("http://www.iks-project.eu/ontology/rick/query/score"));
+
+ // combine similarity with traditional filtering
+ query = yard.getQueryFactory().createFieldQuery();
+ query.setConstraint(similarityfield, new SimilarityConstraint("aaaa
aaaa aaaa aaaa zzzz yyyy"));
+ query.setConstraint(filterfield, new TextConstraint("other"));
+ results = yard.find(query);
+ assertEquals(1, results.size());
+ it = results.iterator();
+ first = it.next();
+
assertEquals("urn:yard.test.testFieldQueryWithSimilarityConstraint:representation.id2",
first.getId());
+ }
+
/**
* This Method removes all Representations create via {@link #create()} or
* {@link #create(String, boolean)} from the tested {@link Yard}. It also
removes all Representations
* there ID was manually added to the {@link #representationIds} list.
*/
- @AfterClass
- public static final void clearUpRepresentations() throws YardException {
- yard.remove(representationIds);
+ @After
+ public final void clearUpRepresentations() throws YardException {
+ if (!representationIds.isEmpty()) {
+ yard.remove(representationIds);
+ }
}
}