You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@geode.apache.org by up...@apache.org on 2017/10/23 17:51:46 UTC

[geode-examples] branch develop updated: GEODE-3888: Adding queries with the java API to the lucene example

This is an automated email from the ASF dual-hosted git repository.

upthewaterspout pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/geode-examples.git


The following commit(s) were added to refs/heads/develop by this push:
     new c375271  GEODE-3888: Adding queries with the java API to the lucene example
c375271 is described below

commit c37527182b6e5696511523bfb240a3489c22fbab
Author: Dan Smith <up...@apache.org>
AuthorDate: Fri Oct 20 16:39:01 2017 -0700

    GEODE-3888: Adding queries with the java API to the lucene example
---
 lucene/build.gradle                                | 19 ++++++++++++++
 .../org/apache/geode/examples/lucene/Example.java  | 30 +++++++++++++---------
 .../apache/geode/examples/lucene/ExampleTest.java  | 21 ++++++---------
 3 files changed, 45 insertions(+), 25 deletions(-)

diff --git a/lucene/build.gradle b/lucene/build.gradle
new file mode 100644
index 0000000..d373d3a
--- /dev/null
+++ b/lucene/build.gradle
@@ -0,0 +1,19 @@
+/*
+ * 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.
+ */
+dependencies {
+    compile "org.apache.geode:geode-lucene:$geodeVersion"
+}
\ No newline at end of file
diff --git a/lucene/src/main/java/org/apache/geode/examples/lucene/Example.java b/lucene/src/main/java/org/apache/geode/examples/lucene/Example.java
index 84cfc4b..9c19f5a 100644
--- a/lucene/src/main/java/org/apache/geode/examples/lucene/Example.java
+++ b/lucene/src/main/java/org/apache/geode/examples/lucene/Example.java
@@ -15,6 +15,7 @@
 package org.apache.geode.examples.lucene;
 
 import java.util.Arrays;
+import java.util.Map;
 import java.util.Random;
 import java.util.function.Consumer;
 
@@ -22,9 +23,13 @@ import org.apache.geode.cache.Region;
 import org.apache.geode.cache.client.ClientCache;
 import org.apache.geode.cache.client.ClientCacheFactory;
 import org.apache.geode.cache.client.ClientRegionShortcut;
+import org.apache.geode.cache.lucene.LuceneQuery;
+import org.apache.geode.cache.lucene.LuceneQueryException;
+import org.apache.geode.cache.lucene.LuceneService;
+import org.apache.geode.cache.lucene.LuceneServiceProvider;
 
-public class Example implements Consumer<Region<Integer, EmployeeData>> {
-  public static void main(String[] args) {
+public class Example {
+  public static void main(String[] args) throws LuceneQueryException {
     // connect to the locator using default port 10334
     ClientCache cache = new ClientCacheFactory().addPoolLocator("127.0.0.1", 10334)
         .set("log-level", "WARN").create();
@@ -34,12 +39,20 @@ public class Example implements Consumer<Region<Integer, EmployeeData>> {
         cache.<Integer, EmployeeData>createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY)
             .create("example-region");
 
-    new Example().accept(region);
+    insertValues(region);
+    query(cache);
     cache.close();
   }
 
-  @Override
-  public void accept(Region<Integer, EmployeeData> region) {
+  private static void query(ClientCache cache) throws LuceneQueryException {
+    LuceneService lucene = LuceneServiceProvider.get(cache);
+    LuceneQuery<Integer, EmployeeData> query = lucene.createLuceneQueryFactory()
+        .create("simpleIndex", "example-region", "firstName:Chris~2", "firstname");
+    System.out.println("Employees with first names like Chris: " + query.findValues());
+  }
+
+
+  public static void insertValues(Map<Integer, EmployeeData> region) {
     // insert values into the region
     String[] firstNames = "Alex,Bertie,Kris,Dale,Frankie,Jamie,Morgan,Pat,Ricky,Taylor".split(",");
     String[] lastNames = "Able,Bell,Call,Driver,Forth,Jive,Minnow,Puts,Reliable,Tack".split(",");
@@ -57,12 +70,5 @@ public class Example implements Consumer<Region<Integer, EmployeeData>> {
           salary, hoursPerWeek);
       region.put(key, val);
     }
-
-    // count the values in the region
-    int inserted = region.keySetOnServer().size();
-    System.out.println(String.format("Counted %d keys in region %s", inserted, region.getName()));
-
-    // fetch the values in the region
-    region.keySetOnServer().forEach(key -> System.out.println(region.get(key)));
   }
 }
diff --git a/lucene/src/test/java/org/apache/geode/examples/lucene/ExampleTest.java b/lucene/src/test/java/org/apache/geode/examples/lucene/ExampleTest.java
index c3e1013..8e488e5 100644
--- a/lucene/src/test/java/org/apache/geode/examples/lucene/ExampleTest.java
+++ b/lucene/src/test/java/org/apache/geode/examples/lucene/ExampleTest.java
@@ -14,25 +14,20 @@
  */
 package org.apache.geode.examples.lucene;
 
-import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.Assert.assertEquals;
+
+import java.util.HashMap;
+import java.util.Map;
 
-import org.apache.geode.cache.Region;
-import org.geode.examples.util.Mocks;
-import org.junit.Rule;
 import org.junit.Test;
-import org.junit.contrib.java.lang.system.SystemOutRule;
 
 public class ExampleTest {
 
-  @Rule
-  public SystemOutRule systemOutRule = new SystemOutRule().enableLog();
-
   @Test
-  public void testExample() throws Exception {
-    Region<Integer, EmployeeData> region = Mocks.region("example-region");
-    new Example().accept(region);
+  public void testInsertEntries() throws Exception {
+    Map<Integer, EmployeeData> region = new HashMap<>();
+    Example.insertValues(region);
 
-    assertThat(systemOutRule.getLog()).contains("Counted 10 keys in region");
-    assertThat(systemOutRule.getLog()).contains("Jamie");
+    assertEquals(10, region.size());
   }
 }

-- 
To stop receiving notification emails like this one, please contact
['"commits@geode.apache.org" <co...@geode.apache.org>'].