Sunday, 30 April 2017

MongoDB text search + return all occurrences of string

What I'm trying to do is check if a word exists inside a document field and retrieve its context on the phrase. To be more clear, imagine I have a document like this:

{
  title: "Shakespeare",
  bio: "Shakespeare was born and brought up in Stratford-upon-Avon,
        Warwickshire. At the age of 18, he married Anne Hathaway, with
        whom he had three children: Susanna, and twins Hamnet and 
        Judith. Sometime between 1585 and 1592, he began a successful 
        career in London as an actor, writer, and part-owner of a 
        playing company called the Lord Chamberlain's Men, later known 
       as the King's Men. He appears to have retired to Stratford 
       around 1613, at age 49, where he died three years later. Few 
       records of Shakespeare's private life survive, which has 
      stimulated considerable speculation about such matters as his 
      physical appearance, sexuality, and religious beliefs and whether 
      the works attributed to him were written by others."
}

I have a text index created for the bio field and if I search for "Shakespeare" it returns this document. But what I would like to get when I search for "Shakespeare" is the exert of this word on the bio. So, I would get something like "Shakespeare was born and brought" and "Few records of Shakespeare's private". Is this possible in anyway? I'm using node.js for the server side, if it's helpful.

Hope someone can help me, thanks in advance!



via Soraia55

No comments:

Post a Comment