# Conditional sorting of hits

**URL:** <https://discuss.elastic.co/t/conditional-sorting-of-hits/231310>\
**Category:** Elasticsearch\
**Created:** [May 6, 2020, 9:51am UTC](https://discuss.elastic.co/t/conditional-sorting-of-hits/231310 "2020-05-06T09:51:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mhoegh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mhoegh/32/67784_2.png) [@mhoegh](https://discuss.elastic.co/u/mhoegh)\
**Post date:** [May 6, 2020, 9:51am UTC](https://discuss.elastic.co/t/conditional-sorting-of-hits/231310/1 "2020-05-06T09:51:02Z")

</div>

I've a index with road names. My settings look like this:

```json
{
  "settings": {
    "max_ngram_diff": 20,
    "analysis": {
      "analyzer": {
        "str_search_analyzer": {
          "type": "custom",
          "tokenizer": "whitespace",
          "filter": [
            "lowercase"
          ]
        },
        "str_index_analyzer": {
          "type": "custom",
          "tokenizer": "whitespace",
          "filter": [
            "lowercase",
            "substring"
          ]
        }
      },
      "filter": {
        "substring": {
          "type": "edgeNGram",
          "min_gram": 1,
          "max_gram": 255
        }
      }
    }
  }
}

```

In the index I've strings like this:

1. Foo Road
2. Foo Bar Road
3. Bar Road

So when I search for 'Bar' I get #2 and #3 as hits. This is expected.

But I would like to control the order of the hits. In this case I would like to have #3 as first hit, because the string starts with the search term.

Is it possible to sort hits as I want?

---

<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:** [June 3, 2020, 9:51am UTC](https://discuss.elastic.co/t/conditional-sorting-of-hits/231310/2 "2020-06-03T09:51:06Z")

</div>

Closed on June 3, 2020, 9:51am UTC

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