How to use filter or compound operator with Slope in Atlas search?

I am trying to use slope MongoDB search, I am wondering if I can use it with filter.
I have huge collection data where if I can able to filter with filed - date and clientid and use slope in it, but I found out I cannot use compound with slope.

In below query it is finding the phrase in whole collection, but I think If I found a solution to filter out that will be reduce a lot of time.

db.getCollection("collection").aggregate([{"$search":{"index":"fulltext","phrase":{"path":["headline","subtitle","fulltext"],
    "query":["ON TIME","FLIGHT"],"slop":10}}}]  )

slop works with the phrase query operator, which itself can work as a clause anywhere that query operators can be used, including within a compound/filter clause.

The main difference between using a filter versus a must clause is whether the queries are being used for relevancy scoring or purely for filtering.

2 Likes

Adding to this - "query":["ON TIME","FLIGHT"] do I get all the data which has these two keywords - “ON TIME” and “FLIGHT” or if any of these keywords found it will return the data.

My scenario is I need to search data based on phrases with slope ex -
Previously I am using below query for slope, but as I noticed it returns data which have any of these keywords present, but I need if both keywords present in certain distance then it will give results.

[
      {
        $search: {
          index: "fulltext",
          compound: {
            filter: [
              {
                range: {
                  path: "pubdateRange",
                  gte: ISODate("2023-05-01T00:00:00.000Z"),
                  lte: ISODate("2023-05-20T18:29:59.000Z"),
                },
              },
            ],
            must: [
              {
                phrase: {
                  query: "AB1",
                  path: ["clientidArray"],
                },
              },

              {
                phrase: {
                  query: ["ON TIME","FLIGHT"],
   
                  path: ["headline", "subtitle", "fulltext"],
                  ...(slop ? { slop } : {}),
                },
              },
            ],
          },
        },
      }
]

@Erik_Hatcher do you have any suggestions according to the query I m using?

To achieve AND behavior, where all clauses are mandatory, you will need to use the compound operator with each of those separated into separate must clauses.

You mean something like this (I am using exactly this query, and slope is giving me OR operator results) -

[
    {
        "$search":{
            "index":"fulltext",
            "compound":{
                "filter":[
                    {
                        "range":{
                            "path":"pubdateRange",
                            "gte":"2023-05-24T00:00:00.000Z",
                            "lte":"2023-05-25T18:29:59.000Z"
                        }
                    }
                ],
                "must":[
                    {
                        "phrase":{
                            "query":"I0027",
                            "path":[
                                "clientidArray"
                            ]
                        }
                    },
                    {
                        "phrase":{
                            "query":"on time",
                            "airport",
                            "path":[
                                "headline",
                                "subtitle",
                                "fulltext"
                            ],
                            "slop":10
                        }
                    }
                ]
            }
        }
    },
    {
        "$project":{
            "articleid":1,
            "_id":0
        }
    }
]

Could you please recommends me or write a query according to above with AND Operator for slope ?

Split that second phrase operator into 2 different ones within the must array, one for each query phrase you have. If you put multiple paths or queries into a single operator, it OR’s (should’s) them, rather than ANDing them.

Note that your posts say “slope” but use the correct parameter name “slop” (as in how sloppy the phrase can be) - just wanted to point that out in case that is confusing for others reading this.