Search code examples
springmongodbspring-data-mongodbfull-text-indexing

Spring Data MongoDB with text index: difference between matchingany and matchingphrase


I am using MongoDB and Spring for an application

I am using a text index on my collection.

I found two methods:

  • matchingany
  • matchingphrase

But I am unable to understand the difference.

Please help me to understand them.


Solution

  • If you want a match on multiple words forming a phrase then use matchingPhrase, if you want a match on at least one word in a ist of words then use matchingAny.

    For example, given these documents (and assuming the title attribute is text-indexed):

    { "id": 1, "title": "The days of the week"}
    { "id": 2, "title": "Once a week"}
    { "id": 3, "title": "Once a month"}
    
    • matchingAny("Once") will match the documents with id=2 and id=3
    • matchingAny("month", "foo' , "bar") will match the document with id=3
    • matchingPhrase("The days of the week") will match the document with id=1

    More details in the docs.