My example is searching for users of my application by name. I created one document for each user, and that document contains every possible string that you can search for.
For example, the document for user "John Smith" has one string: search input delimited by spaces: "joh ohn smi smit mith (etc)".
Here the code I used to get this to work. The "id" is the id for the user in my backend datastore.
private void createSearchableUserDoc(String id, String displayName) {
List<String> substrings = buildAllSubstrings(displayName);
String combinedString = combine(substrings, " ");
// The input for this looks like "CHR CHRI CHRIS HRI HRIS" etc...
createUserDocument(id, combinedString);
}
private List<String> buildAllSubstrings(String displayName) {
List<String> substrings = new ArrayList<String>();
for (String word : displayName.split(" ")) {
int wordSize = 1;
while (true) {
for (int i = 0; i < word.length() - wordSize + 1; i++) {
substrings.add(word.substring(i, i + wordSize));
}
if (wordSize == word.length())
break;
wordSize++;
}
}
return substrings;
}
private String combine(List<String> strings, String glue) {
int k = strings.size();
if (k == 0)
return null;
StringBuilder out = new StringBuilder();
out.append(strings.get(0));
for (int x = 1; x < k; ++x)
out.append(glue).append(strings.get(x));
return out.toString();
}
private void createUserDocument(String id, String searchableSubstring) {
Builder docBuilder = Document
.newBuilder()
.setId(id)
.addField(
Field.newBuilder().setName("Display_Name")
.setText(searchableSubstring));
addDocToIndex(docBuilder.build());
}
private void addDocToIndex(Document document) {
Index index = getUserDocIndex();
try {
index.put(document);
} catch (PutException e) {
log.severe("Error putting document in index... trying again.");
if (StatusCode.TRANSIENT_ERROR.equals(e.getOperationResult().getCode())) {
index.put(document);
}
}
}
public static Index getUserDocIndex() {
IndexSpec indexSpec = IndexSpec.newBuilder().setName("USER_DOC_INDEX").build();
Index index = SearchServiceFactory.getSearchService().getIndex(indexSpec);
return index;
}
To perform a search I did this:
Query query = Query.newBuilder().build("Display_Name" + "=" + searchText);
Index userDocIndex = getUserDocIndex();
Results<ScoredDocument> matchingUsers = userDocIndex.search(query);