简体   繁体   中英

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.

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)

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM