I am trying to replicate the below JSON in Java, using the Aggregates class, rather than just creating all of this by a new Document()
.
/**
* index: The name of the Search index.
* text: Analyzed search, with required fields of query and path, the analyzed field(s) to search.
* compound: Combines ops.
* span: Find in text field regions.
* exists: Test for presence of a field.
* near: Find near number or date.
* range: Find in numeric or date range.
*/
[
{
$search:
/**
* index: The name of the Search index.
* text: Analyzed search, with required fields of query and path, the analyzed field(s) to search.
* compound: Combines ops.
* span: Find in text field regions.
* exists: Test for presence of a field.
* near: Find near number or date.
* range: Find in numeric or date range.
*/
{
compound: {
filter: [
{
compound: {
must: [
...
],
},
},
],
should: [
{
autocomplete: {
query: "apple pie",
path: "name",
fuzzy: {},
},
},
{
phrase: {
query: "apple pie",
path: "name",
},
},
],
minimumShouldMatch: 1,
},
scoreDetails: true,
index: "default",
},
},
]
I want to match at least one of the “should” statements so that it’ll match at least the phrase or the autocomplete query.
My java code is structured like the example from Mongo here: Atlas Search Java
SearchOperator filterClause = compound()
.must(Arrays.asList(...));
Document searchQuery =
new Document("autocomplete",
new Document("query", "apple pie") // query is the word or phrase to be searched
.append("path", "name")
.append("fuzzy", new Document()));
Document searchQuery1=
new Document("phrase",
new Document("query", "apple pie") // query is the word or phrase to be searched
.append("path", "name"));
Bson searchStage = Aggregates.search(
SearchOperator.compound()
.filter(List.of(filterClause))
.should(List.of(SearchOperator.of(searchQuery), SearchOperator.of(searchQuery1))),
searchOptions().option("scoreDetails", true).option("index", "default")
The above works so far, but I am getting stuck on how to add minumumMatchSearch
to that last stage. It has to be within the should
option, but I’m not sure how to add to that.