2

My objective is to create a search pipeline in MongoDB, that returns result for multiple fields.

My index:

{
  "analyzer": "lucene.standard",
  "searchAnalyzer": "lucene.standard",
  "mappings": {
    "dynamic": false,
    "fields": {
      "description": {
        "analyzer": "lucene.standard",
        "type": "string"
      },
      "name": {
        "type": "string"
      }
    }
  }
}

My $search pipeline stage:

{
    $search: {
        index: 'lucene.standard',
        queryString: {
            defaultPath: 'name',
            query: `name:"${query}" OR description:"${query}"`,

        },
    }
}

This works, but only when I search for a full word. For example, if a have a document with description fooBar. My problem is that if I query for foo - I get no results. Only works when I query for the full word fooBar.

How can I fix this - maybe be using another index definition?

DauleDK
  • 3,313
  • 11
  • 55
  • 98

1 Answers1

4

Did the previous solution here work for you? As defined in the documentation:

The autocomplete operator performs a search for a word or phrase that contains a sequence of characters from an incomplete input string.

{
  "analyzer": "lucene.standard",
  "searchAnalyzer": "lucene.standard",
  "mappings": {
    "dynamic": false,
    "fields": {
        "description": {
            "type": "autocomplete"
          },        
        "name": {
          "type": "autocomplete"
        }        
    }
  }
}

Your search stage is also a bit off. Maybe try a compound operator:

{
  $search: {
    "compound": {
      "should": [{
          "autocomplete": {
            "query": "foo",
            "path": "name"
          },
          "autocomplete": {
            "query": "foo",
            "path": "description"
          }
        }]
    }
  }
}
Nice-Guy
  • 1,457
  • 11
  • 20