geode-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From u..@apache.org
Subject [21/33] incubator-geode git commit: GEODE-1545: add a test case for querying JSON in geode-lucene
Date Wed, 15 Jun 2016 16:17:54 GMT
GEODE-1545: add a test case for querying JSON in geode-lucene


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

Branch: refs/heads/feature/GEODE-420
Commit: ed32ceefba89b5cd295659faa3368971ee4adabb
Parents: d5dae19
Author: zhouxh <gzhou@pivotal.io>
Authored: Tue Jun 14 15:10:53 2016 -0700
Committer: zhouxh <gzhou@pivotal.io>
Committed: Tue Jun 14 15:14:39 2016 -0700

----------------------------------------------------------------------
 .../lucene/LuceneQueriesIntegrationTest.java    | 68 ++++++++++++++++++++
 .../cache/lucene/test/LuceneTestUtilities.java  | 27 ++++++++
 .../gemfire/cache/lucene/test/TestObject.java   |  7 ++
 3 files changed, 102 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/ed32ceef/geode-lucene/src/test/java/com/gemstone/gemfire/cache/lucene/LuceneQueriesIntegrationTest.java
----------------------------------------------------------------------
diff --git a/geode-lucene/src/test/java/com/gemstone/gemfire/cache/lucene/LuceneQueriesIntegrationTest.java
b/geode-lucene/src/test/java/com/gemstone/gemfire/cache/lucene/LuceneQueriesIntegrationTest.java
index 536f5c3..95f5781 100644
--- a/geode-lucene/src/test/java/com/gemstone/gemfire/cache/lucene/LuceneQueriesIntegrationTest.java
+++ b/geode-lucene/src/test/java/com/gemstone/gemfire/cache/lucene/LuceneQueriesIntegrationTest.java
@@ -42,6 +42,8 @@ import com.gemstone.gemfire.cache.RegionShortcut;
 import com.gemstone.gemfire.cache.execute.FunctionException;
 import com.gemstone.gemfire.cache.lucene.test.TestObject;
 import com.gemstone.gemfire.cache.query.QueryException;
+import com.gemstone.gemfire.pdx.JSONFormatter;
+import com.gemstone.gemfire.pdx.PdxInstance;
 import com.gemstone.gemfire.test.junit.categories.IntegrationTest;
 
 /**
@@ -96,6 +98,12 @@ public class LuceneQueriesIntegrationTest extends LuceneIntegrationTest
{
     // query will be--field1:one field1:three
     verifyQuery("field1:one@three", DEFAULT_FIELD, "A", "B", "C");
     
+    HashMap expectedResults = new HashMap();
+    expectedResults.put("A", new TestObject(value1, value1));
+    expectedResults.put("B", new TestObject(value2, value2));
+    expectedResults.put("C", new TestObject(value3, value3));
+    verifyQuery("field1:one@three", expectedResults);
+    
     // keyword analyzer, this query will only match the entry that exactly matches
     // this query string will be parsed as "one three"
     // but keyword analyzer will parse one@three to be "one three"
@@ -155,6 +163,29 @@ public class LuceneQueriesIntegrationTest extends LuceneIntegrationTest
{
   }
 
   @Test()
+  public void queryJsonObject() throws ParseException {
+    Map<String, Analyzer> fields = new HashMap<String, Analyzer>();
+    fields.put("name", null);
+    fields.put("lastName", null);
+    fields.put("address", null);
+    luceneService.createIndex(INDEX_NAME, REGION_NAME, fields);
+    Region region = cache.createRegionFactory(RegionShortcut.PARTITION)
+      .create(REGION_NAME);
+    final LuceneIndex index = luceneService.getIndex(INDEX_NAME, REGION_NAME);
+
+    //Put two values with some of the same tokens
+    PdxInstance pdx1 = insertAJson(region, "jsondoc1");
+    PdxInstance pdx2 = insertAJson(region, "jsondoc2");
+    PdxInstance pdx10 = insertAJson(region, "jsondoc10");
+    index.waitUntilFlushed(60000);
+
+    HashMap expectedResults = new HashMap();
+    expectedResults.put("jsondoc1", pdx1);
+    expectedResults.put("jsondoc10", pdx10);
+    verifyQuery("name:jsondoc1*", expectedResults);
+  }
+
+  @Test()
   public void throwFunctionExceptionWhenGivenBadQuery() {
     LuceneService luceneService = LuceneServiceProvider.get(cache);
     luceneService.createIndex(INDEX_NAME, REGION_NAME, "text");
@@ -178,6 +209,36 @@ public class LuceneQueriesIntegrationTest extends LuceneIntegrationTest
{
     }
 
   }
+  
+  private PdxInstance insertAJson(Region region, String key) {
+    String jsonCustomer = "{"
+        + "\"name\": \""+key+"\","
+        + "\"lastName\": \"Smith\","
+        + " \"age\": 25,"
+        + "\"address\":"
+        + "{"
+        + "\"streetAddress\": \"21 2nd Street\","
+        + "\"city\": \"New York\","
+        + "\"state\": \"NY\","
+        + "\"postalCode\": \"10021\""
+        + "},"
+        + "\"phoneNumber\":"
+        + "["
+        + "{"
+        + " \"type\": \"home\","
+        + "\"number\": \"212 555-1234\""
+        + "},"
+        + "{"
+        + " \"type\": \"fax\","
+        + "\"number\": \"646 555-4567\""
+        + "}"
+        + "]"
+        + "}";
+
+    PdxInstance pdx = JSONFormatter.fromJSON(jsonCustomer);
+    region.put(key, pdx);
+    return pdx;
+  }
 
   private void verifyQuery(String query, String defaultField, String ... expectedKeys) throws
ParseException {
     final LuceneQuery<String, Object> queryWithStandardAnalyzer = luceneService.createLuceneQueryFactory().create(
@@ -185,6 +246,13 @@ public class LuceneQueriesIntegrationTest extends LuceneIntegrationTest
{
 
     verifyQueryKeys(queryWithStandardAnalyzer, expectedKeys);
   }
+  
+  private void verifyQuery(String query, HashMap expectedResults) throws ParseException {
+    final LuceneQuery<String, Object> queryWithStandardAnalyzer = luceneService.createLuceneQueryFactory().create(
+      INDEX_NAME, REGION_NAME, query);
+
+    verifyQueryKeyAndValues(queryWithStandardAnalyzer, expectedResults);
+  }
 
   private static class MyCharacterTokenizer extends CharTokenizer {
     @Override

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/ed32ceef/geode-lucene/src/test/java/com/gemstone/gemfire/cache/lucene/test/LuceneTestUtilities.java
----------------------------------------------------------------------
diff --git a/geode-lucene/src/test/java/com/gemstone/gemfire/cache/lucene/test/LuceneTestUtilities.java
b/geode-lucene/src/test/java/com/gemstone/gemfire/cache/lucene/test/LuceneTestUtilities.java
index 6ef93bf..d7150d9 100644
--- a/geode-lucene/src/test/java/com/gemstone/gemfire/cache/lucene/test/LuceneTestUtilities.java
+++ b/geode-lucene/src/test/java/com/gemstone/gemfire/cache/lucene/test/LuceneTestUtilities.java
@@ -21,10 +21,12 @@ package com.gemstone.gemfire.cache.lucene.test;
 import static org.junit.Assert.*;
 
 import java.util.Arrays;
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Set;
 import java.util.function.Consumer;
 import java.util.stream.Collectors;
+import org.apache.logging.log4j.Logger;
 
 import com.gemstone.gemfire.cache.Cache;
 import com.gemstone.gemfire.cache.asyncqueue.AsyncEventQueue;
@@ -38,6 +40,9 @@ import com.gemstone.gemfire.cache.lucene.LuceneServiceProvider;
 import com.gemstone.gemfire.cache.lucene.internal.LuceneIndexForPartitionedRegion;
 import com.gemstone.gemfire.cache.lucene.internal.LuceneServiceImpl;
 import com.gemstone.gemfire.internal.cache.LocalRegion;
+import com.gemstone.gemfire.internal.logging.LogService;
+import com.gemstone.gemfire.pdx.JSONFormatter;
+import com.gemstone.gemfire.pdx.PdxInstance;
 
 public class LuceneTestUtilities {
   public static final String INDEX_NAME = "index";
@@ -96,6 +101,28 @@ public class LuceneTestUtilities {
     assertEquals(expectedKeySet, actualKeySet);
   }
 
+  /**
+   * Verify that a query returns the expected map of key-value. Ordering is ignored.
+   */
+  public static <K> void verifyQueryKeyAndValues(LuceneQuery<K,Object> query,
HashMap expectedResults) {
+    HashMap actualResults = new HashMap<>();
+    final LuceneQueryResults<K, Object> results = query.search();
+    while(results.hasNextPage()) {
+      results.getNextPage().stream()
+        .forEach(struct -> {
+          Object value = struct.getValue();
+          if (value instanceof PdxInstance) {
+            PdxInstance pdx = (PdxInstance)value;
+            String jsonString = JSONFormatter.toJSON(pdx);
+            actualResults.put(struct.getKey(), pdx);
+          } else {
+            actualResults.put(struct.getKey(), value);
+          }
+        });
+    }
+    assertEquals(expectedResults, actualResults);
+  }
+  
   public static void pauseSender(final Cache cache) {
     final AsyncEventQueueImpl queue = (AsyncEventQueueImpl) getIndexQueue(cache);
     queue.getSender().pause();

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/ed32ceef/geode-lucene/src/test/java/com/gemstone/gemfire/cache/lucene/test/TestObject.java
----------------------------------------------------------------------
diff --git a/geode-lucene/src/test/java/com/gemstone/gemfire/cache/lucene/test/TestObject.java
b/geode-lucene/src/test/java/com/gemstone/gemfire/cache/lucene/test/TestObject.java
index a2b249e..9e984bd 100644
--- a/geode-lucene/src/test/java/com/gemstone/gemfire/cache/lucene/test/TestObject.java
+++ b/geode-lucene/src/test/java/com/gemstone/gemfire/cache/lucene/test/TestObject.java
@@ -49,4 +49,11 @@ public class TestObject implements Serializable {
   public void setField2(final String field2) {
     this.field2 = field2;
   }
+  
+  @Override
+  public boolean equals(Object obj) {
+    TestObject testObject = (TestObject)obj;
+    return (testObject.field1.equals(field1) && testObject.field2.equals(field2));
+  }
+
 }


Mime
View raw message