ruby-on-rails - elasticsearch rails 排序错误

标签 ruby-on-rails elasticsearch

我在使用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。以下代码段未排序并给我错误。

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

最佳答案

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

您发布的内容看起来像

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

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

将是有效的

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

此外,score是默认排序方式,因此实际上不需要参数

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

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

(红色框表示错误)

关于ruby-on-rails - elasticsearch rails 排序错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24098997/

相关文章:

ruby-on-rails - 将局部变量返回给 Controller

linux - elasticsearch 中的 Gradle 问题

python - 使用pyes进行 Elasticsearch

elasticsearch - 安全地连接到来自Packetbeat的开放发行版Elastic

ruby-on-rails - 如何从 Skyscanner API 发布请求中检索 session key - Ruby

ruby-on-rails - 为什么我的 View 被标记为 XSS 漏洞?

ruby-on-rails - 将渲染的 View 作为电子邮件发送

ruby-on-rails - Rails - AdminMailer - 设置发件人姓名

c# - 如何使用 Serilog 在 Json 中获取日志级别 - .NET Core 3.1

elasticsearch - 弹性分析器以启用匹配搜索,例如C#,C++,A +