4

How to find partial search?

Now Im trying to find

db.content.find({$text: {$search: "Customer london"}})

It finds all records matching customer, and all records matching london.

If I am searching for a part of a word for example lond or custom

db.content.find({$text: {$search: "lond"}})

It returns an empty result. How can I modify the query to get the same result like when I am searching for london?

Simulant
  • 19,190
  • 8
  • 63
  • 98
lolka
  • 121
  • 1
  • 3
  • 10
  • Possible duplicate of [mongoDB prefix wildcard: fulltext-search ($text) find part with search-string](http://stackoverflow.com/questions/24343156/mongodb-prefix-wildcard-fulltext-search-text-find-part-with-search-string) – Simulant May 01 '17 at 11:04
  • Could you take a look at this answer: https://stackoverflow.com/a/69767176/12011575 – JayCodist Oct 29 '21 at 10:21

3 Answers3

2

You can use regex to get around with it (https://docs.mongodb.com/manual/reference/operator/query/regex/). However, it will work for following :

if you have word Cooking, following queries may give you result

  • cooking(exact matching)
  • coo(part of the word)
  • cooked(The word containing the english root of the document word, where cook is the root word from which cooking or cooked are derived)

If you would like to go one step further and get a result document containing cooking when you type vooking (missplled V instead of C), go for elasticsearch.

Elasticsearch is easy to setup, has extremely powerful edge-ngram analyzer which converts each words into smaller weightage words. Hence when you misspell, you will still get a document based on score elasticsearch gives to that document.

You can read about it here : https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-edgengram-tokenizer.html

Mihir Bhende
  • 8,677
  • 1
  • 30
  • 37
  • and what about if I search two words. I want to find all records which have each words or one of this words, for example Customer london – lolka May 01 '17 at 11:32
0

it will always return the empty array for partial words like when you are searching for lond to get this type of text london..

Because it take full words and search same as that they are ..

Not achive same results like :-

LO LON LOND LONDO LONDON

Here you may get help from ELASTIC-SEARCH . It is quite good for full text search when implement with mongoDB.

Refrence : ElasticSearch

Thanks

hardy
  • 880
  • 2
  • 13
  • 30
0

The find all is to an Array clientDB.collection('details').find({}).toArray().then((docs) =>

I now used the str.StartWith in a for loop to pick out my record.

        if (docs[i].name.startsWith('U', 0)) {
          return console.log(docs[i].name);

        } else {
            console.log('Record not found!!!')
        };

This may not be efficient, but it works for now

Vineeth Sai
  • 3,389
  • 7
  • 23
  • 34
ufookoro
  • 337
  • 1
  • 4
  • 12