You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@stanbol.apache.org by rw...@apache.org on 2011/07/28 15:33:45 UTC
svn commit: r1151837 [2/2] - in /incubator/stanbol/trunk:
entityhub/generic/core/src/main/java/org/apache/stanbol/entityhub/core/impl/
entityhub/generic/core/src/main/java/org/apache/stanbol/entityhub/core/query/
entityhub/generic/servicesapi/src/main/...
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=1151837&r1=1151836&r2=1151837&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 Thu Jul 28 13:33:41 2011
@@ -22,6 +22,7 @@ import java.util.Collection;
import java.util.Collections;
import java.util.EnumMap;
import java.util.HashMap;
+import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@@ -37,10 +38,12 @@ import org.apache.stanbol.entityhub.core
import org.apache.stanbol.entityhub.core.query.DefaultQueryFactory;
import org.apache.stanbol.entityhub.servicesapi.model.Representation;
import org.apache.stanbol.entityhub.servicesapi.model.ValueFactory;
+import org.apache.stanbol.entityhub.servicesapi.model.rdf.RdfResourceEnum;
import org.apache.stanbol.entityhub.servicesapi.query.Constraint;
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.ReferenceConstraint;
import org.apache.stanbol.entityhub.servicesapi.query.SimilarityConstraint;
import org.apache.stanbol.entityhub.servicesapi.query.TextConstraint;
import org.apache.stanbol.entityhub.servicesapi.query.ValueConstraint;
@@ -147,11 +150,20 @@ public class SolrQueryFactory {
QUERY,
ALL
}
-
+ /**
+ * Converts the field query to a SolrQuery. In addition changes the parsed
+ * FieldQuery (e.g. removing unsupported features, setting defaults for
+ * missing parameters)
+ * @param fieldQuery the field query (will be modified to reflect the query
+ * as executed)
+ * @param select the SELECT mode
+ * @return the SolrQuery
+ */
public SolrQuery parseFieldQuery(FieldQuery fieldQuery, SELECT select) {
SolrQuery query = initSolrQuery(fieldQuery);
- setSelected(query, fieldQuery.getSelectedFields(), select);
+ setSelected(query, fieldQuery, select);
StringBuilder queryString = new StringBuilder();
+ Map<String,Constraint> processedFieldConstraints = new HashMap<String,Constraint>();
for (Entry<String,Constraint> fieldConstraint : fieldQuery) {
if (fieldConstraint.getValue().getType() == ConstraintType.similarity) {
// TODO: log make the FieldQuery ensure that there is no more than one instead of similarity
@@ -174,19 +186,28 @@ public class SolrQueryFactory {
} else {
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()));
+ 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);
+ //set the constraint (may be changed because of some unsupported features)
+ processedFieldConstraints.put(fieldConstraint.getKey(),
+ indexConstraint.getFieldQueryConstraint() == null ? //if null
+ fieldConstraint.getValue() : //assume no change and add the parsed one
+ indexConstraint.getFieldQueryConstraint()); //add the changed version
}
}
}
+ //set the constraints as processed to the parsed query
+ fieldQuery.removeAllConstraints();
+ for(Entry<String,Constraint> constraint : processedFieldConstraints.entrySet()){
+ fieldQuery.setConstraint(constraint.getKey(), constraint.getValue());
+ }
if (queryString.length() > 0) {
String qs = queryString.toString();
log.info("QueryString: " + qs);
@@ -210,13 +231,14 @@ public class SolrQueryFactory {
* @param query
* @param selected
*/
- private void setSelected(SolrQuery query, Collection<String> selected, SELECT select) {
+ private void setSelected(SolrQuery query, FieldQuery fieldQuery, SELECT select) {
switch (select) {
case ID:
query.addField(fieldMapper.getDocumentIdField());
+ fieldQuery.removeAllSelectedFields();
break;
case QUERY:
- if (selected.isEmpty()) {
+ if (fieldQuery.getSelectedFields().isEmpty()) {
query.addField(fieldMapper.getDocumentIdField());
} else {
query.addField("*");
@@ -239,6 +261,7 @@ public class SolrQueryFactory {
}
// add the select for the score
query.addField("score");
+ fieldQuery.addSelectedField(RdfResourceEnum.resultScore.getUri());
}
private IndexConstraint createIndexConstraint(Entry<String,Constraint> fieldConstraint) {
@@ -344,12 +367,14 @@ public class SolrQueryFactory {
valueConstraint.getDataTypes()));
} else {
// first process the parsed dataTypes to get the supported types
- Collection<IndexDataType> indexDataTypes = new ArrayList<IndexDataType>();
+ List<IndexDataType> indexDataTypes = new ArrayList<IndexDataType>();
+ List<String> acceptedDataTypes = new ArrayList<String>();
if (valueConstraint.getDataTypes() != null) {
for (String dataType : valueConstraint.getDataTypes()) {
IndexDataTypeEnum indexDataTypeEnumEntry = IndexDataTypeEnum.forUri(dataType);
if (indexDataTypeEnumEntry != null) {
indexDataTypes.add(indexDataTypeEnumEntry.getIndexType());
+ acceptedDataTypes.add(dataType);
} else {
// TODO: Add possibility to add warnings to indexConstraints
log.warn("A Datatype parsed for a ValueConstraint is not " +
@@ -358,35 +383,33 @@ public class SolrQueryFactory {
}
}
}
- IndexDataType indexDataType;
- if(indexDataTypes.isEmpty()){
- indexDataType = null;
- } else {
- Iterator<IndexDataType> it = indexDataTypes.iterator();
- indexDataType = it.next();
- if(it.hasNext()){
- log.warn("Only a single DataType is supported for ValueConstraints" +
- "used: {} ignored {}", indexDataType,
- ModelUtils.asCollection(it));
- }
- }
IndexValue constraintValue;
- if (indexDataType == null) { // if no supported types are present
+ if(indexDataTypes.isEmpty()){ // if no supported types are present
// get the dataType based on the type of the value
try {
constraintValue = indexValueFactory.createIndexValue(valueConstraint.getValue());
} catch (NoConverterException e) {
// if not found use the toString() and string as type
- indexDataType = IndexDataTypeEnum.STR.getIndexType();
log.warn(String
.format(
"Unable to create IndexValue for value %s (type: %s). Create IndexValue manually by using the first parsed IndexDataType %s",
valueConstraint.getValue(), valueConstraint.getValue().getClass(),
- indexDataType));
- constraintValue = new IndexValue(valueConstraint.getValue().toString(), indexDataType);
+ IndexDataTypeEnum.STR.getIndexType()));
+ constraintValue = new IndexValue(valueConstraint.getValue().toString(),
+ IndexDataTypeEnum.STR.getIndexType());
+ }
+ acceptedDataTypes.add(constraintValue.getType().getId());
+ } else {
+ constraintValue = new IndexValue(valueConstraint.getValue().toString(), indexDataTypes.get(0));
+ //we support only a single dataType ...
+ // ... therefore remove additional data types from the ValueConstraint
+ if(indexDataTypes.size() > 1){
+ log.warn("Only a single DataType is supported for ValueConstraints!");
+ while(acceptedDataTypes.size()>1){
+ String ignored = acceptedDataTypes.remove(acceptedDataTypes.size()-1);
+ log.warn(" > ignore parsed dataType {}",ignored);
+ }
}
- } else { // one or more supported dataTypes are present
- constraintValue = new IndexValue(valueConstraint.getValue().toString(), indexDataType);
}
indexConstraint.setFieldConstraint(IndexConstraintTypeEnum.DATATYPE, constraintValue);
if(IndexDataTypeEnum.TXT.getIndexType().equals(constraintValue.getType())){
@@ -396,6 +419,13 @@ public class SolrQueryFactory {
Collections.singleton(constraintValue.getLanguage()));
}
indexConstraint.setFieldConstraint(IndexConstraintTypeEnum.EQ, constraintValue);
+ //update this constraint!
+ if(valueConstraint instanceof ReferenceConstraint){
+ indexConstraint.setFieldQueryConstraint(valueConstraint);
+ } else {
+ indexConstraint.setFieldQueryConstraint(
+ new ValueConstraint(valueConstraint.getValue(), acceptedDataTypes));
+ }
}
}
@@ -416,12 +446,14 @@ public class SolrQueryFactory {
log.warn(String.format(
"Parsed Number of QueryResults %d is greater than the allowed maximum of %d!",
entityhubQuery.getLimit(), MAX_QUERY_RESULTS));
+ entityhubQuery.setLimit(MAX_QUERY_RESULTS);
}
} else {
// maybe remove that to prevent to many results! But for now I would
// rather like to have a default value within the FieldQuery!
// e.g. set by the FieldQueryFactory when creating new queries!
- query.setRows(MAX_QUERY_RESULTS);
+ query.setRows(DEFAULT_QUERY_RESULTS);
+ entityhubQuery.setLimit(DEFAULT_QUERY_RESULTS);
}
return query;
}
@@ -518,6 +550,7 @@ public class SolrQueryFactory {
private final Map<IndexConstraintTypeEnum,Object> fieldConstraints = new EnumMap<IndexConstraintTypeEnum,Object>(
IndexConstraintTypeEnum.class);
private List<String> invalidMessages = new ArrayList<String>();
+ private Constraint fieldQueryConstraint;
/**
* Creates a Field Term for the parsed path
@@ -564,6 +597,24 @@ public class SolrQueryFactory {
public List<String> getInvalidMessages() {
return invalidMessages;
}
+ /**
+ * Getter for the (possible modified against the parsed constrained)
+ * version of the FieldQuery {@link Constraint}
+ * @return the Constraint or <code>null</code> if
+ * <code>{@link #isInvalid()} == false</code>
+ */
+ public final Constraint getFieldQueryConstraint() {
+ return fieldQueryConstraint;
+ }
+ /**
+ /**
+ * Getter for the (possible modified against the parsed constrained)
+ * version of the FieldQuery {@link Constraint}
+ * @param fieldQueryConstraint the constraint
+ */
+ protected final void setFieldQueryConstraint(Constraint fieldQueryConstraint) {
+ this.fieldQueryConstraint = fieldQueryConstraint;
+ }
/**
* Sets an IndexConstraintType to a specific value
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=1151837&r1=1151836&r2=1151837&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 Thu Jul 28 13:33:41 2011
@@ -601,12 +601,15 @@ public class SolrYard extends AbstractYa
}
private QueryResultList<Representation> find(final FieldMapper fieldMapper,final FieldQuery parsedQuery, SELECT select) throws YardException {
- log.debug("find " + parsedQuery);
+ //create a clone of the query, because we need to refine it because the
+ //query (as executed) needs to be included in the result set
+ FieldQuery fieldQuery = parsedQuery.clone();
+ log.debug("find " + fieldQuery);
long start = System.currentTimeMillis();
final Set<String> selected;
if (select == SELECT.QUERY) {
// if query set the fields to add to the result Representations
- selected = new HashSet<String>(parsedQuery.getSelectedFields());
+ selected = new HashSet<String>(fieldQuery.getSelectedFields());
// add the score to query results!
selected.add(RdfResourceEnum.resultScore.getUri());
} else {
@@ -614,7 +617,7 @@ public class SolrYard extends AbstractYa
selected = null;
}
- SolrQuery query = getSolrQueryFactory().parseFieldQuery(parsedQuery, select);
+ SolrQuery query = getSolrQueryFactory().parseFieldQuery(fieldQuery, select);
long queryGeneration = System.currentTimeMillis();
QueryResponse response;
@@ -631,7 +634,7 @@ public class SolrYard extends AbstractYa
}
long queryTime = System.currentTimeMillis();
// return a queryResultList
- QueryResultListImpl<Representation> resultList = new QueryResultListImpl<Representation>(parsedQuery,
+ QueryResultListImpl<Representation> resultList = new QueryResultListImpl<Representation>(fieldQuery,
// by adapting SolrDocuments to Representations
new AdaptingIterator<SolrDocument,Representation>(response.getResults().iterator(),
// inline Adapter Implementation
@@ -652,7 +655,10 @@ public class SolrYard extends AbstractYa
@Override
public final QueryResultList<String> findReferences(FieldQuery parsedQuery) throws YardException {
- SolrQuery query = getSolrQueryFactory().parseFieldQuery(parsedQuery, SELECT.ID);
+ //create a clone of the query, because we need to refine it because the
+ //query (as executed) needs to be included in the result set
+ FieldQuery fieldQuery = parsedQuery.clone();
+ SolrQuery query = getSolrQueryFactory().parseFieldQuery(fieldQuery, SELECT.ID);
QueryResponse respone;
try {
respone = getServer().query(query, METHOD.POST);
@@ -661,7 +667,7 @@ public class SolrYard extends AbstractYa
}
final FieldMapper fieldMapper = getFieldMapper();
// return a queryResultList
- return new QueryResultListImpl<String>(parsedQuery,
+ return new QueryResultListImpl<String>(fieldQuery,
// by adapting SolrDocuments to Representations
new AdaptingIterator<SolrDocument,String>(respone.getResults().iterator(),
// inline Adapter Implementation
Modified: incubator/stanbol/trunk/integration-tests/pom.xml
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/integration-tests/pom.xml?rev=1151837&r1=1151836&r2=1151837&view=diff
==============================================================================
--- incubator/stanbol/trunk/integration-tests/pom.xml (original)
+++ incubator/stanbol/trunk/integration-tests/pom.xml Thu Jul 28 13:33:41 2011
@@ -60,7 +60,7 @@
<groupId>org.apache.stanbol</groupId>
<artifactId>org.apache.stanbol.launchers.full</artifactId>
</dependency>
- <dependency>
+ <dependency>
<groupId>org.apache.stanbol</groupId>
<artifactId>org.apache.stanbol.commons.web.base</artifactId>
</dependency>
@@ -73,6 +73,10 @@
<artifactId>org.apache.stanbol.commons.testing.stanbol</artifactId>
</dependency>
<dependency>
+ <groupId>org.apache.stanbol</groupId>
+ <artifactId>org.apache.stanbol.entityhub.test</artifactId>
+ </dependency>
+ <dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
Modified: incubator/stanbol/trunk/integration-tests/src/test/java/org/apache/stanbol/entityhub/it/EntityHubTest.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/integration-tests/src/test/java/org/apache/stanbol/entityhub/it/EntityHubTest.java?rev=1151837&r1=1151836&r2=1151837&view=diff
==============================================================================
--- incubator/stanbol/trunk/integration-tests/src/test/java/org/apache/stanbol/entityhub/it/EntityHubTest.java (original)
+++ incubator/stanbol/trunk/integration-tests/src/test/java/org/apache/stanbol/entityhub/it/EntityHubTest.java Thu Jul 28 13:33:41 2011
@@ -16,19 +16,26 @@
*/
package org.apache.stanbol.entityhub.it;
+import static junit.framework.Assert.*;
+
+import java.util.ArrayList;
+import java.util.Collections;
+
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.stanbol.commons.testing.http.RequestExecutor;
-import org.apache.stanbol.commons.web.base.writers.JsonLdSerializerProvider;
-import org.apache.stanbol.enhancer.it.EnhancerTestBase;
+import org.apache.stanbol.entityhub.test.it.EntityhubTestBase;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONObject;
-import org.junit.Assert;
import org.junit.Test;
//inherit from EnhancerTestBase, but we more care about the entityhub readiness than engine's one.
-public class EntityHubTest extends EnhancerTestBase {
+public final class EntityHubTest extends EntityhubTestBase {
+ public EntityHubTest() {
+ super(new ArrayList<String>(),LoggerFactory.getLogger(EntityHubTest.class));
+ }
+
private final Logger log = LoggerFactory.getLogger(getClass());
final String PARIS_VALUE =
@@ -79,14 +86,14 @@ public class EntityHubTest extends Enhan
) ;
log.info("Test request number {}/{} : ",i,queryRequests.length);
- log.info(re.getContent());
+ log.debug(re.getContent());
re.assertStatus(200)
.assertContentType("application/json");
JSONObject jso = new JSONObject(re.getContent());
JSONArray result = jso.getJSONArray("results");
- Assert.assertNotSame(0, result.length());
+ assertNotSame(0, result.length());
}
}
@@ -99,13 +106,13 @@ public class EntityHubTest extends Enhan
) ;
log.info("Test request : ");
- log.info(re.getContent());
+ log.debug(re.getContent());
re.assertStatus(200)
.assertContentType("application/json");
JSONObject jso = new JSONObject(re.getContent());
JSONArray result = jso.getJSONArray("results");
- Assert.assertNotSame(0, result.length());
+ assertNotSame(0, result.length());
}
}