Coder Social home page Coder Social logo

Comments (4)

robinp avatar robinp commented on August 16, 2024 1

Thank you! Just a not for posterity, one still needs to use the parseEsResponse helper, since on full-batch level errors, ES still returns that kind of error.

from bloodhound.

robinp avatar robinp commented on August 16, 2024

FYI sketch of something:

data BulkResponse = BulkResponse                           
    { bulkTook        :: Int
    , bulkErrors      :: Bool
    , bulkActionItems :: [BulkActionItem]
    }                                                                                                                                                                                                                                                                                                                        
    deriving (Show, Generic)

data BulkActionItem = BulkActionItem
    { baiAction :: BulkAction
    , baiItem   :: BulkItem                                                                                                                                   
    }  
    deriving (Show, Generic)
                                                                               
data BulkItem = BulkItem
    { biIndex  :: Text
    , biId     :: Text
    , biStatus :: Maybe Int
    , biError  :: Maybe BulkError
    }
    deriving (Show, Generic)

data BulkAction = Index | Create | Delete | Update
    deriving (Show, Generic)

data BulkError = BulkError
    { beType   :: Text
    , beReason :: Text
    }
    deriving (Show, Generic)

instance FromJSON BulkResponse where
    parseJSON = withObject "BulkResponse" $ \o ->
        BulkResponse <$> o .: "took" <*> o .: "errors" <*> o .: "items"

instance FromJSON BulkActionItem where
    parseJSON j =
        parseItem Index j
            <|> parseItem Create j
            <|> parseItem Delete j
            <|> parseItem Update j
      where
            -- | The object has a single key: value pair, where the key encodes 
            -- the action.
        parseItem :: BulkAction -> A.Value -> A.Parser BulkActionItem
        parseItem action = withObject "BulkActionItem" $ \o -> do
            v <- o .: toS actionText
            pure $! BulkActionItem { baiAction = action, baiItem = v }
          where
            actionText :: Text
            actionText = case action of
                Index  -> "index"
                Create -> "create"
                Delete -> "delete"
                Update -> "update"

instance FromJSON BulkItem where
    parseJSON = withObject "BulkItem" $ \o ->
        BulkItem
            <$> o
            .:  "_index"
            <*> o
            .:  "_id"
            <*> o
            .:? "status" -- allegedly present but ES example shows a case where it is missing.. so..
            <*> o
            .:? "error"

instance FromJSON BulkError where
    parseJSON = withObject "BulkError"
        $ \o -> BulkError <$> o .: "type" <*> o .: "reason"

testErrorBody :: Text
testErrorBody = [text|
            {"took":0,"errors":true,"items":[{"index":{"_index":"theidx","_type":"_doc","_id":"some:id:1","status":429,"error":{"type":"cluster_block_exception","reason":"index [theidx] blocked by: [TOO_MANY_REQUESTS/12/disk usage exceeded flood-stage watermark, index has read-only-allow-delete block];"}}}]}
        |]

testSuccessBody :: Text
testSuccessBody = [text|
     {"took":7, "errors": false, "items":[{"index":{"_index":"test","_id":"1","_version":1,"result":"created","forced_refresh":false}}]}
     |]

Feel free to use.

from bloodhound.

blackheaven avatar blackheaven commented on August 16, 2024

Thanks for your report and your contribution, I'll have a look tomorrow.

from bloodhound.

blackheaven avatar blackheaven commented on August 16, 2024

I have pushed it! Thanks a lot!

from bloodhound.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.