[SERVER-49343] logical find operators: $or, $and, $not, $nor... return matching field count for sort Created: 07/Jul/20  Updated: 27/Oct/23  Resolved: 21/Sep/20

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: 4.4.0-rc12
Fix Version/s: None

Type: New Feature Priority: Trivial - P5
Reporter: Andrew Hodel Assignee: Katya Kamenieva
Resolution: Community Answered Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Sprint: Query 2020-09-21, Query 2020-10-05
Participants:

 Description   

I find the text search inadequate at times when splitting the search by word and performing an $or per field with a regex is much more useful.

 

Would it be possible to add an indicator to each result that counts the number of field matches so that I can sort by relevance within mongodb rather than having to parse through the results after the find()?

 

Will you, it's a much improved search offering for your product.



 Comments   
Comment by Katya Kamenieva [ 21/Sep/20 ]

andrewhodel@gmail.com Thank you for your feedback. Currently there is no plan so support the requested functionality. Feel free to start conversation on the Community forum if you want to discuss more

Comment by Andrew Hodel [ 20/Sep/20 ]

That will work, it is odd that there is not a flagged option that simply
returns this for all query and field matches as a single field for count in
the response.

The way this is built I will have to do another comparison loop to relate
the data, which I am sure is already done internally so I am confused at
the programming choices.

It will work though.

On Sat, Sep 19, 2020 at 6:02 PM Katya Kamenieva (Jira) <jira@mongodb.org>

Comment by Katya Kamenieva [ 20/Sep/20 ]

Hi andrewhodel@gmail.com, I don't think I understand what do you mean by "I am asking for matches per field counts". Please provide example of the input documents, and the result of the query that you are looking for.

What I wanted to illustrate is that for data like this:

db.movies.insert([
{"_id": 1, "title": "Happy people"}, 
{"_id": 2, "title": "Happy, happy life"}
])

And query like this, with the $regexFindAll and $size in projection:

db.movies.find(
{ title: /\bhappy\b/i  },
{
  numMatchesInTitle: {
    $size: 
      { $regexFindAll: 
        { input: "$title", regex: /\bhappy\b/i }
    }
  }
}
)

The output tells that the word "happy" was found one time for the "title" field of the first document, and two times for the second document.

  "_id": 1,
  "numMatchesInTitle": 1
}
{
  "_id": 2,
  "numMatchesInTitle": 2
}

 

Comment by Andrew Hodel [ 19/Sep/20 ]

No, this is about returning counts per field.

I know how to perform the search. I am asking for matches per field counts
to be returned for relevance matching.

On Fri, Sep 18, 2020 at 6:31 PM Katya Kamenieva (Jira) <jira@mongodb.org>

Comment by Katya Kamenieva [ 18/Sep/20 ]

andrewhodel@gmail.com, if I understood your question correctly, here is example how you could do this using $regexFindAll in version 4.2 and above.
Find the happiest movies where `title` or `plot` contains "happy" and count number of occurrences of "happy" in `title` and in `plot`:

db.movies.find({ 
$or: [ 
  	{ title: /\bhappy\b/i }, 
  	{ fullplot: /\bhappy\b/i } 
] },{
  numMatchesInTitle: {
    $size: 
      { $regexFindAll: 
        { input: "$title", regex: /\bhappy\b/i }
    }
  },
  numMatchesInPlot: {
    $size: 
      { $regexFindAll: 
        { input: "$fullplot", regex: /\bhappy\b/i }
    }
  }
})

Does this help?

Comment by Andrew Hodel [ 18/Aug/20 ]

Sure.

 

You just search on many fields using a regex per word (or letter if you are that way).

 

Then you need to get back a counter per field that shows how many matches there were for each field.

 

The problem with the text search is that it is not capable of searching many fields.  I have used it, it is inadequate.

Comment by Katya Kamenieva [ 17/Aug/20 ]

Hi andrewhodel@gmail.com, do you use MongoDB text search and text indexes or is this regarding the Atlas Search? Could you provide an example for the search query and regex that you are referring to?

Thank you

Generated at Thu Feb 08 05:19:35 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.