Is it possible to return only one result if an exact match is found?

Hi guys,

I'm building a site with a database of watches. I have an autocomplete search input. I have fuzzy matching for typing in the watch name (post_title), but I'd like for the user to be able to enter the watch reference if they have it, this would show just one result if there was an exact match. Is it possible to return just one result if a certain match is found matching the meta.reference.raw field, and if no match is found for that field fall back to the fuzzy search for the post_title field?

Example of the data below:

{
  "_index": "twptestwp-post-1",
  "_type": "_doc",
  "_id": "32567",
  "_source": {
    "post_id": 32567,
    "ID": 32567,
    "post_title": "ROYAL OAK OFFSHORE DIVER",
    "meta": {
      "reference": [
        {
          "value": "15710ST.OO.A010CA.01",
          "raw": "15710ST.OO.A010CA.01",
          "boolean": false,
          "date": "1971-01-01",
          "datetime": "1971-01-01 00:00:01",
          "time": "00:00:01"
        }
      ]
    }
  }
}

Many thanks

hey,

if you really only want a single result, then you need to execute an exact search first.

if you want to score an exact match higher (but still find the other documents), you may want to take a look at the should query as part of the bool query.

--Alex

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.