# Rank documents by specific field with the search query result

**URL:** <https://discuss.elastic.co/t/rank-documents-by-specific-field-with-the-search-query-result/66438>\
**Category:** Elasticsearch\
**Created:** [November 17, 2016, 5:53pm UTC](https://discuss.elastic.co/t/rank-documents-by-specific-field-with-the-search-query-result/66438 "2016-11-17T17:53:26Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![kavuri](https://avatars.discourse-cdn.com/v4/letter/k/e9c0ed/32.png) [@kavuri](https://discuss.elastic.co/u/kavuri)\
**Post date:** [November 17, 2016, 5:53pm UTC](https://discuss.elastic.co/t/rank-documents-by-specific-field-with-the-search-query-result/66438/1 "2016-11-17T17:53:26Z")

</div>

I have documents that are related to website and ranking of the websites. Each of the document has atleast the following three fields

- Site name (like google, yahoo etc.)
- Site URL (like [google.com](http://google.com), [yahoo.com](http://yahoo.com), etc.)
- Site rank (which is a pre-determined numeric rank)

I have a search field that the user can search on the "site name". My intention is to allow the search and then order the results based on the site rank. I tried to sort the documents based on the site rank, but then the result has all irrelevant documents, i.e., the query result is not being used for sort

I have tried function\_score, but my results are messed up, where-in search for "char" gives results that start with "chas".

Is there a way to achieve this what I am trying?

Here is my query:  
\> {

> ```
> query: {
> bool: {
> should: [
> {
> match: {
> PRIMARY_LOCALE_ID: {
> query: "en_US",
> boost: 2
> }
> }
> },
> {
> match_phrase: {
> VALUE: "cha"
> }
> }
> ]
> }
> }
> }
> 
> ```

Thanks,  
Sateesh

---

<div class="post-metadata">

**Author:** ![cbuescher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cbuescher/32/60402_2.png) [@cbuescher](https://discuss.elastic.co/u/cbuescher)\
**Post date:** [November 21, 2016, 11:16am UTC](https://discuss.elastic.co/t/rank-documents-by-specific-field-with-the-search-query-result/66438/2 "2016-11-21T11:16:42Z")

</div>

Hi,

sorting the search results on a numeric field after filtering the documents by some search should work. So I'd check first that everything is okay with your search. What is the mapping of the field you allow your users to search on?  
Your example suggests you are using two different filter criteria (some locale and some other value, its unclear to me chat it represents). If you combine those in a "should" clause, documents satisfying either of those two this get returned, so maybe this is something to look into first.

---

<div class="post-metadata">

**Author:** ![kavuri](https://avatars.discourse-cdn.com/v4/letter/k/e9c0ed/32.png) [@kavuri](https://discuss.elastic.co/u/kavuri)\
**Post date:** [November 21, 2016, 11:43am UTC](https://discuss.elastic.co/t/rank-documents-by-specific-field-with-the-search-query-result/66438/3 "2016-11-21T11:43:56Z")

</div>

Sorting by rank after search does not give me correct results. Here is my mapping

> {  
> "template": "mysites\*",  
> "settings": {  
> "index": {  
> "analysis": {  
> "filter": {  
> "autocomplete\_filter": {  
> "type": "edge\_ngram",  
> "min\_gram": "1",  
> "max\_gram": "20"  
> }  
> },  
> "analyzer": {  
> "autocomplete": {  
> "filter": [  
> "lowercase",  
> "autocomplete\_filter"  
> ],  
> "type": "custom",  
> "tokenizer": "standard"  
> }  
> }  
> },  
> "number\_of\_shards": "1",  
> "number\_of\_replicas": "1"  
> }  
> },  
> "mappings":{  
> "sites": {  
> "properties": {  
> "VALUE": {  
> "type": "string",  
> "analyzer": "autocomplete"  
> }  
> }  
> }  
> }  
> }

The locale is to boost results specific to region. The reason for should is if the search exhausts the results of that region, then the search should return results in other regions (this works). I can even remove the locale in this case and try only sorting the results after search, but sort just sorts everything with the highest rank and not based on my search results

---

<div class="post-metadata">

**Author:** ![cbuescher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cbuescher/32/60402_2.png) [@cbuescher](https://discuss.elastic.co/u/cbuescher)\
**Post date:** [November 21, 2016, 11:56am UTC](https://discuss.elastic.co/t/rank-documents-by-specific-field-with-the-search-query-result/66438/4 "2016-11-21T11:56:36Z")

</div>

Why are you using the `edge_ngram` filter with those settings? This explains

> [@kavuri](#):
>
> in search for "char" gives results that start with "chas"

because you are indexing each input token with lots if edge ngrams. At search time the analyzer is also applied to your query, so when searching for "char"m this will also be broken donw as "c", "ch", "cha" and "char", so you will get tons of matches. You should define another `search_analyzer` that doesn't do the edge ngram analysis.

---

<div class="post-metadata">

**Author:** ![kavuri](https://avatars.discourse-cdn.com/v4/letter/k/e9c0ed/32.png) [@kavuri](https://discuss.elastic.co/u/kavuri)\
**Post date:** [November 22, 2016, 3:45am UTC](https://discuss.elastic.co/t/rank-documents-by-specific-field-with-the-search-query-result/66438/5 "2016-11-22T03:45:10Z")

</div>

> [@cbuescher](#):
>
> You should define another search\_analyzer that doesn't do the edge ngram analysis.

I wanted to have edge-ngram so that I can enable 'search as user types' and not using it would not help my usecase. Can you please confirm  
Can you suggest any other mechanism on how I can enable 'search as user types' but not use edge-ngram

Thanks,  
Sateesh

---

<div class="post-metadata">

**Author:** ![cbuescher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cbuescher/32/60402_2.png) [@cbuescher](https://discuss.elastic.co/u/cbuescher)\
**Post date:** [November 22, 2016, 8:45am UTC](https://discuss.elastic.co/t/rank-documents-by-specific-field-with-the-search-query-result/66438/6 "2016-11-22T08:45:19Z")

</div>

As I already mentioned:

> [@cbuescher](#):
>
> You should define another search\_analyzer that doesn't do the edge ngram analysis.

---

<div class="post-metadata">

**Author:** ![cbuescher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cbuescher/32/60402_2.png) [@cbuescher](https://discuss.elastic.co/u/cbuescher)\
**Post date:** [November 22, 2016, 8:47am UTC](https://discuss.elastic.co/t/rank-documents-by-specific-field-with-the-search-query-result/66438/7 "2016-11-22T08:47:23Z")

</div>

[This article](https://www.elastic.co/guide/en/elasticsearch/guide/current/_index_time_search_as_you_type.html) explains the details.

---

<div class="post-metadata">

**Author:** ![kavuri](https://avatars.discourse-cdn.com/v4/letter/k/e9c0ed/32.png) [@kavuri](https://discuss.elastic.co/u/kavuri)\
**Post date:** [November 29, 2016, 9:53am UTC](https://discuss.elastic.co/t/rank-documents-by-specific-field-with-the-search-query-result/66438/8 "2016-11-29T09:53:57Z")

</div>

Setting "search\_analyzer": "standard" helped. This works.

Thanks for your help Christoph

---

<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:** [December 27, 2016, 9:54am UTC](https://discuss.elastic.co/t/rank-documents-by-specific-field-with-the-search-query-result/66438/9 "2016-12-27T09:54:04Z")

</div>

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