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/06/03 15:21:24 UTC

svn commit: r1131027 - in /incubator/stanbol/trunk/entityhub: jersey/src/main/java/org/apache/stanbol/entityhub/jersey/parsers/ yard/solr/src/main/java/org/apache/stanbol/entityhub/yard/solr/impl/ yard/solr/src/main/java/org/apache/stanbol/entityhub/ya...

Author: rwesten
Date: Fri Jun  3 13:21:24 2011
New Revision: 1131027

URL: http://svn.apache.org/viewvc?rev=1131027&view=rev
Log:
Bug corrections in the FieldQuery implementation of the SolrYard
 - For Value Constraints the check for "text" types compared IndexDataTypeEnum with IndexDataType. This caused that ' ' where not replaced with '+' in QueryStrings
 - For Value Constraints of Type "text" no language constraint was set. This caused the creation of invalid Solr Queries
 
 Improvements:
  - The lang Encoder accepts now null, an empty list and an list with only an null element to indicate that a search should include labels of any language.
  - For value constraints there is now a clear warning that only a single data type is supported.
  - added a logging in case of JSON parsing errors in the FieldQueryReader

Modified:
    incubator/stanbol/trunk/entityhub/jersey/src/main/java/org/apache/stanbol/entityhub/jersey/parsers/FieldQueryReader.java
    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/queryencoders/LangEncoder.java
    incubator/stanbol/trunk/entityhub/yard/solr/src/main/java/org/apache/stanbol/entityhub/yard/solr/utils/SolrUtil.java

Modified: incubator/stanbol/trunk/entityhub/jersey/src/main/java/org/apache/stanbol/entityhub/jersey/parsers/FieldQueryReader.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/entityhub/jersey/src/main/java/org/apache/stanbol/entityhub/jersey/parsers/FieldQueryReader.java?rev=1131027&r1=1131026&r2=1131027&view=diff
==============================================================================
--- incubator/stanbol/trunk/entityhub/jersey/src/main/java/org/apache/stanbol/entityhub/jersey/parsers/FieldQueryReader.java (original)
+++ incubator/stanbol/trunk/entityhub/jersey/src/main/java/org/apache/stanbol/entityhub/jersey/parsers/FieldQueryReader.java Fri Jun  3 13:21:24 2011
@@ -58,6 +58,7 @@ public class FieldQueryReader implements
                 DefaultQueryFactory.getInstance(), 
                 queryString);
         } catch (JSONException e) {
+            log.error("Unable to parse Request ",e);
             String acceptedMediaType = httpHeaders.getFirst("Accept");
             throw new WebApplicationException(
                 Response.status(Status.BAD_REQUEST).

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=1131027&r1=1131026&r2=1131027&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 Fri Jun  3 13:21:24 2011
@@ -19,9 +19,11 @@ package org.apache.stanbol.entityhub.yar
 import java.util.ArrayList;
 import java.util.Arrays;
 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;
 import java.util.Map.Entry;
@@ -31,6 +33,7 @@ import org.apache.solr.client.solrj.Solr
 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.defaults.DataTypeEnum;
 import org.apache.stanbol.entityhub.servicesapi.model.Representation;
 import org.apache.stanbol.entityhub.servicesapi.model.Text;
 import org.apache.stanbol.entityhub.servicesapi.model.ValueFactory;
@@ -42,6 +45,7 @@ import org.apache.stanbol.entityhub.serv
 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.servicesapi.util.ModelUtils;
 import org.apache.stanbol.entityhub.yard.solr.defaults.IndexDataTypeEnum;
 import org.apache.stanbol.entityhub.yard.solr.impl.queryencoders.AssignmentEncoder;
 import org.apache.stanbol.entityhub.yard.solr.impl.queryencoders.DataTypeEncoder;
@@ -332,14 +336,13 @@ public class SolrQueryFactory {
      */
     private void initIndexConstraint(IndexConstraint indexConstraint, ValueConstraint valueConstraint) {
         if (valueConstraint.getValue() == null) {
-            indexConstraint
-                    .setInvalid(String
-                            .format(
-                                "ValueConstraint without a value - that check only any value for the parsed datatypes %s is present - can not be supported by a Solr query!",
-                                valueConstraint.getDataTypes()));
+            indexConstraint.setInvalid(String.format(
+                "ValueConstraint without a value - that check only any value for " +
+                "the parsed datatypes %s is present - can not be supported by a Solr query!",
+                valueConstraint.getDataTypes()));
         } else {
             // first process the parsed dataTypes to get the supported types
-            Collection<IndexDataType> indexDataTypes = new HashSet<IndexDataType>();
+            Collection<IndexDataType> indexDataTypes = new ArrayList<IndexDataType>();
             if (valueConstraint.getDataTypes() != null) {
                 for (String dataType : valueConstraint.getDataTypes()) {
                     IndexDataTypeEnum indexDataTypeEnumEntry = IndexDataTypeEnum.forUri(dataType);
@@ -347,45 +350,50 @@ public class SolrQueryFactory {
                         indexDataTypes.add(indexDataTypeEnumEntry.getIndexType());
                     } else {
                         // TODO: Add possibility to add warnings to indexConstraints
-                        log.warn(String
-                                .format(
-                                    "A Datatype parsed for a ValueConstraint is not supported and will be ignored (dataTypeUri=%s)",
-                                    dataType));
+                        log.warn("A Datatype parsed for a ValueConstraint is not " +
+                        		"supported and will be ignored (dataTypeUri={})",
+                                dataType);
                     }
                 }
             }
-            if (indexDataTypes.isEmpty()) { // if no supported types are present
+            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
                 // get the dataType based on the type of the value
                 try {
-                    IndexValue indexValue = indexValueFactory.createIndexValue(valueConstraint.getValue());
-                    indexConstraint.setFieldConstraint(IndexConstraintTypeEnum.EQ, indexValue);
-                    indexConstraint.setFieldConstraint(IndexConstraintTypeEnum.DATATYPE, indexValue);
-                } catch (NoConverterException e) {
-                    indexConstraint.setInvalid(e.getMessage());
-                }
-            } else { // one or more supported dataTypes are present
-                for (IndexDataType indexDataType : indexDataTypes) {
-                    indexConstraint.setFieldConstraint(IndexConstraintTypeEnum.DATATYPE, indexDataType);
-                }
-                /*
-                 * NOTE: add only a single EQ constraints, because if different dataTypes would result in
-                 * different representations of the parsed value this code would not work altogether!
-                 */
-                IndexValue indexValue;
-                try { // use the default converter for the value
-                    indexValue = indexValueFactory.createIndexValue(valueConstraint.getValue());
+                    constraintValue = indexValueFactory.createIndexValue(valueConstraint.getValue());
                 } catch (NoConverterException e) {
-                    // if not found use the toString() and the first parsed DataType
-                    IndexDataType indexDataType = indexDataTypes.iterator().next();
+                    // 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));
-                    indexValue = new IndexValue(valueConstraint.getValue().toString(), indexDataType);
+                    constraintValue = new IndexValue(valueConstraint.getValue().toString(), indexDataType);
                 }
-                indexConstraint.setFieldConstraint(IndexConstraintTypeEnum.EQ, indexValue);
+            } 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())){
+                //NOTE: in case of TEXT we need also to add the language to create a valid
+                //query!
+                indexConstraint.setFieldConstraint(IndexConstraintTypeEnum.LANG, 
+                    Collections.singleton(constraintValue.getLanguage()));
             }
+            indexConstraint.setFieldConstraint(IndexConstraintTypeEnum.EQ, constraintValue);
         }
     }
 

Modified: incubator/stanbol/trunk/entityhub/yard/solr/src/main/java/org/apache/stanbol/entityhub/yard/solr/impl/queryencoders/LangEncoder.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/entityhub/yard/solr/src/main/java/org/apache/stanbol/entityhub/yard/solr/impl/queryencoders/LangEncoder.java?rev=1131027&r1=1131026&r2=1131027&view=diff
==============================================================================
--- incubator/stanbol/trunk/entityhub/yard/solr/src/main/java/org/apache/stanbol/entityhub/yard/solr/impl/queryencoders/LangEncoder.java (original)
+++ incubator/stanbol/trunk/entityhub/yard/solr/src/main/java/org/apache/stanbol/entityhub/yard/solr/impl/queryencoders/LangEncoder.java Fri Jun  3 13:21:24 2011
@@ -18,6 +18,8 @@ package org.apache.stanbol.entityhub.yar
 
 import java.util.Arrays;
 import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
 
 import org.apache.stanbol.entityhub.yard.solr.model.FieldMapper;
 import org.apache.stanbol.entityhub.yard.solr.query.ConstraintTypePosition;
@@ -42,7 +44,20 @@ public class LangEncoder implements Inde
 
     @Override
     public void encode(EncodedConstraintParts constraint, Collection<String> value) {
-        if (value != null && !value.isEmpty()) { // null indicates the default language!
+        //We need to process languages, because one may parse null, or
+        //an empty list or a list that contains a single element "null"
+        Collection<String> languages;
+        if(value == null || value.isEmpty()){
+            languages = Collections.emptyList();
+        } else {
+            languages = new HashSet<String>();
+            for(String lang : value){
+                if(lang != null){
+                    languages.add(lang);
+                }
+            }
+        }
+        if (!languages.isEmpty()) {
             for (String prefix : fieldMapper.encodeLanguages(value)) {
                 constraint.addEncoded(PREFIX, SolrUtil.escapeSolrSpecialChars(prefix));
             }
@@ -50,13 +65,6 @@ public class LangEncoder implements Inde
             // search in the language merger field of the default language
             constraint.addEncoded(PREFIX,
                 SolrUtil.escapeSolrSpecialChars(fieldMapper.getLanguageMergerField(null)));
-            // String[] prefixSuffix = fieldMapper.encodeDataType(STRING_DATATYPE);
-            // if(prefixSuffix[0] != null && !prefixSuffix[0].isEmpty()){
-            // constraint.addEncoded(PREFIX, prefixSuffix[0]);
-            // }
-            // if(prefixSuffix[1] != null && !prefixSuffix[1].isEmpty()){
-            // constraint.addEncoded(SUFFIX, prefixSuffix[1]);
-            // }
         }
     }
 

Modified: incubator/stanbol/trunk/entityhub/yard/solr/src/main/java/org/apache/stanbol/entityhub/yard/solr/utils/SolrUtil.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/entityhub/yard/solr/src/main/java/org/apache/stanbol/entityhub/yard/solr/utils/SolrUtil.java?rev=1131027&r1=1131026&r2=1131027&view=diff
==============================================================================
--- incubator/stanbol/trunk/entityhub/yard/solr/src/main/java/org/apache/stanbol/entityhub/yard/solr/utils/SolrUtil.java (original)
+++ incubator/stanbol/trunk/entityhub/yard/solr/src/main/java/org/apache/stanbol/entityhub/yard/solr/utils/SolrUtil.java Fri Jun  3 13:21:24 2011
@@ -67,7 +67,7 @@ public final class SolrUtil {
         if (IndexDataTypeEnum.TXT.getIndexType().equals(indexValue.getType())) {
             value = value.toLowerCase();
             queryConstraints = value.split(" ");
-        } else if (IndexDataTypeEnum.STR.equals(indexValue.getType())) {
+        } else if (IndexDataTypeEnum.STR.getIndexType().equals(indexValue.getType())) {
             value = value.toLowerCase();
             queryConstraints = new String[] {value.replace(' ', '+')};
         } else {