How to add minumimShouldMatch to a search query with Java Aggregates.search?

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.

Found out that this is a very simple solution and came back to answer my own question.

In the last step of combining everything to create the search stage, one of the compound() options is .minimumShouldMatch() option. So that line would look like:

Bson searchStage = Aggregates.search(
    SearchOperator.compound()
        .filter(List.of(filterClause))
        .should(List.of( <search queries> ))
        .minumumShouldMatch(1),
    searchOptions().option( <options> ));
1 Like

sweet. i wonder if you get a badge for solving your own question.

my java skills are lacking but the aggregate looks correct!

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.