Query for sub-set of word in MongoDB Atlas full text search
Asked Answered
B

1

2

My objective is to create an index + search pipeline, so I can find the following document by searching for "reprod":

{ name: "can you find this and reproduce?" }

What I have:

I'm using the default index. My search pipeline looks like this:

$search: {
    text: {
        query: 'reprod',
        path: 'name',
    },
},

But this does not work - I only get the full result when I provide the entire word in the query. I would like the document to be returned, even if I only provide a subset of a word.

Bihari answered 9/7, 2020 at 20:18 Comment(0)
L
5

If you can change your index definition, I'd suggest using the autocomplete operator but fields on which you query using this operator currently require them to be statically defined in the index definition. For this example, your index definition could look something like this:

{
  "mappings": {
    "dynamic": false,
    "fields": {
        "name": {
          "type": "autocomplete"
        }
    }
  }
}

And then your query would look like this:

{
  $search: {
    "autocomplete": {
      "path": "name",
      "query": "reprod"
    }
  }
}

If you don't want a statically defined index definition, you could use the Wildcard Operator instead with the Allow Analyzed Field set to true.

Laniary answered 15/7, 2020 at 1:5 Comment(1)
I now have a problem with searching 2 fields with autocomplete, but decided to create a separate question here: #63186783. I hope you might have the answer for this once as well ☺️Bihari

© 2022 - 2024 — McMap. All rights reserved.