来自Haskell的查询请求有两次"相同的数据"

时间:2017-03-02 15:21:36

标签: json haskell elasticsearch aeson http.client

我尝试用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
                                }
                            }
                        }
                    }
                }
            ]
        }
    }
}

它运作正常。

所以,我做了#34;等同于#34;在哈斯克尔:

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 =" {\"错误\":{\" ROOT_CAUSE \":[{\"类型\":\" parsing_exception \" \&#34 ; reason \":\"没有[query]注册[queryIn] \",\" line \":1,\" col \& #34; 39}],\"类型\":\" parsing_exception \" \"原因\":\"无[query]已注册[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。 我不知道如何解决它,因为响应不会有问题,但对于请求,这是一个问题。

也许有人曾经尝试过类似的东西。

1 个答案:

答案 0 :(得分:2)

我修好了。我必须从toJSON

重新声明aeson个实例
instance ToJSON FunctionScore where
  toJSON (FunctionScore q f)   = object ["query" .= q, "functions" .= f]

它运作良好。