【问题标题】:elasticsearch rails sort errorelasticsearch rails排序错误
【发布时间】:2014-06-07 15:58:05
【问题描述】:

我在使用 elasticsearch 排序时遇到错误。

参考:
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

我正在使用带有 rails 的 elasticsearch-model。以下 sn-p 未排序并给我错误。

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

从终端尝试时出现以下错误。

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

回复是:

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

【问题讨论】:

    标签: ruby-on-rails elasticsearch


    【解决方案1】:

    最直接没有深入研究,看起来这不是有效的 json(在此处检查:http://jsonlint.com/

    您发布的内容看起来像

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

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

    有效

    另一个问题是排序不是查询的子查询。查看他们的使用示例,您可以看到它没有嵌套(Source)

    另外 score 是默认排序,所以这个参数并不是真正需要的

    在 Elasticsearch 中,相关性分数由搜索结果中返回的浮点数表示为 _score,因此默认排序顺序为:_score 降序。

    Source

    Marvel(在开发期间免费)之类的东西将有助于防止此类错误,因为它会检查传入的 json

    (红框表示错误)

    【讨论】:

      猜你喜欢
      • 2015-03-25
      • 1970-01-01
      • 2019-03-08
      • 2014-10-24
      • 2012-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多