简体   繁体   中英

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]}"
}

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 在此处输入图片说明 (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
支票
(red box signifies an error)

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