GEODE-11: Adding a test of using a KeywordAnalyzer

Testing that a KeywordAnalyzer does not tokenize fields.


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

Branch: refs/heads/feature/GEODE-835
Commit: 8d1fa0c32de3369e39c05bc9a48c64149df09a98
Parents: 9d23a69
Author: Dan Smith <upthewatersp...@apache.org>
Authored: Wed May 18 15:27:35 2016 -0700
Committer: Dan Smith <upthewatersp...@apache.org>
Committed: Thu May 19 17:02:05 2016 -0700

----------------------------------------------------------------------
 .../lucene/LuceneQueriesIntegrationTest.java    | 44 ++++++++++++++++++++
 .../cache/lucene/test/LuceneTestUtilities.java  | 23 +++++++++-
 .../gemfire/cache/lucene/test/TestObject.java   | 29 ++++++++++++-
 3 files changed, 93 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/8d1fa0c3/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 2262339..15f5747 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
@@ -16,15 +16,25 @@
  */
 package com.gemstone.gemfire.cache.lucene;
 
+import static 
com.gemstone.gemfire.cache.lucene.test.LuceneTestUtilities.verifyQueryKeys;
 import static org.hamcrest.Matchers.isA;
 import static org.junit.Assert.*;
 
+import java.util.HashMap;
+import java.util.Map;
+
 import com.gemstone.gemfire.cache.Region;
 import com.gemstone.gemfire.cache.RegionShortcut;
 import com.gemstone.gemfire.cache.execute.FunctionException;
+import com.gemstone.gemfire.cache.lucene.test.LuceneTestUtilities;
+import com.gemstone.gemfire.cache.lucene.test.TestObject;
 import com.gemstone.gemfire.cache.query.QueryException;
 import com.gemstone.gemfire.test.junit.categories.IntegrationTest;
 
+import org.apache.lucene.analysis.Analyzer;
+import org.apache.lucene.analysis.core.KeywordAnalyzer;
+import org.apache.lucene.analysis.standard.StandardAnalyzer;
+import org.apache.lucene.queryparser.classic.ParseException;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.experimental.categories.Category;
@@ -41,6 +51,33 @@ public class LuceneQueriesIntegrationTest extends 
LuceneIntegrationTest {
   protected static final String REGION_NAME = "index";
 
   @Test()
+  public void shouldNotTokenizeWordsWithKeywordAnalyzer() throws 
ParseException {
+    Map<String, Analyzer> fields = new HashMap<String, Analyzer>();
+    fields.put("field1", new StandardAnalyzer());
+    fields.put("field2", new KeywordAnalyzer());
+    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
+    String value1 = "one three";
+    region.put("A", new TestObject(value1, value1));
+    String value2 = "one two three";
+    region.put("B", new TestObject(value2, value2));
+
+    index.waitUntilFlushed(60000);
+
+    //Using the standard analyzer, this query will match both results
+    verifyQuery("field1:\"one three\"", "A", "B");
+
+    //Using the keyword analyzer, this query will only match the entry that 
exactly matches
+    verifyQuery("field2:\"one three\"", "A");
+
+
+  }
+
+  @Test()
   public void throwFunctionExceptionWhenGivenBadQuery() {
     LuceneService luceneService = LuceneServiceProvider.get(cache);
     luceneService.createIndex(INDEX_NAME, REGION_NAME, "text");
@@ -65,5 +102,12 @@ public class LuceneQueriesIntegrationTest extends 
LuceneIntegrationTest {
 
   }
 
+  private void verifyQuery(String query, String ... expectedKeys) throws 
ParseException {
+    final LuceneQuery<Object, Object> queryWithStandardAnalyzer = 
luceneService.createLuceneQueryFactory().create(
+      INDEX_NAME, REGION_NAME, query);
+
+    verifyQueryKeys(queryWithStandardAnalyzer, expectedKeys);
+  }
+
 
 }

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/8d1fa0c3/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 61355c4..7a3ef04 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
@@ -18,13 +18,20 @@
  */
 package com.gemstone.gemfire.cache.lucene.test;
 
-import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.*;
 
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
 import java.util.function.Consumer;
+import java.util.stream.Collectors;
 
 import com.gemstone.gemfire.cache.Cache;
 import com.gemstone.gemfire.cache.asyncqueue.AsyncEventQueue;
 import com.gemstone.gemfire.cache.lucene.LuceneIndex;
+import com.gemstone.gemfire.cache.lucene.LuceneQuery;
+import com.gemstone.gemfire.cache.lucene.LuceneQueryResults;
+import com.gemstone.gemfire.cache.lucene.LuceneResultStruct;
 import com.gemstone.gemfire.cache.lucene.LuceneService;
 import com.gemstone.gemfire.cache.lucene.LuceneServiceProvider;
 import 
com.gemstone.gemfire.cache.lucene.internal.LuceneIndexForPartitionedRegion;
@@ -61,4 +68,18 @@ public class LuceneTestUtilities {
     boolean flushed = index.waitUntilFlushed(60000);
     assertTrue(flushed);
   }
+
+  /**
+   * Verify that a query returns the expected list of keys. Ordering is 
ignored.
+   */
+  public static <K> void  verifyQueryKeys(LuceneQuery<K,Object> query,K ... 
expectedKeys) {
+    Set<K> expectedKeySet = new HashSet<>(Arrays.asList(expectedKeys));
+    Set<K> actualKeySet = new HashSet<>(Arrays.asList(expectedKeys));
+    final LuceneQueryResults<K, Object> results = query.search();
+    while(results.hasNextPage()) {
+      results.getNextPage().stream()
+        .forEach(struct -> actualKeySet.add(struct.getKey()));
+    }
+    assertEquals(expectedKeySet, actualKeySet);
+  }
 }

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/8d1fa0c3/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 3287b9f..a2b249e 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
@@ -22,6 +22,31 @@ import java.io.Serializable;
 
 public class TestObject implements Serializable {
 
-  String field1 = "hello world";
-  String field2 = "this is a field";
+  private String field1 = "hello world";
+  private String field2 = "this is a field";
+
+  public TestObject() {
+
+  }
+
+  public TestObject(final String field1, final String field2) {
+    this.field1 = field1;
+    this.field2 = field2;
+  }
+
+  public String getField1() {
+    return field1;
+  }
+
+  public void setField1(final String field1) {
+    this.field1 = field1;
+  }
+
+  public String getField2() {
+    return field2;
+  }
+
+  public void setField2(final String field2) {
+    this.field2 = field2;
+  }
 }

Reply via email to