当前位置: 首页 > 面试题库 >

ElasticSearch-格式错误的查询(RoR)

狄玉书
2023-03-14
问题内容

我正在尝试使用elasticsearch和ruby-on-
rails建立一个现有项目。我可以启动并运行该项目,但是elasticsearch查询为我返回了各种错误消息,而我的同事都没有:

[400] {"error":{"root_cause":[{"type":"parsing_exception","reason":"[and] query malformed, no start_object after query name","line":1,"col":896}],"type":"parsing_exception","reason":"[and] query malformed, no start_object after query name","line":1,"col":896},"status":400}

这显然使我相信这与我的设置有关。我已经安装了它们的相同版本(2.4),但是即使项目附带了setup命令,也是如此:

rake environment elasticsearch:import:all DIR=app/models FORCE=y

给我超时错误。我已经检查并且服务正在运行。如果我在此方面呆了2天,将对我的设置有什么问题的任何帮助将不胜感激。

提前致谢。

编辑:我不认为问题出在查询本身,因为它对我的同事有效,但如下所示:

{: date_range => {: from => Mon,
    01 Aug 2016 00: 00: 00 UTC + 00: 00,
    : to => Mon,
    31 Jul 2017 00: 00: 00 UTC + 00: 00
  }, : activities_only => nil
}, @pupils = [# < Pupil id: 1042, first_name: "Mark", last_name: "Mekhaiel", date_of_birth: "2017-03-19", level: 0, group_id: 95, created_at: "2017-07-31 08:59:59", updated_at: "2017-07-31 09:48:27", username: "m.mekhaiel1", picture: 0, color: 0, archived: false, sub_group_id: nil, score: 20, deleted_at: nil > ], @search = # < PupilCreditSearch: 0x007fc961801308 @interval = : month, @page = 1, @per = 25, @pupil_id = [1042], @date_range = {: from => Mon,
  01 Aug 2016 00: 00: 00 UTC + 00: 00,
  : to => Mon,
  31 Jul 2017 00: 00: 00 UTC + 00: 00
}, @queries = [# < Search::Query: 0x007fc964791bb8 @query = nil, @fields = [], @type = : multi_match, @options = {: fields => [],
  : type => : cross_fields
} > ], @filters = [# < Search::Filter: 0x007fc964756db0 @name = : pupil_id, @value = [1042], @type = : terms, @options = {} > ], @aggregations = {: years_created => # < Search::Aggregation: 0x007fc9647a32f0 @type = : date_histogram,
  @field = : created_at,
  @options = {: field => : created_at,
    : type => : date_histogram,
    : params => {: interval => : year
    }
  } > ,
  : months_created => # < Search::Aggregation: 0x007fc9647a30c0 @type = : date_histogram,
  @field = : created_at,
  @options = {: field => : created_at,
    : type => : date_histogram,
    : params => {: interval => : month
    }
  } > ,
  : date_ranges => # < Search::Aggregation: 0x007fc9647a2800 @type = : date_range,
  @field = : created_at,
  @options = {: field => : created_at,
    : type => : date_range,
    : params => {: keyed => true,
      : ranges => [{: from => "now/w",
        : to => "now+1w/w",
        : key => : this_week
      }, {: from => "now/M",
        : to => "now+1M/M",
        : key => : this_month
      }, {: from => "now-1w/w",
        : to => "now-2w/w",
        : key => : last_week
      }, {: from => "now-1M/M",
        : to => "now-2M/M",
        : key => : last_month
      }, {: from => "2016-09-01",
        : to => "2017-08-31",
        : key => : academic_year
      }]
    }
  } > ,
  : groups => # < Search::Aggregation: 0x007fc9647a2260 @type = : terms,
  @field = : pupil_id,
  @options = {: aggs => {: all_dates => # < Search::Aggregation: 0x007fc9647a22d8 @type = : date_histogram,
      @field = : created_at,
      @options = {: params => {: interval => : month
        },
        : aggs => {: total_points => # < Search::Aggregation: 0x007fc9647a2440 @type = : sum,
          @field = : points,
          @options = {} > ,
          : running_total => # < Search::Aggregation: 0x007fc9647a2350 @type = : cumulative_sum,
          @field = nil,
          @options = {: params => {: buckets_path => "total_points"
            }
          } >
        }
      } >
    }
  } > ,
  : options => # < Search::Aggregation: 0x007fc9647a1518 @type = : global,
  @field = nil,
  @options = {: aggs => {: filtered => # < Search::Aggregation: 0x007fc9647a1590 @type = : filter,
      @field = nil,
      @options = {: params => {: and => [{: range => {: created_at => {: gte => Mon,
                01 Aug 2016 00: 00: 00 UTC + 00: 00,
                : lt => Mon,
                31 Jul 2017 00: 00: 00 UTC + 00: 00
              }
            }
          }, {: terms => {: pupil_id => [1042]
            }
          }]
        },
        : aggs => {: categories => # < Search::Aggregation: 0x007fc9647a1b80 @type = : terms,
          @field = "category",
          @options = {: ignore => true
          } > ,
          : topic_ids => # < Search::Aggregation: 0x007fc9647a19a0 @type = : terms,
          @field = "topic_id",
          @options = {: ignore => true
          } > ,
          : learning_objective_ids => # < Search::Aggregation: 0x007fc9647a17c0 @type = : terms,
          @field = "learning_objective_id",
          @options = {: ignore => true
          } > ,
          : completed_activities => # < Search::Aggregation: 0x007fc9647a1608 @type = : terms,
          @field = : pupil_id,
          @options = {: aggs => {: activities => # < Search::Aggregation: 0x007fc9647a1680 @type = : filter,
              @field = nil,
              @options = {: params => {: and => [{: terms => {: category => [0, 1, 2, 3]
                    }
                  }]
                }
              } >
            }
          } >
        }
      } >
    }
  } >
}, @request = # < Search::Request: 0x007fc964753548 @params = {: query => {: bool => {: must => [{: match_all => {}
      }],
      : filter => [{: terms => {: pupil_id => [1042]
        }
      }]
    }
  },
  : aggs => {: years_created => {: date_histogram => {: field => : created_at,
        : interval => : year
      }
    },
    : months_created => {: date_histogram => {: field => : created_at,
        : interval => : month
      }
    },
    : date_ranges => {: date_range => {: field => : created_at,
        : keyed => true,
        : ranges => [{: from => "now/w",
          : to => "now+1w/w",
          : key => : this_week
        }, {: from => "now/M",
          : to => "now+1M/M",
          : key => : this_month
        }, {: from => "now-1w/w",
          : to => "now-2w/w",
          : key => : last_week
        }, {: from => "now-1M/M",
          : to => "now-2M/M",
          : key => : last_month
        }, {: from => "2016-09-01",
          : to => "2017-08-31",
          : key => : academic_year
        }]
      }
    },
    : groups => {: terms => {: field => : pupil_id
      },
      : aggs => {: all_dates => {: date_histogram => {: field => : created_at,
            : interval => : month
          },
          : aggs => {: total_points => {: sum => {: field => : points
              }
            },
            : running_total => {: cumulative_sum => {: buckets_path => "total_points"
              }
            }
          }
        }
      }
    },
    : options => {: global => {},
      : aggs => {: filtered => {: filter => {: and => [{: range => {: created_at => {: gte => Mon,
                  01 Aug 2016 00: 00: 00 UTC + 00: 00,
                  : lt => Mon,
                  31 Jul 2017 00: 00: 00 UTC + 00: 00
                }
              }
            }, {: terms => {: pupil_id => [1042]
              }
            }]
          },
          : aggs => {: categories => {: terms => {: field => "category"
              }
            },
            : topic_ids => {: terms => {: field => "topic_id"
              }
            },
            : learning_objective_ids => {: terms => {: field => "learning_objective_id"
              }
            },
            : completed_activities => {: terms => {: field => : pupil_id
              },
              : aggs => {: activities => {: filter => {: and => [{: terms => {: category => [0, 1, 2, 3]
                      }
                    }]
                  }
                }
              }
            }
          }
        }
      }
    }
  }
} >> , @options = # < ReportOptions: 0x007fc96b663258 @search = # < PupilCreditSearch: 0x007fc961801308 @interval = : month, @page = 1, @per = 25, @pupil_id = [1042], @date_range = {: from => Mon,
  01 Aug 2016 00: 00: 00 UTC + 00: 00,
  : to => Mon,
  31 Jul 2017 00: 00: 00 UTC + 00: 00
}, @queries = [# < Search::Query: 0x007fc964791bb8 @query = nil, @fields = [], @type = : multi_match, @options = {: fields => [],
  : type => : cross_fields
} > ], @filters = [# < Search::Filter: 0x007fc964756db0 @name = : pupil_id, @value = [1042], @type = : terms, @options = {} > ], @aggregations = {: years_created => # < Search::Aggregation: 0x007fc9647a32f0 @type = : date_histogram,
  @field = : created_at,
  @options = {: field => : created_at,
    : type => : date_histogram,
    : params => {: interval => : year
    }
  } > ,
  : months_created => # < Search::Aggregation: 0x007fc9647a30c0 @type = : date_histogram,
  @field = : created_at,
  @options = {: field => : created_at,
    : type => : date_histogram,
    : params => {: interval => : month
    }
  } > ,
  : date_ranges => # < Search::Aggregation: 0x007fc9647a2800 @type = : date_range,
  @field = : created_at,
  @options = {: field => : created_at,
    : type => : date_range,
    : params => {: keyed => true,
      : ranges => [{: from => "now/w",
        : to => "now+1w/w",
        : key => : this_week
      }, {: from => "now/M",
        : to => "now+1M/M",
        : key => : this_month
      }, {: from => "now-1w/w",
        : to => "now-2w/w",
        : key => : last_week
      }, {: from => "now-1M/M",
        : to => "now-2M/M",
        : key => : last_month
      }, {: from => "2016-09-01",
        : to => "2017-08-31",
        : key => : academic_year
      }]
    }
  } > ,
  : groups => # < Search::Aggregation: 0x007fc9647a2260 @type = : terms,
  @field = : pupil_id,
  @options = {: aggs => {: all_dates => # < Search::Aggregation: 0x007fc9647a22d8 @type = : date_histogram,
      @field = : created_at,
      @options = {: params => {: interval => : month
        },
        : aggs => {: total_points => # < Search::Aggregation: 0x007fc9647a2440 @type = : sum,
          @field = : points,
          @options = {} > ,
          : running_total => # < Search::Aggregation: 0x007fc9647a2350 @type = : cumulative_sum,
          @field = nil,
          @options = {: params => {: buckets_path => "total_points"
            }
          } >
        }
      } >
    }
  } > ,
  : options => # < Search::Aggregation: 0x007fc9647a1518 @type = : global,
  @field = nil,
  @options = {: aggs => {: filtered => # < Search::Aggregation: 0x007fc9647a1590 @type = : filter,
      @field = nil,
      @options = {: params => {: and => [{: range => {: created_at => {: gte => Mon,
                01 Aug 2016 00: 00: 00 UTC + 00: 00,
                : lt => Mon,
                31 Jul 2017 00: 00: 00 UTC + 00: 00
              }
            }
          }, {: terms => {: pupil_id => [1042]
            }
          }]
        },
        : aggs => {: categories => # < Search::Aggregation: 0x007fc9647a1b80 @type = : terms,
          @field = "category",
          @options = {: ignore => true
          } > ,
          : topic_ids => # < Search::Aggregation: 0x007fc9647a19a0 @type = : terms,
          @field = "topic_id",
          @options = {: ignore => true
          } > ,
          : learning_objective_ids => # < Search::Aggregation: 0x007fc9647a17c0 @type = : terms,
          @field = "learning_objective_id",
          @options = {: ignore => true
          } > ,
          : completed_activities => # < Search::Aggregation: 0x007fc9647a1608 @type = : terms,
          @field = : pupil_id,
          @options = {: aggs => {: activities => # < Search::Aggregation: 0x007fc9647a1680 @type = : filter,
              @field = nil,
              @options = {: params => {: and => [{: terms => {: category => [0, 1, 2, 3]
                    }
                  }]
                }
              } >
            }
          } >
        }
      } >
    }
  } >
}, @request = # < Search::Request: 0x007fc964753548 @params = {: query => {: bool => {: must => [{: match_all => {}
      }],
      : filter => [{: terms => {: pupil_id => [1042]
        }
      }]
    }
  },
  : aggs => {: years_created => {: date_histogram => {: field => : created_at,
        : interval => : year
      }
    },
    : months_created => {: date_histogram => {: field => : created_at,
        : interval => : month
      }
    },
    : date_ranges => {: date_range => {: field => : created_at,
        : keyed => true,
        : ranges => [{: from => "now/w",
          : to => "now+1w/w",
          : key => : this_week
        }, {: from => "now/M",
          : to => "now+1M/M",
          : key => : this_month
        }, {: from => "now-1w/w",
          : to => "now-2w/w",
          : key => : last_week
        }, {: from => "now-1M/M",
          : to => "now-2M/M",
          : key => : last_month
        }, {: from => "2016-09-01",
          : to => "2017-08-31",
          : key => : academic_year
        }]
      }
    },
    : groups => {: terms => {: field => : pupil_id
      },
      : aggs => {: all_dates => {: date_histogram => {: field => : created_at,
            : interval => : month
          },
          : aggs => {: total_points => {: sum => {: field => : points
              }
            },
            : running_total => {: cumulative_sum => {: buckets_path => "total_points"
              }
            }
          }
        }
      }
    },
    : options => {: global => {},
      : aggs => {: filtered => {: filter => {: and => [{: range => {: created_at => {: gte => Mon,
                  01 Aug 2016 00: 00: 00 UTC + 00: 00,
                  : lt => Mon,
                  31 Jul 2017 00: 00: 00 UTC + 00: 00
                }
              }
            }, {: terms => {: pupil_id => [1042]
              }
            }]
          },
          : aggs => {: categories => {: terms => {: field => "category"
              }
            },
            : topic_ids => {: terms => {: field => "topic_id"
              }
            },
            : learning_objective_ids => {: terms => {: field => "learning_objective_id"
              }
            },
            : completed_activities => {: terms => {: field => : pupil_id
              },
              : aggs => {: activities => {: filter => {: and => [{: terms => {: category => [0, 1, 2, 3]
                      }
                    }]
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

问题答案:

问题在于聚合之一,即:

:filter => { :and => [...] }

…这使我得出的结论是,您实际上可能在计算机上运行的是Elasticsearch 5而不是2.4,因为在第5版中删除了该语法,而支持:

:filter => { :bool => { :must: [...] } }

使用curl localhost:9200(或您的应用连接到的实际主机/端口)验证elasticsearch版本。



 类似资料:
  • 我试图在Elasticsearch中创建一个具有以下条件的手表: 字段“ht”必须小于100 字段“SV”不能是“Tier1”,字段“RV”可以是其他任何内容。 字段“SV”不得为“Tier2”,字段“RV”不得为“Red”。 这是我此刻的身体搜索,但它返回: “[term]格式错误的查询,应为[END_OBJECT],但找到[FIELD_NAME]”: 你能帮帮我吗?谢了。

  • 我已经使用映射查询在ElasticSearch中执行搜索,它工作得很好,如下所示。 然而,即使我添加了“terms”,我也会得到错误:TransporDeror(400,u'parsing_exception',u'[term]格式错误的查询,预期的[END_OBJECT]但找到了[FIELD_NAME]')

  • 有人可以解释一下这个查询有什么问题吗?我需要将此生成的查询从弹性搜索 2 转换为弹性搜索 6。在ES2中,这个工作得很好,但在ES6中,它给我一个错误:[或]查询格式不正确,查询名称后没有start_object。我迷失在其中。OR是必要的,因为可能有更多的条件比这个。 谢谢。

  • 我试图在Python post请求中复制以下json params: 这是我字典里的内容: 然后,我用json.dumps将这个字典传递到我的post请求中,这样它就被转换为json并与post请求一起发送。 data=json.dumps(有效负载) 但是,当我得到以下错误时,一切都不起作用: {“type”:“elasticsearch”,“message”:“[parsing_excepti

  • 问题内容: 我正在针对AWS Elasticsearch 5.1运行此查询,并收到格式错误的查询错误。这是请求的正文。我基本上只是在检查时间范围内是否存在该字段。 问题答案: 第二条陈述不正确:

  • 有两个实体Person和Address。人与地址之间存在1:M关系。(假定某人有临时和永久地址)。 Person类的关键属性是: < li>personId(pk) < li >性别 地址类的主要属性是: 地址id(pk) 人(fk) 性别 以下是Person和Address类的描述符代码片段: 以下是用于生成动态查询的代码片段: 运行此程序时,根据日志生成的查询: 如何编写表达式在第一个join