2

I have a mysql query like this:

SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
FROM wp_posts 
WHERE 1=1 
AND wp_posts.ID IN (2991,2993,2989,2983,2987)
AND wp_posts.post_type = 'product'
AND ((wp_posts.post_status = 'publish')) 
ORDER BY FIELD( wp_posts.ID, 2991,2993,2989,2983,2987 )
LIMIT 0, 10

And I want to note the specific line:
ORDER BY FIELD( wp_posts.ID, 2991,2993,2989,2983,2987 )

In elasticsearchwe can only have sort order by asc or desc: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html#_sort_order

So anyone can point me out on how to achieve the order by field (id, x,y,z) in elasticsearch query?

1 Answer 1

2

It's not as easy as SQL but you can use function_score and multiple matches for you'r values.
something like this:

{
   "query": {
      "function_score": {
         "query": {
            "bool": {
               "must": [
                  {
                     "terms": {
                        " ID": [
                           2991,
                           2993,
                           2989,
                           2983,
                           2987
                        ]
                     }
                  },
                  {
                     "term": {
                        "post_type": {
                           "value": "product"
                        }
                     }
                  },
                  {
                     "term": {
                        "post_status": {
                           "value": "publish"
                        }
                     }
                  }
               ]
            }
         },
         "boost": "5",
         "functions": [
            {
               "filter": {
                  "match": {
                     "ID": 2991
                  }
               },
               "weight": 100
            },
            {
               "filter": {
                  "match": {
                     "test": 2993
                  }
               },
               "weight": 99
            },
            {
               "filter": {
                  "match": {
                     "test": 2989
                  }
               },
               "weight": 98
            },
            {
               "filter": {
                  "match": {
                     "test": 2983
                  }
               },
               "weight": 97
            },
            {
               "filter": {
                  "match": {
                     "test": 2987
                  }
               },
               "weight": 96
            }
         ],
         "score_mode": "max",
         "boost_mode": "multiply"
      }
   }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, it's working. But it does make the query to ES complicated. Anyway thank you so much for helping me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.