aashish
aashish

Reputation: 2603

elasticsearch rails sort error

I am getting error with elasticsearch sort.

references:
http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-match-query.html http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-sort.html

I am using elasticsearch-model with rails. The following snippet was not sorting and giving me error.

Ad.search(query: {
    sort: [{posted_on: {order: "asc"}},
      ],
    match: {
      description: {
        query: params[:search]
      }
    }
  })

The following is error when trying from terminal.

curl -XPOST 'localhost:9200/_search' -d '{
  "query": {
    "match": {
      "description": {
        "query": "good center location"
      }
    },
    "sort": [
      "_score"
    ]
  }
}'

response is:

{
  "error": "SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {[eF0LAz1gQxOXKPlYGjj9eA][.marvel-2014.06.07][0]: SearchParseException[[.marvel-2014.06.07][0]: query[block.2.description:good block.2.description:center block.2.description:location],from[-1],size[-1]: Parse Failure [Failed to parse source [{\n   \"query\" : {\n     \"match\" : {\n       \"description\" : {\n         \"query\" : \"good center location\"\n       }\n     },\n     \"sort\" : [\"_score\"]\n   }]]]; nested: ElasticsearchParseException[Expected field name but got START_ARRAY \"sort\"]; }{[eF0LAz1gQxOXKPlYGjj9eA][.marvel-2014.05.29][0]: SearchParseException[[.marvel-2014.05.29][0]: query[block.2.description:good block.2.description:center block.2.description:location],from[-1],size[-1]: Parse Failure [Failed to parse source [{\n   \"query\" : {\n     \"match\" : {\n       \"description\" : {\n         \"query\" : \"good center location\"\n       }\n     },\n     \"sort\" : [\"_score\"]\n   }]]]; nested: ElasticsearchParseException[Expected field name but got START_ARRAY \"sort\"]; }{[eF0LAz1gQxOXKPlYGjj9eA][.marvel-2014.05.31][0]: SearchParseException[[.marvel-2014.05.31][0]: query[block.2.description:good block.2.description:center block.2.description:location],from[-1],size[-1]: Parse Failure [Failed to parse source [{\n   \"query\" : {\n     \"match\" : {\n       \"description\" : {\n         \"query\" : \"good center location\"\n       }\n     },\n     \"sort\" : [\"_score\"]\n   }]]]; nested: ElasticsearchParseException[Expected field name but got START_ARRAY \"sort\"]; }{[eF0LAz1gQxOXKPlYGjj9]}"
}

Upvotes: 0

Views: 1203

Answers (1)

Khaliq Gant
Khaliq Gant

Reputation: 130

Most immediately without looking at it deeply, it looks like this is not valid json (checked here: http://jsonlint.com/)

What you posted would look like

{
    "query": {
        "match": {
            "description": {
                "query": "good center location"
            }
        },
        "sort": [
            "_score"
        ]
     }

Whereas

{
    "query": {
        "match": {
            "description": {
                "query": "good center location"
            }
        },
        "sort": [
            "_score"
        ]
    }
}

would be valid

Another issue is that sort is not a sub query of query. Looking at their usage example you can see that it is not nested enter image description here (Source).

In addition score is the default sort so that parameter is not really necessary

In Elasticsearch the relevance score is represented by the floating point number returned in the search results as the _score, so the default sort order is: _score descending.

Source

Something like Marvel (free during development) would help prevent these kind of errors as it checks the json being passed in
checks
(red box signifies an error)

Upvotes: 2

Related Questions