我正在尝试使用 Haskell (Aeson) 向 Elasticsearch 发出 Http 请求。
Elasticsearch 主体看起来是这样的:
{
"query": {
"function_score": {
"query": {
"bool": {
"should": [
{"term": {"word_n_gram": "str"}},
{"term": {"word_n_gram": "not"}}
]
}
},
"functions": [
{
"script_score": {
"script": {
"lang": "groovy",
"file": "test-score",
"params": {
"boostBy": {
"str": 1,
"not": 1
}
}
}
}
}
]
}
}
}
它工作正常。
所以,我在 Haskell 中做了“等价”:
data QueryRequest = QueryRequest {
query :: Query
} deriving (Eq, Generic, Show)
instance ToJSON QueryRequest
data Query = Query {
function_score :: FunctionScore
} deriving (Eq, Generic, Show)
instance ToJSON Query
data FunctionScore = FunctionScore {
queryIn :: QueryIn
, functions :: [Functions]
} deriving (Eq, Generic, Show)
instance ToJSON FunctionScore
data QueryIn = QueryIn {
bool :: BoolQuery
} deriving (Eq, Generic, Show)
instance ToJSON QueryIn
data BoolQuery = BoolQuery {
should :: [ShouldQuery]
} deriving (Eq, Generic, Show)
等等...
关键是,在haskell中,我不能有两次“查询”声明,这就是我写的原因,queryIn
但是,因为我正在发出请求,而Elasticsearch正在等待query
两次,我收到了这个错误:
FailureResponse {responseStatus = Status {statusCode = 400, statusMessage = "Bad Request"}, responseContentType = application/json;charset=UTF-8, responseBody = "{\"error\":{\"root_cause\":[{\ "type\":\"parsing_exception\",\"reason\":\"没有为 [queryIn] 注册 [query]\",\"line\":1,\"col\":39}],\ "type\":\"parsing_exception\",\"reason\":\"没有为 [queryIn] 注册 [query]\",\"line\":1,\"col\":39},\"状态\":400}"}
这是一个逻辑错误。但我不知道我该如何解决它......
我以这种方式制作“RequestQuery”:
toElasticSearchQuery :: T.Text -> RW.QueryRequest
toElasticSearchQuery word =
RW.QueryRequest {
RW.query = RW.Query {
RW.function_score = RW.FunctionScore {
RW.queryIn = RW.QueryIn {
RW.bool = RW.BoolQuery {
RW.should = toShouldQueryList (splitInNGrams word)
}
},
RW.functions = [
RW.Functions {
RW.scriptScore = RW.ScriptScore {
RW.script = RW.Script {
RW.lang = scriptLang,
RW.file = scriptFile,
RW.params = RW.Params {
RW.boostBy = fixGramConter (splitInNGrams word)
}
}
}
}
]
}
}
}
当然,我不能在 RW.FunctionScore 中编写 RW.query。我不知道如何解决它,因为响应没有问题,但对于请求,这是一个问题。
也许有人以前尝试过类似的东西。