Quantcast
Channel: Active questions tagged mongodb-atlas - Stack Overflow
Viewing all articles
Browse latest Browse all 283

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

$
0
0

I am trying to replicate the below JSON in Java to search using Atlas Search, using the Aggregates class, rather than just creating all of this by a new Document().

[  {    $search:      {        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 the phrase or the autocomplete query, which means adding the minimumShouldMatch: 1 line like above.

So far, I have this as my code:

SearchOperator filterClause = compound()        .must(Arrays.asList(...)); //irrelevant to question        Document searchQuery =                    new Document("autocomplete",                    new Document("query", "apple pie")                        .append("path", "name")                        .append("fuzzy", new Document()));        Document searchQuery1=                    new Document("phrase",                    new Document("query", "apple pie")                        .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")

Which works so far as recreating the above JSON minus the minimumMatchSearch. That has to be added within the should option, but I’m not sure how to add to that.

I've specifically tried

.should(List.of(SearchOperator.of(searchQuery), SearchOperator.of(searchQuery1)), n ew Document("minimumMatchSearch", 1)),

which results in the error:

Request processing failed; nested exception is org.bson.codecs.configuration.CodecConfigurationException: Unable to encode a Bson implementation: ConstructibleBsonElement{baseElement=Document{{minimumMatchSearch=1}}, appendedElementValue=Document{{}}}] with root causejava.lang.IllegalStateException: baseElement value must be a document, but it is INT32

when I try to use the mongoCollection.aggregate(); call at the end after combining the search stage with the project and limit stage (not within sample code).


Viewing all articles
Browse latest Browse all 283

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>