gpt4 book ai didi

elasticsearch - 如何使用 elasticsearch-rails DSL 转换 Elasticsearch 查询

转载 作者:行者123 更新时间:2023-12-02 22:46:35 31 4
gpt4 key购买 nike

我得到了这个 Exception ,它似乎告诉我查询语句中有异常。

但是,我可以弄清楚我应该修改什么,有什么想法吗?

(pry) output error: #<Elasticsearch::Transport::Transport::Errors::BadRequest: [400] {"error":{"root_cause":[{"type":"query_parsing_exception","reason":"[_na] query malformed, no field after start_object","index":"flights","line":1,"col":11}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"flights","node":"mVtcYMIfRxOBeqWvMbShhA","reason":{"type":"query_parsing_exception","reason":"[_na] query malformed, no field after start_object","index":"flights","line":1,"col":11}}]},"status":400}>

这是原始的 Elasticsearch ,它有效,我得到了结果
{
"size": 0,
"query": {
"filtered": {
"query": {
"query_string": {
"analyze_wildcard": true,
"query": "total_price_twd: [10 TO 10000]"
}
},
"filter": {
"bool": {
"must": [
{
"range": {
"departure_at": {
"gte": 1452164239142,
"lte": 1457348239142,
"format": "epoch_millis"
}
}
}
],
"must_not": []
}
}
}
},
"aggs": {
"3": {
"date_histogram": {
"field": "departure_at",
"interval": "1d",
"time_zone": "Asia/Shanghai",
"min_doc_count": 1,
"extended_bounds": {
"min": 1452164239141,
"max": 1457348239141
}
}
}
}
}

这就是我正在使用 elasticsearch-rails 设计查询 DSL
Q_PARAMS = {
"size":0,
"query":{
"filtered":{
"query":{
"query_string":{
"analyze_wildcard":true,
"query":"total_price_twd: [10 TO 10000]"
}
},
"filter":{
"bool":{
"must":[
{
"range":{
"departure_at":{
"gte":1452164239142,
"lte":1457348239142,
"format":"epoch_millis"
}
}
}
],
"must_not":[
]
}
}
}
},
"aggs":{
"3":{
"date_histogram":{
"field":"departure_at",
"interval":"1d",
"time_zone":"Asia/Shanghai",
"min_doc_count":1,
"extended_bounds":{
"min":1452164239141,
"max":1457348239141
}
}
}
}
}

def self.es_search
__elasticsearch__.search(
{
query: Q_PARAMS,
size: 200
}
)
end

Elasticsearch 搜索的结果
{
"took": 23,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 12991,
"max_score": 0,
"hits": []
},
"aggregations": {
"3": {
"buckets": [
{
"key_as_string": "2016-01-07T00:00:00.000+08:00",
"key": 1452096000000,
"doc_count": 46
},
...
]
}
}
}

来自 Elasticsearch 服务器的日志
elasticsearch_1 | [2016-03-07 11:05:04,273][INFO ][rest.suppressed          ] //flights/flight/_search Params: {index=flights, type=flight}
elasticsearch_1 | Failed to execute phase [query], all shards failed; shardFailures {[mVtcYMIfRxOBeqWvMbShhA][flights][0]: RemoteTransportException[[Payback][172.17.0.3:9300][indices:data/read/search[phase/query]]]; nested: SearchParseException[failed to parse search source [{"query":{"size":0,"query":{"filtered":{"query":{"query_string":{"analyze_wildcard":true,"query":"total_price_twd: [10 TO 10000]"}},"filter":{"bool":{"must":[{"range":{"departure_at":{"gte":1452164239142,"lte":1457348239142,"format":"epoch_millis"}}}],"must_not":[]}}}},"aggs":{"3":{"date_histogram":{"field":"departure_at","interval":"1d","time_zone":"Asia/Shanghai","min_doc_count":1,"extended_bounds":{"min":1452164239141,"max":1457348239141}}}}},"size":200}]]; nested: QueryParsingException[[_na] query malformed, no field after start_object]; }{[mVtcYMIfRxOBeqWvMbShhA][flights][1]: RemoteTransportException[[Payback][172.17.0.3:9300][indices:data/read/search[phase/query]]]; nested: SearchParseException[failed to parse search source [{"query":{"size":0,"query":{"filtered":{"query":{"query_string":{"analyze_wildcard":true,"query":"total_price_twd: [10 TO 10000]"}},"filter":{"bool":{"must":[{"range":{"departure_at":{"gte":1452164239142,"lte":1457348239142,"format":"epoch_millis"}}}],"must_not":[]}}}},"aggs":{"3":{"date_histogram":{"field":"departure_at","interval":"1d","time_zone":"Asia/Shanghai","min_doc_count":1,"extended_bounds":{"min":1452164239141,"max":1457348239141}}}}},"size":200}]]; nested: QueryParsingException[[_na] query malformed, no field after start_object]; }{[mVtcYMIfRxOBeqWvMbShhA][flights][2]: RemoteTransportException[[Payback][172.17.0.3:9300][indices:data/read/search[phase/query]]]; nested: SearchParseException[failed to parse search source [{"query":{"size":0,"query":{"filtered":{"query":{"query_string":{"analyze_wildcard":true,"query":"total_price_twd: [10 TO 10000]"}},"filter":{"bool":{"must":[{"range":{"departure_at":{"gte":1452164239142,"lte":1457348239142,"format":"epoch_millis"}}}],"must_not":[]}}}},"aggs":{"3":{"date_histogram":{"field":"departure_at","interval":"1d","time_zone":"Asia/Shanghai","min_doc_count":1,"extended_bounds":{"min":1452164239141,"max":1457348239141}}}}},"size":200}]]; nested: QueryParsingException[[_na] query malformed, no field after start_object]; }{[mVtcYMIfRxOBeqWvMbShhA][flights][3]: RemoteTransportException[[Payback][172.17.0.3:9300][indices:data/read/search[phase/query]]]; nested: SearchParseException[failed to parse search source [{"query":{"size":0,"query":{"filtered":{"query":{"query_string":{"analyze_wildcard":true,"query":"total_price_twd: [10 TO 10000]"}},"filter":{"bool":{"must":[{"range":{"departure_at":{"gte":1452164239142,"lte":1457348239142,"format":"epoch_millis"}}}],"must_not":[]}}}},"aggs":{"3":{"date_histogram":{"field":"departure_at","interval":"1d","time_zone":"Asia/Shanghai","min_doc_count":1,"extended_bounds":{"min":1452164239141,"max":1457348239141}}}}},"size":200}]]; nested: QueryParsingException[[_na] query malformed, no field after start_object]; }{[mVtcYMIfRxOBeqWvMbShhA][flights][4]: RemoteTransportException[[Payback][172.17.0.3:9300][indices:data/read/search[phase/query]]]; nested: SearchParseException[failed to parse search source [{"query":{"size":0,"query":{"filtered":{"query":{"query_string":{"analyze_wildcard":true,"query":"total_price_twd: [10 TO 10000]"}},"filter":{"bool":{"must":[{"range":{"departure_at":{"gte":1452164239142,"lte":1457348239142,"format":"epoch_millis"}}}],"must_not":[]}}}},"aggs":{"3":{"date_histogram":{"field":"departure_at","interval":"1d","time_zone":"Asia/Shanghai","min_doc_count":1,"extended_bounds":{"min":1452164239141,"max":1457348239141}}}}},"size":200}]]; nested: QueryParsingException[[_na] query malformed, no field after start_object]; }
elasticsearch_1 | at org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction.onFirstPhaseResult(TransportSearchTypeAction.java:228)

最佳答案

尝试将其包装在 body 中键并直接从 Client 执行目的:

Q_PARAMS = {
size: 200,
query: {
filtered: {
query: {
query_string: {
analyze_wildcard: true,
query: "total_price_twd: [10 TO 10000]"
}
},
filter: {
bool: {
must: [
{
"range":{
"departure_at":{
"gte":1452164239142,
"lte":1457348239142,
"format":"epoch_millis"
}
}
}
],
"must_not":[
]
}
}
}
},
"aggs":{
"3":{
"date_histogram":{
"field":"departure_at",
"interval":"1d",
"time_zone":"Asia/Shanghai",
"min_doc_count":1,
"extended_bounds":{
"min":1452164239141,
"max":1457348239141
}
}
}
}
}

def self.es_search
__elasticsearch__.client.search(body: Q_PARAMS)
end

关于elasticsearch - 如何使用 elasticsearch-rails DSL 转换 Elasticsearch 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35842203/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com