How to do a multi match that sorts all matches of one field before another field?

Hello everyone! I am trying to build a search query for an e commerce, I have two requests that I will explain to detail:

A) I want to sort results depending on if a search term is on one field or another.

The use case would be like this:

  1. Search in two fields "title" and "description"
  2. Sort the results that match with "title" always first, and then "description" second, no matter how many instances of the search are on the field "description", it shall always come second if the term is not present on "title".

So for example, If I search of "laptop" , I want to get documents that contain "laptop" on the field "title" always first, and the documents that contain the word "laptop" on the field "description" second.

The caviat here is that any document that DOES contain the word "laptop" on the field "description" but does NOT contain the word "laptop" on the field "title" shall NEVER be before any document that contains the word "laptop" on the field "title".

A practical example, imagine these three documents:

GET /products/doc/1
{ 
"title" : "Laptop Lenovo" ,
"description" : "A fast computer device from Lenovo." ,
}

GET /products/doc/2
{ 
"title" : "HP Intel Laptop" ,
"description" : "A HP Laptop with an Intel chip." ,
}

GET /products/doc/3
{ 
"title" : "Desk Stand",
"description" : "A Laptop desk stand perfect for laptop, fits any kind of laptop , no matter the laptop manufacturer." ,
 }

My expected result would be to get:
The document 1 first because the word Laptop is on the field "title" and is closer to the beginning.
Then the document 2 next because the word laptop is on the field "title" , but its further away from the beginning of the field.
Lastly, the document 3 because even though the document has the word "laptop" FOUR times on the field "description" it doesn't have that word on the field "title" even once.

My second request I partially mentioned just above, but what I want to do is:

B) Don't count multiple instances of the same words as a better score, but DO count how close is the words I am looking for to the beginning of the field

Something I noticed is that , If I have a document 1 with the title "Laptop Lenovo" and document 2 with the title "Desk stand for laptops, perfect fit for any laptop", document 2 will show first, because it contains the term "laptop" two times.

I want to ignore that, what I want is to get the best score, depending on where the term being searched is located. In this case, document 1 has the word laptop before any other word, so I expect it to come first, no matter how many times the word "laptop" is on any other document's title.

I really hope someone can help me because I can't make this work at all. If you feel like you can help me with either point A) or B) but not both, please do help anyways,

Thank you in advanve!

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