# Implementing "Sort" while searching

**URL:** <https://discuss.elastic.co/t/implementing-sort-while-searching/135960>\
**Category:** Elasticsearch\
**Created:** [June 14, 2018, 4:02pm UTC](https://discuss.elastic.co/t/implementing-sort-while-searching/135960 "2018-06-14T16:02:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![saurabhbhatt](https://avatars.discourse-cdn.com/v4/letter/s/97f17d/32.png) [@saurabhbhatt](https://discuss.elastic.co/u/saurabhbhatt)\
**Post date:** [June 14, 2018, 4:02pm UTC](https://discuss.elastic.co/t/implementing-sort-while-searching/135960/1 "2018-06-14T16:02:22Z")

</div>

Hi Everyone,

Basically my requirement is little complex so I have to give example to explain better. Below is my indexed document (sort of, not completely)

"productid": 181750,  
"productname": "Synthetic Promotional Tennis Ball",  
"productname\_suggest": "Synthetic Promotional Tennis Ball",  
"saleprice": 1.15,  
"filename": "Promotional-Synthetic-Promotional-Tennis-Ball-181750\_red.jpg",  
"minquantity": 150,  
"imprinttype": "Silk Screen",  
"imprinttypeids": "16",  
"madeinUSA": false,  
"themetype": "Tennis",  
"themetype\_id": "404",  
"colorArray": [  
"Blue",  
"Green",  
"Orange",  
"Pink",  
"Purple",  
"Red",  
"White",  
"Yellow"  
],  
"topsellersOrders": 1,  
"topsellersQuantity": 300,  
"region": "SE",  
"regionnmlong": "Southeast",  
"createDate": "2017-04-27T12:58:42.387",  
"freeShipping": false,  
"issale": true,  
"tN\_Width": 160,  
"tN\_Height": 160,  
"subcategory\_id\_list": "161",  
"subcategory\_nm\_list": "Toys",  
"subcategory\_id\_main": 161,  
"subcategory\_nm\_main": "Toys",  
"subcategory\_nm\_main\_suggest": "Toys",  
"category\_id\_list": "36",  
"category\_nm\_list": "Games & Toys",  
"category\_id\_main": 36,  
"category\_nm\_main": "Games & Toys",  
"category\_nm\_main\_suggest": "Games & Toys",  
"subcategoryURL": "/Promotional-Toys-Custom-36-161.html",  
"categoryURL": "/Promotional-GamesandToys-Custom-36.html",  
"themetype\_url": "/Promotional-Tennis-Products-404.html",  
"productFeaturedOrder": 102

**and the search query I use is as below:**

GET /allproducts\_beta/product/\_search  
{  
"from": 0,  
"size": 10000,  
"query": {  
"multi\_match" : {  
"query": "pink color frisbee",  
"type": "cross\_fields",  
"fields": ["productname", "brandname", "imprinttype", "themetype", "offerthemetype", "industrythemetype", "schoolthemetype", "eventthemetype", "awarenessthemetype", "tradeshowthemetype", "colorArray", "materialArray", "subcategory\_nm\_list", "subcategory\_nm\_main", "category\_nm\_list", "category\_nm\_main", "ProductFeaturedOrder", "smartTags"],  
"operator": "or",  
"minimum\_should\_match": "70%"  
}  
}  
}

Basically this query returns document hits (and please correct me if my understanding of this queries working is wrong) in order as below:  
**STEP 1:** On top are document hits that contain all 3 search words "pink" , "color" and "frisbee" from the search phrase individually in any of the fields.  
**STEP 2:** Next are the document hits that contain any 2 word from the search phrase "pink color frisbee" and these would be appended to result as well by ElasticSearch.  
**STEP 3:** Lastly come document hits that may contain ANY one of the word from the search phrase "pink color frisbee" and these documents would also be appended.  
**STEP 4:** Obviously these would be by DEFAULT sorted based on "\_score" and returned by ElasticSearch API.

Looking at these results, we understand that the final results sent by ElasticSearch is actually created of 3 parts. The part on top is containing all 3 search words followed by part that contains 2 search words and finally only 1 search word which is at bottom of results since their \_score is least. Now my requirement here is, very specific, I want that at each STEP 1, 2 & 3 I want that my records should be "sort" 'ed based another column "productFeaturedOrder" inside each part while keeping the order of part 1, 2 and 3 intact before result is sent back.

---

<div class="post-metadata">

**Author:** ![klof](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/klof/32/31590_2.png) [@klof](https://discuss.elastic.co/u/klof)\
**Post date:** [June 15, 2018, 4:05pm UTC](https://discuss.elastic.co/t/implementing-sort-while-searching/135960/2 "2018-06-15T16:05:27Z")

</div>

Have you tried the field value factor, on the field `ProductFeaturedOrder`?  
I don't know if this will solve your problem but this might help.

[https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html#function-field-value-factor](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html#function-field-value-factor)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 13, 2018, 4:05pm UTC](https://discuss.elastic.co/t/implementing-sort-while-searching/135960/3 "2018-07-13T16:05:32Z")

</div>

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