cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From r...@apache.org
Subject [1/2] git commit: CXF-5923: Create sample to demonstrate usage of search extension with Lucene and Tika. Added initial search support.
Date Mon, 11 Aug 2014 14:24:44 GMT
Repository: cxf
Updated Branches:
  refs/heads/master 8bcb5133c -> fc1ac5cb2


CXF-5923: Create sample to demonstrate usage of search extension with Lucene and Tika. Added
initial search support.


Project: http://git-wip-us.apache.org/repos/asf/cxf/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/b5fc90f0
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/b5fc90f0
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/b5fc90f0

Branch: refs/heads/master
Commit: b5fc90f09c6b039a54564e0825a264310453c550
Parents: 9344b09
Author: reta <drreta@gmail.com>
Authored: Mon Aug 11 10:24:07 2014 -0400
Committer: reta <drreta@gmail.com>
Committed: Mon Aug 11 10:24:07 2014 -0400

----------------------------------------------------------------------
 .../java/demo/jaxrs/search/client/Client.java   | 26 +++++++++++++--
 .../java/demo/jaxrs/search/server/Catalog.java  | 35 ++++++++++++++------
 .../java/demo/jaxrs/search/server/Server.java   |  1 +
 3 files changed, 49 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/b5fc90f0/distribution/src/main/release/samples/jax_rs/search/src/main/java/demo/jaxrs/search/client/Client.java
----------------------------------------------------------------------
diff --git a/distribution/src/main/release/samples/jax_rs/search/src/main/java/demo/jaxrs/search/client/Client.java
b/distribution/src/main/release/samples/jax_rs/search/src/main/java/demo/jaxrs/search/client/Client.java
index d1fad9c..211ee18 100644
--- a/distribution/src/main/release/samples/jax_rs/search/src/main/java/demo/jaxrs/search/client/Client.java
+++ b/distribution/src/main/release/samples/jax_rs/search/src/main/java/demo/jaxrs/search/client/Client.java
@@ -40,8 +40,11 @@ public final class Client {
         final HttpClient httpClient = new HttpClient();
                         
         uploadToCatalog(url, httpClient, "jsr339-jaxrs-2.0-final-spec.pdf");
-        uploadToCatalog(url, httpClient, "JavaWebSocketAPI_1.0_Final.pdf");
-        list(url, httpClient);
+        uploadToCatalog(url, httpClient, "JavaWebSocketAPI_1.0_Final.pdf");             

+        
+        list(url, httpClient);        
+        
+        search(url, httpClient, "ct==java");        
     }
 
     private static void list(final String url, final HttpClient httpClient) 
@@ -59,6 +62,25 @@ public final class Client {
             get.releaseConnection();
         }
     }
+    
+    private static void search(final String url, final HttpClient httpClient, final String
expression) 
+        throws IOException, HttpException {
+            
+        System.out.println("Sent HTTP GET request to search the books in catalog: " + expression);
+        
+        final GetMethod get = new GetMethod(url + "/search");
+        get.setQueryString("$filter=" + expression);
+        
+        try {
+            int status = httpClient.executeMethod(get);
+            if (status == 200) {   
+                System.out.println(get.getResponseBodyAsString());
+            }
+        } finally {
+            get.releaseConnection();
+        }
+    }
+    
 
     private static void uploadToCatalog(final String url, final HttpClient httpClient,
             final String filename) throws IOException, HttpException {

http://git-wip-us.apache.org/repos/asf/cxf/blob/b5fc90f0/distribution/src/main/release/samples/jax_rs/search/src/main/java/demo/jaxrs/search/server/Catalog.java
----------------------------------------------------------------------
diff --git a/distribution/src/main/release/samples/jax_rs/search/src/main/java/demo/jaxrs/search/server/Catalog.java
b/distribution/src/main/release/samples/jax_rs/search/src/main/java/demo/jaxrs/search/server/Catalog.java
index eb9241a..42cb4d5 100644
--- a/distribution/src/main/release/samples/jax_rs/search/src/main/java/demo/jaxrs/search/server/Catalog.java
+++ b/distribution/src/main/release/samples/jax_rs/search/src/main/java/demo/jaxrs/search/server/Catalog.java
@@ -22,8 +22,6 @@ package demo.jaxrs.search.server;
 
 import java.io.BufferedInputStream;
 import java.io.IOException;
-import java.util.Arrays;
-import java.util.Collection;
 import java.util.Date;
 import java.util.HashMap;
 import java.util.Map;
@@ -63,6 +61,7 @@ import org.apache.lucene.search.IndexSearcher;
 import org.apache.lucene.search.MatchAllDocsQuery;
 import org.apache.lucene.search.Query;
 import org.apache.lucene.search.ScoreDoc;
+import org.apache.lucene.search.TopDocs;
 import org.apache.lucene.store.Directory;
 import org.apache.lucene.store.RAMDirectory;
 import org.apache.lucene.util.Version;
@@ -73,8 +72,7 @@ public class Catalog {
     private final TikaLuceneContentExtractor extractor = new TikaLuceneContentExtractor(new
PDFParser());    
     private final Directory directory = new RAMDirectory();
     private final Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_40);
-    private final IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_40, analyzer);
-    private final LuceneQueryVisitor<SearchBean> visitor = createVisitor();
+    private final IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_40, analyzer);
   
     
     public Catalog() throws IOException {
         initIndex();
@@ -129,11 +127,12 @@ public class Catalog {
             final Query query = new MatchAllDocsQuery();
             
             for (final ScoreDoc scoreDoc: searcher.search(query, 1000).scoreDocs) {
-                final DocumentStoredFieldVisitor fieldVisitor = 
+                final DocumentStoredFieldVisitor visitor = 
                     new DocumentStoredFieldVisitor("source");
                 
-                reader.document(scoreDoc.doc, fieldVisitor);
-                builder.add(fieldVisitor.getDocument().getField("source").stringValue());
+                
+                reader.document(scoreDoc.doc, visitor);
+                builder.add(visitor.getDocument().getField("source").stringValue());
             }
             
             return builder.build();
@@ -145,13 +144,27 @@ public class Catalog {
     @GET
     @Produces(MediaType.APPLICATION_JSON)
     @Path("/search")
-    public Collection<ScoreDoc> findBook(@Context SearchContext searchContext) throws
IOException {
+    public JsonArray findBook(@Context SearchContext searchContext) throws IOException {
         final IndexReader reader = DirectoryReader.open(directory);
         final IndexSearcher searcher = new IndexSearcher(reader);
+        final JsonArrayBuilder builder = Json.createArrayBuilder();
 
-        try {
+        try {            
+            final LuceneQueryVisitor<SearchBean> visitor = createVisitor();
             visitor.visit(searchContext.getCondition(SearchBean.class));
-            return Arrays.asList(searcher.search(visitor.getQuery(), null, 1000).scoreDocs);
+            
+            final TopDocs topDocs = searcher.search(visitor.getQuery(), 1000);
+            for (final ScoreDoc scoreDoc: topDocs.scoreDocs) {
+                final Document document = reader.document(scoreDoc.doc);
+                
+                builder.add(
+                    Json.createObjectBuilder()
+                        .add("source", document.getField("source").stringValue())
+                        .add("score", scoreDoc.score)
+                );
+            }
+            
+            return builder.build();
         } finally {
             reader.close();
         }
@@ -174,7 +187,7 @@ public class Catalog {
     private void initIndex() throws IOException {
         final IndexWriter writer = new IndexWriter(directory, config);
         
-        try {            
+        try {
             writer.commit();
         } finally {
             writer.close();

http://git-wip-us.apache.org/repos/asf/cxf/blob/b5fc90f0/distribution/src/main/release/samples/jax_rs/search/src/main/java/demo/jaxrs/search/server/Server.java
----------------------------------------------------------------------
diff --git a/distribution/src/main/release/samples/jax_rs/search/src/main/java/demo/jaxrs/search/server/Server.java
b/distribution/src/main/release/samples/jax_rs/search/src/main/java/demo/jaxrs/search/server/Server.java
index aeacb54..1b25496 100644
--- a/distribution/src/main/release/samples/jax_rs/search/src/main/java/demo/jaxrs/search/server/Server.java
+++ b/distribution/src/main/release/samples/jax_rs/search/src/main/java/demo/jaxrs/search/server/Server.java
@@ -40,6 +40,7 @@ public class Server {
         properties.put(SearchUtils.DATE_FORMAT_PROPERTY, "yyyy/MM/dd");
 
         final JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
+        sf.setProperties(properties);
         sf.setResourceClasses(Catalog.class);
         sf.setResourceProvider(Catalog.class, new SingletonResourceProvider(new Catalog()));
         sf.setAddress("http://localhost:9000/");


Mime
View raw message