You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@stanbol.apache.org by me...@apache.org on 2013/02/27 16:29:05 UTC

svn commit: r1450806 - in /stanbol/branches/contenthub-two-layered-structure/contenthub/servicesapi: ./ src/license/ src/main/java/org/apache/stanbol/contenthub/servicesapi/ src/main/java/org/apache/stanbol/contenthub/servicesapi/index/search/solr/ src...

Author: meric
Date: Wed Feb 27 15:29:05 2013
New Revision: 1450806

URL: http://svn.apache.org/r1450806
Log:
* dependency versions are updated according to the last release
* component version is updated to 0.11.0-SNAPSHOT
* merged with trunk(r1444001)
* enhancement graph uri is changed to "org.apache.stanbol.contenthub.enhancements" from "org.apache.stanbol.enhancer.standalone.store.enhancements"
* SolrQueryUtil is moved to servicesapi/index/search/solr/

Added:
    stanbol/branches/contenthub-two-layered-structure/contenthub/servicesapi/src/main/java/org/apache/stanbol/contenthub/servicesapi/index/search/solr/SolrQueryUtil.java
Modified:
    stanbol/branches/contenthub-two-layered-structure/contenthub/servicesapi/pom.xml
    stanbol/branches/contenthub-two-layered-structure/contenthub/servicesapi/src/license/THIRD-PARTY.properties
    stanbol/branches/contenthub-two-layered-structure/contenthub/servicesapi/src/main/java/org/apache/stanbol/contenthub/servicesapi/Constants.java
    stanbol/branches/contenthub-two-layered-structure/contenthub/servicesapi/src/main/java/org/apache/stanbol/contenthub/servicesapi/store/vocabulary/SolrVocabulary.java

Modified: stanbol/branches/contenthub-two-layered-structure/contenthub/servicesapi/pom.xml
URL: http://svn.apache.org/viewvc/stanbol/branches/contenthub-two-layered-structure/contenthub/servicesapi/pom.xml?rev=1450806&r1=1450805&r2=1450806&view=diff
==============================================================================
--- stanbol/branches/contenthub-two-layered-structure/contenthub/servicesapi/pom.xml (original)
+++ stanbol/branches/contenthub-two-layered-structure/contenthub/servicesapi/pom.xml Wed Feb 27 15:29:05 2013
@@ -16,15 +16,15 @@
 
   <parent>
     <groupId>org.apache.stanbol</groupId>
-    <artifactId>org.apache.stanbol.contenthub.parent</artifactId>
-    <version>0.10.0-incubating-SNAPSHOT</version>
-    <relativePath>../parent</relativePath>
+    <artifactId>apache-stanbol-contenthub</artifactId>
+    <version>0.11.0-SNAPSHOT</version>
+    <relativePath>..</relativePath>
   </parent>
 
   <artifactId>org.apache.stanbol.contenthub.servicesapi</artifactId>
   <packaging>bundle</packaging>
 
-  <name>Apache Stanbol Contenthub API Bundle</name>
+  <name>Apache Stanbol Contenthub Service API</name>
 
   <build>
     <plugins>
@@ -32,21 +32,31 @@
         <groupId>org.apache.felix</groupId>
         <artifactId>maven-bundle-plugin</artifactId>
         <extensions>true</extensions>
+        <configuration>
+          <instructions>
+            <Export-Package>
+              org.apache.stanbol.contenthub.servicesapi;version=${project.version},
+              org.apache.stanbol.contenthub.servicesapi.*;version=${project.version}
+            </Export-Package>
+          </instructions>
+        </configuration>
       </plugin>
     </plugins>
   </build>
 
   <dependencies>
+    <!-- Increased the version because of the change in
+    ContentItem#getMetadata -->
     <dependency>
       <groupId>org.apache.stanbol</groupId>
       <artifactId>org.apache.stanbol.enhancer.servicesapi</artifactId>
-      <version>0.10.0-incubating-SNAPSHOT</version>
+      <version>0.10.0</version>
     </dependency>
-    <dependency>
+    <!-- dependency>
       <groupId>org.apache.stanbol</groupId>
       <artifactId>org.apache.stanbol.entityhub.servicesapi</artifactId>
-      <version>0.11.0-incubating-SNAPSHOT</version>
-    </dependency>
+      <version>0.11.0</version>
+    </dependency -->
     <dependency>
       <groupId>org.apache.solr</groupId>
       <artifactId>solr-solrj</artifactId>

Modified: stanbol/branches/contenthub-two-layered-structure/contenthub/servicesapi/src/license/THIRD-PARTY.properties
URL: http://svn.apache.org/viewvc/stanbol/branches/contenthub-two-layered-structure/contenthub/servicesapi/src/license/THIRD-PARTY.properties?rev=1450806&r1=1450805&r2=1450806&view=diff
==============================================================================
--- stanbol/branches/contenthub-two-layered-structure/contenthub/servicesapi/src/license/THIRD-PARTY.properties (original)
+++ stanbol/branches/contenthub-two-layered-structure/contenthub/servicesapi/src/license/THIRD-PARTY.properties Wed Feb 27 15:29:05 2013
@@ -19,8 +19,6 @@
 # Please fill the missing licenses for dependencies :
 #
 #
-#Thu Jul 19 17:29:09 EEST 2012
-javax.servlet--servlet-api--2.5=Common Development And Distribution License (CDDL), Version 1.0
-org.apache.zookeeper--zookeeper--3.3.1=The Apache Software License, Version 2.0
+#Tue Jan 22 16:20:32 CET 2013
 org.osgi--org.osgi.compendium--4.1.0=The Apache Software License, Version 2.0
 org.osgi--org.osgi.core--4.1.0=The Apache Software License, Version 2.0

Modified: stanbol/branches/contenthub-two-layered-structure/contenthub/servicesapi/src/main/java/org/apache/stanbol/contenthub/servicesapi/Constants.java
URL: http://svn.apache.org/viewvc/stanbol/branches/contenthub-two-layered-structure/contenthub/servicesapi/src/main/java/org/apache/stanbol/contenthub/servicesapi/Constants.java?rev=1450806&r1=1450805&r2=1450806&view=diff
==============================================================================
--- stanbol/branches/contenthub-two-layered-structure/contenthub/servicesapi/src/main/java/org/apache/stanbol/contenthub/servicesapi/Constants.java (original)
+++ stanbol/branches/contenthub-two-layered-structure/contenthub/servicesapi/src/main/java/org/apache/stanbol/contenthub/servicesapi/Constants.java Wed Feb 27 15:29:05 2013
@@ -34,7 +34,7 @@ public class Constants {
     /**
      * The URI of the global enhancement graph. All enhancements are stored in this graph.
      */
-    public static final String ENHANCEMENTS_GRAPH_URI = "org.apache.stanbol.enhancer.standalone.store.enhancements";
+    public static final String ENHANCEMENTS_GRAPH_URI = "org.apache.stanbol.contenthub.enhancements";
 
     /**
      * The URI of ???

Added: stanbol/branches/contenthub-two-layered-structure/contenthub/servicesapi/src/main/java/org/apache/stanbol/contenthub/servicesapi/index/search/solr/SolrQueryUtil.java
URL: http://svn.apache.org/viewvc/stanbol/branches/contenthub-two-layered-structure/contenthub/servicesapi/src/main/java/org/apache/stanbol/contenthub/servicesapi/index/search/solr/SolrQueryUtil.java?rev=1450806&view=auto
==============================================================================
--- stanbol/branches/contenthub-two-layered-structure/contenthub/servicesapi/src/main/java/org/apache/stanbol/contenthub/servicesapi/index/search/solr/SolrQueryUtil.java (added)
+++ stanbol/branches/contenthub-two-layered-structure/contenthub/servicesapi/src/main/java/org/apache/stanbol/contenthub/servicesapi/index/search/solr/SolrQueryUtil.java Wed Feb 27 15:29:05 2013
@@ -0,0 +1,275 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.stanbol.contenthub.servicesapi.index.search.solr;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+
+import org.apache.solr.client.solrj.SolrQuery;
+import org.apache.solr.client.solrj.SolrServer;
+import org.apache.solr.client.solrj.SolrServerException;
+import org.apache.solr.client.solrj.request.LukeRequest;
+import org.apache.solr.client.solrj.util.ClientUtils;
+import org.apache.solr.common.params.CommonParams;
+import org.apache.solr.common.params.SolrParams;
+import org.apache.solr.common.util.NamedList;
+import org.apache.stanbol.contenthub.servicesapi.index.search.featured.Constraint;
+import org.apache.stanbol.contenthub.servicesapi.index.search.featured.Facet;
+import org.apache.stanbol.contenthub.servicesapi.index.search.featured.FacetResult;
+import org.apache.stanbol.contenthub.servicesapi.store.vocabulary.SolrVocabulary;
+import org.apache.stanbol.contenthub.servicesapi.store.vocabulary.SolrVocabulary.SolrFieldName;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * 
+ * @author anil.pacaci
+ * 
+ */
+public class SolrQueryUtil {
+
+    private static final Logger log = LoggerFactory.getLogger(SolrQueryUtil.class);
+
+    public static final String CONTENT_FIELD = "content_t";
+    public static final String ID_FIELD = "id";
+    public static final String SCORE_FIELD = "score";
+
+    public static final String and = "AND";
+    public static final String or = "OR";
+    public static final String facetDelimiter = ":";
+    public static final char quotation = '"';
+
+    public static final List<Character> queryDelimiters = Arrays.asList(' ', ',');
+
+    private static String removeFacetConstraints(String query) {
+        int delimiteri = query.indexOf(facetDelimiter);
+        while (delimiteri > -1) {
+            int starti = delimiteri;
+            for (starti = delimiteri; starti >= 0 && !queryDelimiters.contains(query.charAt(starti)); starti--)
+                ;
+            ++starti;
+
+            int endi = delimiteri + 1;
+            if (endi < query.length()) {
+                if (query.charAt(endi) == quotation) {
+                    ++endi;
+                    for (; endi < query.length() && query.charAt(endi) != quotation; endi++)
+                        ;
+                    ++endi;
+                } else {
+                    for (; endi < query.length() && !queryDelimiters.contains(query.charAt(endi)); endi++)
+                        ;
+                }
+            }
+            query = query.substring(0, starti) + (endi >= query.length() ? "" : query.substring(endi));
+            delimiteri = query.indexOf(facetDelimiter);
+        }
+        query = query.replaceAll(and + '|' + or + '|' + and.toLowerCase() + '|' + or.toLowerCase(), "");
+        return query;
+    }
+
+    private static String removeSpecialCharacter(String query, char ch) {
+        int starti = query.indexOf(ch);
+        while (starti > -1) {
+            int endi = -1;
+            for (endi = starti; endi < query.length() && !queryDelimiters.contains(query.charAt(endi)); endi++)
+                ;
+            query = query.substring(0, starti) + (endi >= query.length() ? "" : query.substring(endi));
+            starti = query.indexOf(ch);
+        }
+        return query;
+    }
+
+    private static String removeSpecialCharacters(String query) {
+        query = query.replaceAll("[+|\\-&!\\(\\)\\{\\}\\[\\]\\*\\?\\\\]", "");
+        query = removeSpecialCharacter(query, '^');
+        query = removeSpecialCharacter(query, '~');
+        return query;
+    }
+
+    public static String extractQueryTermFromSolrQuery(SolrParams solrQuery) {
+        String queryFull = solrQuery instanceof SolrQuery ? ((SolrQuery) solrQuery).getQuery() : solrQuery
+                .get(CommonParams.Q);
+        queryFull = removeSpecialCharacters(queryFull);
+        queryFull = removeFacetConstraints(queryFull);
+        return queryFull.trim();
+    }
+
+    /**
+     * This methods adds a facet field the given <code>solrQuery</code> for each facet passed in
+     * <code>allAvailableFacetNames</code>. This provides obtaining information about specified facets such as
+     * possible facet values, number documents of documents matching with certain values of facets, etc in the
+     * search results.
+     * 
+     * @param solrQuery
+     *            {@link SolrQuery} to be extended with facet fields
+     * @param allAvailableFacetNames
+     *            list of facets
+     */
+    public static <T> void setFacetFields(SolrQuery solrQuery, List<T> allAvailableFacetNames) {
+        solrQuery.setFields("*", SCORE_FIELD);
+        solrQuery.setFacet(true);
+        solrQuery.setFacetMinCount(1);
+        if (allAvailableFacetNames != null) {
+            for (T facet : allAvailableFacetNames) {
+                String facetName;
+                if (facet instanceof String) {
+                    facetName = (String) facet;
+                } else if (facet instanceof FacetResult) {
+                    facetName = ((FacetResult) facet).getFacetField().getName();
+                } else {
+                    facetName = facet.toString();
+                }
+                if (SolrFieldName.CREATIONDATE.toString().equals(facetName)
+                    || (!SolrFieldName.isNameReserved(facetName) && !SolrVocabulary.isNameExcluded(facetName))) {
+                    solrQuery.addFacetField(facetName);
+                }
+            }
+        }
+    }
+
+    /**
+     * This method create a {@link SolrQuery} using the given parameters. <code>queryTerm</code> is the main
+     * query of the solr query to be created. <code>solrServer</code> is used fetch possible facet fields of
+     * the underlying Solr schema. Obtained facet names are attached to the query to obtain the corresponding
+     * facet information such as possible facet values, number documents of documents matching with certain
+     * values of facets, etc in the search results.
+     * 
+     * @param solrServer
+     *            Solr server to obtain corresponding facet names
+     * @param queryTerm
+     *            main query term to be used in {@link SolrQuery#setQuery(String)}
+     * @return {@link SolrQuery} constructed by using the given parameters
+     * @throws SolrServerException
+     * @throws IOException
+     */
+    public static SolrQuery prepareSolrQuery(SolrServer solrServer, String queryTerm) throws SolrServerException,
+                                                                                     IOException {
+        SolrQuery solrQuery = new SolrQuery();
+        solrQuery.setQuery(queryTerm);
+        setFacetFields(solrQuery, getAllFacetNames(solrServer));
+        return solrQuery;
+    }
+
+    /**
+     * This method simply wraps the given <code>queryTerm</code> in a {@link SolrQuery} instance.
+     * 
+     * @param queryTerm
+     *            {@link String} query term to be represented as a {@link SolrQuery}
+     * @return {@link SolrQuery} wrapping the given <code>queryTerm</code>
+     */
+    public static SolrQuery prepareSolrQuery(String queryTerm) {
+        SolrQuery solrQuery = new SolrQuery();
+        solrQuery.setQuery(queryTerm);
+        return solrQuery;
+    }
+
+    /**
+     * This method creates a {@link SolrQuery} with the given parameters. It sets the <code>queryTerm</code>
+     * as the main query and for each constraint passed in the <code>constraints</code> a filter query is
+     * added to the solr query.
+     * 
+     * @param queryTerm
+     *            main query to be used in {@link SolrQuery#setQuery(String)}
+     * @param allAvailableFacets
+     *            {@link FacetResult}s passed in this list are used to check types of the facets.
+     * @param constraints
+     *            additional constraints to be applied in the {@link SolrQuery}.
+     * @return {@link SolrQuery} constructed by using the given parameters
+     */
+    public static SolrQuery prepareSolrQuery(String queryTerm,
+                                             List<FacetResult> allAvailableFacets,
+                                             Map<String,List<Object>> constraints) {
+        SolrQuery query = new SolrQuery();
+        query.setQuery(queryTerm);
+        if (constraints != null) {
+            try {
+                for (Entry<String,List<Object>> entry : constraints.entrySet()) {
+                    String fieldName = ClientUtils.escapeQueryChars(entry.getKey());
+                    String type = getFacetFieldType(fieldName, allAvailableFacets);
+                    for (Object value : entry.getValue()) {
+                        if (SolrVocabulary.isRangeType(type)) {
+                            query.addFilterQuery(fieldName + facetDelimiter + (String) value);
+                        } else {
+                            query.addFilterQuery(fieldName + facetDelimiter + quotation
+                                                 + ClientUtils.escapeQueryChars((String) value) + quotation);
+                        }
+                    }
+                }
+            } catch (Exception e) {
+                log.warn("Facet constraints could not be added to Query", e);
+            }
+        }
+        return query;
+    }
+
+    private static String getFacetFieldType(String fieldName, List<FacetResult> allAvailableFacets) {
+        for (FacetResult fr : allAvailableFacets) {
+            if (fieldName.equals(fr.getFacetField().getName())) {
+                return fr.getType();
+            }
+        }
+        return "";
+    }
+
+    public static List<String> getAllFacetNames(SolrServer solrServer) throws SolrServerException,
+                                                                      IOException {
+        List<String> facetNames = new ArrayList<String>();
+        NamedList<Object> fieldsList = getAllFacetFields(solrServer);
+        for (int i = 0; i < fieldsList.size(); i++) {
+            facetNames.add(fieldsList.getName(i));
+        }
+        return facetNames;
+    }
+
+    @SuppressWarnings("unchecked")
+    public static NamedList<Object> getAllFacetFields(SolrServer solrServer) throws SolrServerException,
+                                                                            IOException {
+        LukeRequest qr = new LukeRequest();
+        NamedList<Object> qresp = solrServer.request(qr);
+        Object fields = qresp.get("fields");
+        if (fields instanceof NamedList<?>) {
+            return (NamedList<Object>) fields;
+        } else {
+            throw new IllegalStateException(
+                    "Fields container is not a NamedList, so there is no facet information available");
+        }
+    }
+
+    /**
+     * This method parses the {@link Set} of {@link Constraint} and update the {@link SolrQuery} with
+     * corresponding field queries. Name of the field is obtained from associated {@link Facet} of a
+     * constraint and the value is obtained from the constraint itself.
+     * 
+     * @param constraints
+     *            {@link Set} of {@link Constraint}s to be transformed into the given <code>solrQuery</code>
+     * @param solrQuery
+     *            {@link SolrQuery} to be updated with the given <code>constraints</code>
+     */
+    public static void addConstraintsToSolrQuery(Set<Constraint> constraints, SolrQuery solrQuery) {
+        if (constraints != null) {
+            for (Constraint constraint : constraints) {
+                solrQuery.addFilterQuery(constraint.getFacet().getLabel(null) + ":" + constraint.getValue());
+            }
+        }
+    }
+}
\ No newline at end of file

Modified: stanbol/branches/contenthub-two-layered-structure/contenthub/servicesapi/src/main/java/org/apache/stanbol/contenthub/servicesapi/store/vocabulary/SolrVocabulary.java
URL: http://svn.apache.org/viewvc/stanbol/branches/contenthub-two-layered-structure/contenthub/servicesapi/src/main/java/org/apache/stanbol/contenthub/servicesapi/store/vocabulary/SolrVocabulary.java?rev=1450806&r1=1450805&r2=1450806&view=diff
==============================================================================
--- stanbol/branches/contenthub-two-layered-structure/contenthub/servicesapi/src/main/java/org/apache/stanbol/contenthub/servicesapi/store/vocabulary/SolrVocabulary.java (original)
+++ stanbol/branches/contenthub-two-layered-structure/contenthub/servicesapi/src/main/java/org/apache/stanbol/contenthub/servicesapi/store/vocabulary/SolrVocabulary.java Wed Feb 27 15:29:05 2013
@@ -275,7 +275,7 @@ public class SolrVocabulary {
      */
     public static boolean isNameExcluded(String name) {
         // trim the data type extension if there is one
-        int underscoreIndex = name.lastIndexOf("_");
+        int underscoreIndex = name.lastIndexOf('_');
         if (underscoreIndex != -1) {
             String extension = name.substring(underscoreIndex);
             if (DYNAMIC_FIELD_EXTENSIONS.contains(extension)) {