# Сортировка с запросом "indices\_boost"

**URL:** <https://discuss.elastic.co/t/indices-boost/181835>\
**Category:** Вопросы на русском языке\
**Created:** [May 20, 2019, 1:43pm UTC](https://discuss.elastic.co/t/indices-boost/181835 "2019-05-20T13:43:55Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![surkis](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/surkis/32/46477_2.png) [@surkis](https://discuss.elastic.co/u/surkis)\
**Post date:** [May 20, 2019, 1:43pm UTC](https://discuss.elastic.co/t/indices-boost/181835/1 "2019-05-20T13:43:55Z")

</div>

В запросе к двом индексам я использую "indices\_boost" с соответсвующими коефициентами. Мне нужно еще дополнительно отсортировать по определенному полю, да так чтоб сначала ишли результаты с одного индекса, а затем с другого

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [May 20, 2019, 5:37pm UTC](https://discuss.elastic.co/t/indices-boost/181835/2 "2019-05-20T17:37:42Z")

</div>

Какая версия elasticsearch?

---

<div class="post-metadata">

**Author:** ![surkis](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/surkis/32/46477_2.png) [@surkis](https://discuss.elastic.co/u/surkis)\
**Post date:** [May 21, 2019, 1:51pm UTC](https://discuss.elastic.co/t/indices-boost/181835/3 "2019-05-21T13:51:32Z")

</div>

=6.2

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [May 21, 2019, 8:26pm UTC](https://discuss.elastic.co/t/indices-boost/181835/4 "2019-05-21T20:26:57Z")

</div>

Если только 2 индекса, то проще всего просто отсортировать по полю `_index`.

```auto
PUT test_a/_doc/1
{
  "test": "foo"
}

PUT test_a/_doc/2
{
  "test": "bar"
}

PUT test_a/_doc/3
{
  "test": "baz"
}

PUT test_b/_doc/1
{
  "test": "foo"
}

PUT test_b/_doc/2
{
  "test": "baz"
}

POST test_*/_doc/_search
{
  "query": {
    "match_all": {}
  },
  "sort": {
    "_index": {
      "order": "asc"
    },
    "test.keyword": {
      "order": "asc"
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![surkis](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/surkis/32/46477_2.png) [@surkis](https://discuss.elastic.co/u/surkis)\
**Post date:** [May 23, 2019, 7:19am UTC](https://discuss.elastic.co/t/indices-boost/181835/5 "2019-05-23T07:19:00Z")

</div>

клас, спасибо!

---

<div class="post-metadata">

**Author:** ![surkis](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/surkis/32/46477_2.png) [@surkis](https://discuss.elastic.co/u/surkis)\
**Post date:** [May 23, 2019, 11:50am UTC](https://discuss.elastic.co/t/indices-boost/181835/6 "2019-05-23T11:50:15Z")

</div>

а если больше двух?

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [May 23, 2019, 2:16pm UTC](https://discuss.elastic.co/t/indices-boost/181835/7 "2019-05-23T14:16:00Z")

</div>

Тогда надо будет либо индексы именовать, чтобы они сортировались правильно, либо `_score` присваивать в зависимости от названия в function\_score:

```auto
POST test_*/_doc/_search
{
  "query": {
    "function_score": {
      "query": {
        "match_all": {}
      },
      "functions": [
        {
          "filter": {"term": {"_index": "test_a"}},
          "weight": 1
        },
        {
          "filter": {"term": {"_index": "test_b"}},
          "weight": 2
        },
        {
          "filter": {"term": {"_index": "test_c"}},
          "weight": 3
        }
      ],
      "score_mode": "first",
      "boost_mode": "replace"
    }
  },
  "sort": {
    "_score": {
      "order": "asc"
    },
    "test.keyword": {
      "order": "asc"
    }
  }
}

```

---

<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 20, 2019, 2:16pm UTC](https://discuss.elastic.co/t/indices-boost/181835/8 "2019-06-20T14:16:19Z")

</div>

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