来自 Haskell 的查询请求是 "same data" 的两倍

Query Request from Haskell with two times the "same data"

我正在尝试使用 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 中制作了 "equivalent":

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 中,我不能有两倍的 "query" 声明,这就是我写 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\":\"no [query] registered for [queryIn]\",\"line\":1,\"col\":39}],\"type\":\"parsing_exception\",\"reason\":\"no [query] registered for [queryIn]\",\"line\":1,\"col\":39},\"status\":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。 我不知道如何解决它,因为 Response 没有问题,但对于 Request 来说,这是个问题。

也许以前有人尝试过类似的东西。

我修好了。我不得不重新声明来自 aeson

toJSON 实例
instance ToJSON FunctionScore where
  toJSON (FunctionScore q f)   = object ["query" .= q, "functions" .= f]

而且效果很好。