J. Doe
J. Doe

Reputation: 21

Elasticsearch sort option not supported

I'm using elastic search in Rails. I am trying to sort a list of customers by their total dollars spent descending. This is my ruby code:

query = { 
    bool: {
      filter: { 
        term: { store_id: store.id } # Limits customers by current store
      }   
    }   
  }   

  sort = { 
    sort: { "total_spent": { order: "desc" }}
  }   

  response = Contact.search(query: query, sort: sort)

This returns with an error of sort option [total_spent] not supported I've tried with other fields to make sure it wasn't just something wrong with the total_spent field. Thanks.

Upvotes: 0

Views: 396

Answers (2)

sudhakar
sudhakar

Reputation: 48

We can sort specific to the field, refer https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html.

so we can use like,

query = { 
    bool: {
      filter: { 
        term: { store_id: store.id } # Limits customers by current store
      }   
    },
    sort: { total_spent: { order: :desc }}   
  }   

response = Contact.search(query)

Upvotes: 0

oreoluwa
oreoluwa

Reputation: 5633

I'm not really sure, but I think this may be related to incorrect usage of the ES::DSL.

What happens when you try this:

query = { 
    bool: {
      filter: { 
        term: { store_id: store.id } # Limits customers by current store
      }   
    }   
  }   

  sort = { 
    sort: [{ "total_spent": { order: "desc" }}] #https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html
  }   

  response = Contact.search(query, sort)

Upvotes: 1

Related Questions