Search Over mutiple Types in an index and Sort the result

I have an index 'elasticsearch' and have 4 Types 'user','admin','employer','students'..When i typing the name from front end,it should search the name in all 4 types and return.Search priority will user,employer,student,admin. If the name is exists in user first result should be from user type,and if its exists in employer too ,then it will come after the data from user type.next will be student ,then admin(this will be sorting order of result).Can anybody help me on this?

Here is something you could start from:

DELETE test
PUT test/doc/1
{
  "user": "user",
  "name": "foo"
}
PUT test/doc/2
{
  "user": "admin",
  "name": "foo"
}
PUT test/doc/3
{
  "user": "employer",
  "name": "foo"
}
PUT test/doc/4
{
  "user": "students",
  "name": "foo"
}
GET test/_search
{
  "query": {
    "function_score": {
      "query": {
        "match": {
          "name": "foo"
        }
      },
      "boost": "5",
      "functions": [
        { "filter": { "match": { "user": "user" } }, "weight": 4 },
        { "filter": { "match": { "user": "employer" } }, "weight": 3 },
        { "filter": { "match": { "user": "students" } }, "weight": 2 },
        { "filter": { "match": { "user": "admin" } }, "weight": 1 }
      ],
      "boost_mode": "multiply"
    }
  }
}
1 Like

Thanks dadoonet...

If am using my ranking field is an array of uuids,["0e9aca40-2b55-11e8-a3d3-b5b01b7e610f",
"0e9aca41-2b55-11e8-a3d3-b5b01b7e610f",
"0e9aca42-2b55-11e8-a3d3-b5b01b7e610f",
"0e9aca43-2b55-11e8-a3d3-b5b01b7e610f],
Then how can i give ranking?below method not giving expected result.

{ "filter": { "match": { "users": "0e9aca42-2b55-11e8-a3d3-b5b01b7e610f" } }, "weight": 4 }.

All these UUIDs are different each other in 1 character only(0e9aca40,0e9aca41,0e9aca42,0e9aca43 starting first 8 bytes of each uuid)

It might be a problem with analyzers.
Check your mapping and make sure you are using in that case may be a keyword data type or a text datatype but with may be a simple analyzer or a keyword analyzer.

Thanks!. If i want to give filter weight based on 'or' how can achieve same?Suppose "user": "employer" or "user": "students" match in filter query,i need to give weight 2 for that(if any of the criteria match,i shud be able to give weight 2)..How can i do that in filter weight ?

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