You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@geode.apache.org by zh...@apache.org on 2017/10/26 18:55:30 UTC

[geode-examples] 01/01: GEODE-3789: add example for search on nested object

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

zhouxj pushed a commit to branch feature/GEODE-3789
in repository https://gitbox.apache.org/repos/asf/geode-examples.git

commit 8fb045ed3a2095a1c77931df0d7cf97b34585747
Author: zhouxh <gz...@pivotal.io>
AuthorDate: Thu Oct 26 11:53:49 2017 -0700

    GEODE-3789: add example for search on nested object
---
 lucene/README.md                                   | 14 ++++++--
 lucene/scripts/start.gfsh                          | 13 +++++--
 .../apache/geode/examples/lucene/EmployeeData.java | 11 ++++--
 .../org/apache/geode/examples/lucene/Example.java  | 32 +++++++++++++++--
 .../apache/geode/examples/lucene/ZipAndPhone.java  | 41 ++++++++++++++++++++++
 5 files changed, 102 insertions(+), 9 deletions(-)

diff --git a/lucene/README.md b/lucene/README.md
index 6f30610..2044a2a 100644
--- a/lucene/README.md
+++ b/lucene/README.md
@@ -51,7 +51,7 @@ will also be retrieved from the region and printed to the console.
 
         $ gfsh
         ...
-        gfsh>connect --locators=127.0.0.1[10334]
+        gfsh>connect --locators=localhost[10334]
         gfsh>query --query="select * from /example-region"
         ...
 
@@ -59,7 +59,7 @@ will also be retrieved from the region and printed to the console.
 
         gfsh> list lucene indexes
 
-    Note that each server that holds partitioned data for this region has both the ```simpleIndex``` and the ```analyzerIndex```. Each Lucene index is stored as a co-located region with the partitioned data region.
+    Note that each server that holds partitioned data for this region has both the ```simpleIndex``` , ```analyzerIndex``` and the ```nestedObjectIndex```. Each Lucene index is stored as a co-located region with the partitioned data region.
 
      // Search for an exact name match
         gfsh>search lucene --name=simpleIndex --region=example-region --queryStrings="Jive" --defaultField=lastName
@@ -73,6 +73,16 @@ will also be retrieved from the region and printed to the console.
      // Do a compound search on last name and email using analyzerIndex
         gfsh>search lucene --name=analyzerIndex --region=example-region --queryStrings="lastName:hall~ AND email:Kris.Call@example.com" --defaultField=lastName
 
+     // Do a compound search on nested object with both 5035330001 AND 5036430001 in either home or office. 
+     // Note: 5035330001 is one of his home phone, 5036430001 is one of his office phone. 
+        gfsh>search lucene --name=nestedObjectIndex --region=/example-region --queryString="5035330001 AND 5036430001" --defaultField=zipAndPhoneBook.phones
+
+     // If query on 5035330001 AND 5036430002, it will not find the person, because the 2 phone numbers belong to different person's entry. 
+        gfsh>search lucene --name=nestedObjectIndex --region=/example-region --queryString="5035330001 AND 5036430002" --defaultField=zipAndPhoneBook.phones
+
+     // If query on 5035330001 OR 5036430002, it will find 2 people's entries
+        gfsh>search lucene --name=nestedObjectIndex --region=/example-region --queryString="5035330001 OR 5036430002" --defaultField=zipAndPhoneBook.phones
+
 3. Examine the Lucene index statistics
 
         gfsh>describe lucene index --name=simpleIndex --region=example-region
diff --git a/lucene/scripts/start.gfsh b/lucene/scripts/start.gfsh
index ac8356d..f757f96 100644
--- a/lucene/scripts/start.gfsh
+++ b/lucene/scripts/start.gfsh
@@ -14,10 +14,10 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 #
-start locator --name=locator --bind-address=127.0.0.1
+start locator --name=locator --bind-address=localhost
 
-start server --name=server1 --locators=127.0.0.1[10334] --server-port=0 --classpath=../build/classes/main --enable-time-statistics --statistic-archive-file=lucene1.gfs
-start server --name=server2 --locators=127.0.0.1[10334] --server-port=0 --classpath=../build/classes/main --enable-time-statistics --statistic-archive-file=lucene2.gfs
+start server --name=server1 --locators=localhost[10334] --server-port=0 --classpath=../build/classes/main --enable-time-statistics --statistic-archive-file=lucene1.gfs
+start server --name=server2 --locators=localhost[10334] --server-port=0 --classpath=../build/classes/main --enable-time-statistics --statistic-archive-file=lucene2.gfs
 
 ## simpleIndex uses default Lucene StandardAnalyzer
 create lucene index --name=simpleIndex --region=example-region --field=firstName,lastName
@@ -25,7 +25,14 @@ create lucene index --name=simpleIndex --region=example-region --field=firstName
 ## analyzerIndex uses both the default StandardAnalyzer and the KeywordAnalyzer
 create lucene index --name=analyzerIndex --region=example-region --field=lastName,email --analyzer=DEFAULT,org.apache.lucene.analysis.core.KeywordAnalyzer
 
+## nestedObjectIndex will index on nested objects or collection objects
+create lucene index --name=nestedObjectIndex --region=example-region --field=zipAndPhoneBook.phones --serializer=org.apache.geode.cache.lucene.FlatFormatSerializer
+
 create region --name=example-region --type=PARTITION --enable-statistics=true
 
+#create region --name=nestedobject-region --type=PARTITION --enable-statistics=true
+
 list members
 describe region --name=example-region
+#describe region --name=nestedobject-region
+
diff --git a/lucene/src/main/java/org/apache/geode/examples/lucene/EmployeeData.java b/lucene/src/main/java/org/apache/geode/examples/lucene/EmployeeData.java
index ce7c899..27893a9 100644
--- a/lucene/src/main/java/org/apache/geode/examples/lucene/EmployeeData.java
+++ b/lucene/src/main/java/org/apache/geode/examples/lucene/EmployeeData.java
@@ -15,6 +15,7 @@
 package org.apache.geode.examples.lucene;
 
 import java.io.Serializable;
+import java.util.Collection;
 
 public class EmployeeData implements Serializable {
   private static final long serialVersionUID = 1L;
@@ -25,15 +26,17 @@ public class EmployeeData implements Serializable {
   private String email;
   private int salary;
   private int hoursPerWeek;
+  private Collection<ZipAndPhone> zipAndPhoneBook;
 
   public EmployeeData(String firstName, String lastName, int emplNumber, String email, int salary,
-      int hoursPerWeek) {
+      int hoursPerWeek, Collection<ZipAndPhone> zipAndPhoneBook) {
     this.firstName = firstName;
     this.lastName = lastName;
     this.emplNumber = emplNumber;
     this.email = email;
     this.salary = salary;
     this.hoursPerWeek = hoursPerWeek;
+    this.zipAndPhoneBook = zipAndPhoneBook;
   }
 
   public String getFirstName() {
@@ -60,10 +63,14 @@ public class EmployeeData implements Serializable {
     return hoursPerWeek;
   }
 
+  public Collection<ZipAndPhone> getZipAndPhones() {
+    return this.zipAndPhoneBook;
+  }
+
   @Override
   public String toString() {
     return "EmployeeData [firstName=" + firstName + ", lastName=" + lastName + ", emplNumber="
         + emplNumber + ", email= " + email + ", salary=" + salary + ", hoursPerWeek=" + hoursPerWeek
-        + "]";
+        + ", zipAndPhoneBook=" + zipAndPhoneBook + "]";
   }
 }
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 9c19f5a..c26cd5e 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
@@ -14,6 +14,7 @@
  */
 package org.apache.geode.examples.lucene;
 
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Map;
 import java.util.Random;
@@ -29,6 +30,14 @@ import org.apache.geode.cache.lucene.LuceneService;
 import org.apache.geode.cache.lucene.LuceneServiceProvider;
 
 public class Example {
+  // These index names are predefined in gfsh scripts
+  final static String SIMPLE_INDEX = "simpleIndex";
+  final static String ANALYZER_INDEX = "analyzerIndex";
+  final static String NESTEDOBJECT_INDEX = "nestedObjectIndex";
+
+  // These region names are prefined in gfsh scripts
+  final static String EXAMPLE_REGION = "example-region";
+
   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)
@@ -41,16 +50,25 @@ public class Example {
 
     insertValues(region);
     query(cache);
+    queryNestedObject(cache);
     cache.close();
   }
 
   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");
+        .create(SIMPLE_INDEX, EXAMPLE_REGION, "firstName:Chris~2", "firstname");
     System.out.println("Employees with first names like Chris: " + query.findValues());
   }
 
+  private static void queryNestedObject(ClientCache cache) throws LuceneQueryException {
+    LuceneService lucene = LuceneServiceProvider.get(cache);
+    LuceneQuery<Integer, EmployeeData> query = lucene.createLuceneQueryFactory().create(
+        NESTEDOBJECT_INDEX, EXAMPLE_REGION, "5035330001 AND 5036430001", "zipAndPhoneBook.phones");
+    System.out.println(
+        "Employees with both phone number 5035330001 and 5036330001 either in office or home: "
+            + query.findValues());
+  }
 
   public static void insertValues(Map<Integer, EmployeeData> region) {
     // insert values into the region
@@ -66,8 +84,18 @@ public class Example {
       // Generating random number between 0 and 100000 for salary
       int salary = salaries[index % 5];
       int hoursPerWeek = hours[index % 5];
+
+      // create a home zipAndPhone with zip=9700x, phones=503533000x, 503633000x;
+      // an office zipAndPhone with zip=9800x, phones=503543000x, 503643000x
+      ArrayList<ZipAndPhone> zipAndPhoneBook = new ArrayList();
+      ZipAndPhone home = new ZipAndPhone(97000 + index,
+          new String[] {"50353" + (30000 + index), "50363" + (30000 + index)});
+      ZipAndPhone office = new ZipAndPhone(98000 + index,
+          new String[] {"50354" + (30000 + index), "50364" + (30000 + index)});
+      zipAndPhoneBook.add(home);
+      zipAndPhoneBook.add(office);
       EmployeeData val = new EmployeeData(firstNames[index], lastNames[index], emplNumber, email,
-          salary, hoursPerWeek);
+          salary, hoursPerWeek, zipAndPhoneBook);
       region.put(key, val);
     }
   }
diff --git a/lucene/src/main/java/org/apache/geode/examples/lucene/ZipAndPhone.java b/lucene/src/main/java/org/apache/geode/examples/lucene/ZipAndPhone.java
new file mode 100644
index 0000000..4aa5b04
--- /dev/null
+++ b/lucene/src/main/java/org/apache/geode/examples/lucene/ZipAndPhone.java
@@ -0,0 +1,41 @@
+/*
+ * 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.geode.examples.lucene;
+
+import java.io.Serializable;
+import java.util.Arrays;
+
+public class ZipAndPhone implements Serializable {
+  private int zip;
+  private String[] phones;
+
+  ZipAndPhone(int zip, String[] phones) {
+    this.zip = zip;
+    this.phones = phones;
+  }
+
+  public int getZip() {
+    return this.zip;
+  }
+
+  public String[] getPhones() {
+    return this.phones;
+  }
+
+  @Override
+  public String toString() {
+    return "(zip=" + zip + ", phones=" + Arrays.toString(phones) + ")";
+  }
+}

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