Skip to content

Commit

Permalink
Merge pull request apache#34 from apache/feature/GEODE-3789
Browse files Browse the repository at this point in the history
Feature/geode 3789
  • Loading branch information
gesterzhou authored Oct 26, 2017
2 parents abcff61 + e1f2e15 commit 1ab4c24
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 9 deletions.
14 changes: 12 additions & 2 deletions lucene/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ 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"
...

2. Try different Lucene searches for data in example-region

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
Expand All @@ -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.[email protected]" --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
Expand Down
10 changes: 7 additions & 3 deletions lucene/scripts/start.gfsh
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,22 @@
# 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

## 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

list members
describe region --name=example-region

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
Expand All @@ -60,10 +63,14 @@ public int getHoursPerWeek() {
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 + "]";
}
}
32 changes: 30 additions & 2 deletions lucene/src/main/java/org/apache/geode/examples/lucene/Example.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,6 +30,14 @@
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)
Expand All @@ -41,16 +50,25 @@ public static void main(String[] args) throws LuceneQueryException {

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
Expand All @@ -66,8 +84,18 @@ public static void insertValues(Map<Integer, EmployeeData> region) {
// 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);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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) + ")";
}
}

0 comments on commit 1ab4c24

Please sign in to comment.