Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix suggestion classes for Term, Phrase, and Completion. #477

Merged
merged 7 commits into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

### Fixed
- Fix version and build ([#254](https://github.com/opensearch-project/opensearch-java/pull/254))
- Fixed Suggesters for Completion, Term, and Phrase and refactored the Suggestion class ([#477](https://github.com/opensearch-project/opensearch-java/pull/477))

### Security

Expand Down
242 changes: 242 additions & 0 deletions USER_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
- [Search for the documents](#search-for-the-documents)
- [Get raw JSON results](#get-raw-json-results)
- [Search documents using a match query](#search-documents-using-a-match-query)
- [Search documents using suggesters](#search-documents-using-suggesters)
- [App Data class](#app-data-class)
- [Using completion suggester](#using-completion-suggester)
- [Using term suggester](#using-term-suggester)
- [Using phrase suggester](#using-phrase-suggester)
- [Bulk requests](#bulk-requests)
- [Aggregations](#aggregations)
- [Delete the document](#delete-the-document)
Expand Down Expand Up @@ -169,6 +174,243 @@ for (int i = 0; i < searchResponse.hits().hits().size(); i++) {
}
```

## Search documents using suggesters

### App Data class

```java
public static class AppData {

private int intValue;
private String msg;

public int getIntValue() {
return intValue;
}

public void setIntValue(int intValue) {
this.intValue = intValue;
}

public String getMsg() {
return msg;
}

public void setMsg(String msg) {
this.msg = msg;
}
}
```

### Using completion suggester

```java
String index = "completion-suggester";

Property intValueProp = new Property.Builder()
.long_(v -> v)
.build();
Property msgCompletionProp = new Property.Builder()
.completion(c -> c)
.build();
client.indices().create(c -> c
.index(index)
.mappings(m -> m
.properties("intValue", intValueProp)
.properties("msg", msgCompletionProp)));

AppData appData = new AppData();
appData.setIntValue(1337);
appData.setMsg("foo");

client.index(b -> b
.index(index)
.id("1")
.document(appData)
.refresh(Refresh.True));

appData.setIntValue(1338);
appData.setMsg("foobar");

client.index(b -> b
.index(index)
.id("2")
.document(appData)
.refresh(Refresh.True));

String suggesterName = "msgSuggester";

CompletionSuggester completionSuggester = FieldSuggesterBuilders.completion()
.field("msg")
.size(1)
.build();
FieldSuggester fieldSuggester = new FieldSuggester.Builder().prefix("foo")
.completion(completionSuggester)
.build();
Suggester suggester = new Suggester.Builder()
.suggesters(Collections.singletonMap(suggesterName, fieldSuggester))
.build();
SearchRequest searchRequest = new SearchRequest.Builder()
.index(index)
.suggest(suggester)
.build();

SearchResponse<AppData> response = client.search(searchRequest, AppData.class);
```

### Using term suggester

```java
String index = "term-suggester";

// term suggester does not require a special mapping
client.indices().create(c -> c
.index(index));

AppData appData = new AppData();
appData.setIntValue(1337);
appData.setMsg("foo");

client.index(b -> b
.index(index)
.id("1")
.document(appData)
.refresh(Refresh.True));

appData.setIntValue(1338);
appData.setMsg("foobar");

client.index(b -> b
.index(index)
.id("2")
.document(appData)
.refresh(Refresh.True));

String suggesterName = "msgSuggester";

TermSuggester termSuggester = FieldSuggesterBuilders.term()
.field("msg")
.size(1)
.build();
FieldSuggester fieldSuggester = new FieldSuggester.Builder().text("fool")
.term(termSuggester)
.build();
Suggester suggester = new Suggester.Builder()
.suggesters(Collections.singletonMap(suggesterName, fieldSuggester))
.build();
SearchRequest searchRequest = new SearchRequest.Builder()
.index(index)
.suggest(suggester)
.build();

SearchResponse<AppData> response = client.search(searchRequest, AppData.class);
```

### Using phrase suggester

```java
String index = "phrase-suggester";

String settingsJson =
"""
{
"index": {
"analysis": {
"analyzer": {
"trigram": {
"type": "custom",
"tokenizer": "standard",
"filter": [
"lowercase",
"shingle"
]
}
},
"filter": {
"shingle": {
"type": "shingle",
"min_shingle_size": 2,
"max_shingle_size": 3
}
}
}
}
}
""";

String mappingJson =
"""
{
"properties": {
"msg": {
"type": "text",
"fields": {
"trigram": {
"type": "text",
"analyzer": "trigram"
}
}
}
}
}
""";


ObjectMapper mapper = new JsonMapper();
harshavamsi marked this conversation as resolved.
Show resolved Hide resolved
JsonpMapper jsonpMapper = new JacksonJsonpMapper(mapper);
IndexSettings settings;
try (JsonParser settingsParser = new JacksonJsonpParser(mapper.createParser(settingsJson))) {
reta marked this conversation as resolved.
Show resolved Hide resolved
settings = IndexSettings._DESERIALIZER.deserialize(settingsParser, jsonpMapper);
}

TypeMapping mapping;
try (JsonParser mappingParser = new JacksonJsonpParser(mapper.createParser(mappingJson))) {
mapping = TypeMapping._DESERIALIZER.deserialize(mappingParser, jsonpMapper);
}

client.indices().create(c -> c
.index(index)
.settings(settings)
.mappings(mapping));

AppData appData = new AppData();
appData.setIntValue(1337);
appData.setMsg("Design Patterns");

client.index(b -> b
.index(index)
.id("1")
.document(appData)
.refresh(Refresh.True));

appData.setIntValue(1338);
appData.setMsg("Software Architecture Patterns Explained");

client.index(b -> b
.index(index)
.id("2")
.document(appData)
.refresh(Refresh.True));

String suggesterName = "msgSuggester";

PhraseSuggester phraseSuggester = FieldSuggesterBuilders.phrase()
.field("msg.trigram")
.build();
FieldSuggester fieldSuggester = new FieldSuggester.Builder().text("design paterns")
.phrase(phraseSuggester)
.build();
Suggester suggester = new Suggester.Builder()
.suggesters(Collections.singletonMap(suggesterName, fieldSuggester))
.build();
SearchRequest searchRequest = new SearchRequest.Builder()
.index(index)
.suggest(suggester)
.build();

SearchResponse<AppData> response = client.search(searchRequest, AppData.class);
```

## Bulk requests

```java
Expand Down
4 changes: 4 additions & 0 deletions java-client/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ java {
}
}

java.sourceSets["test"].java {
harshavamsi marked this conversation as resolved.
Show resolved Hide resolved
srcDir("src/test/resources")
}

tasks.withType<ProcessResources> {
expand(
"version" to version,
Expand Down
Loading