Home > Enterprise >  ElasticSearch order based on type of hit
ElasticSearch order based on type of hit

Time:07-01

I started using ElasticSearch in my ReactJS project but I'm having some troubles with it.

When I search, I'd like to have my results ordered based on this table

Full-Hit Start-hit Sub-hit Fuzzy-hit
category 1 3 5 10
trade name 2 4 6 11
official name 7 8 9 12

The definition (the way I see it, unless I'm wrong) are like this:

Full-hit

examples:

  • Term "John" has a full-hit on "John doe"
  • Term "John" doesn't have a full-hit on "JohnDoe"

Start-hit

examples:

  • Term "John" has a start-hit on "Johndoe"
  • Term "Doe" doesn't have a start-hit on "JohnDoe"

sub-hit

examples:

  • Term "baker" has a sub-hit on "breadbakeries"
  • Term "baker" doesn't have a sub-hit on "de backer"

fuzzy-hit

From my understanding fuzzy-hit is when the queried word has 1 mistake or 1 letter is missing

examples:

  • Term "bakker" has a fuzzy-hit on "baker"
  • Term "bakker" doesn't have a fuzzy-hit on "bakers"

I found out that we can boost fields like this

fields = [
    `category^3`,
    `name^2`,
    `official name^1`,
  ];

But that is not based on the full-, start-, sub-, or fuzzy-hit

Is this doable in ReactJS with Elasticsearch?

CodePudding user response:

I need to understand your problem. In a nutshell

1."If a full-hit is found in the category field, then we should boost it by 1".

  1. If a full-hit is found in the official_name field we should boost by 7..

and so on for all the 12 possibilities?

If this is what you want, you are going to need 12 seperate queries, all covered under one giant bool -> should clause.

I won't write out the query for you, but I will give you some pointers, on how to structure the 4 subtypes of the queries.

  1. Full Hit
{
  "term" : {"field" : "category/tradE_name/official_name", "value" : "the_search_term"}
}
  1. Start-hit
{
  "match_phrase_prefix" : {"category/trade_name/official_name" :  "the search term"}
}
  1. Sub-hit
{
  "regexp" : {
     "category/official/trade" : {"value" : "*term*"}
   }
}
  1. Fuzzy
{
  "fuzzy" : {
    "category/trade/official" : {"value" : "term"}
  }
}

You will need one giant bool

{
  "query" : {
    "bool" : {
       "should" : [
             // category field queries, 4 total clauses.      
             {
                
             }
             // official field queries, 4 clauses, to each clauses assign the boost as per your table. that's it.
       ]
    }
  }
}

To each clause, assign a boost as per your table. That;s it.

HTH.

  • Related