polyfractal / sherlock

MIT License
119 stars 28 forks source link

The DeleteDocumentRequest interface #51

Closed kwisatz closed 11 years ago

kwisatz commented 11 years ago

I have a question on the DeleteDocumentRequest class.

There's the documents() method that allows deleting multiple documents in a single go. However, the method expects its argument to be either an instance of BatchCommand or an array of Command instances.

Now, I haven't figured out how to create such a Batch using the DeleteDocumentRequest object itself. Am I missing something?

I've been solving my issue like this for the moment:

use Sherlock\requests\Command;
[...]

$documents = array();
foreach($response->hits as $hit) {
    $command = new Command;
    $documents[] = $command->id($hit['id'])->action('delete');
}

$delete = $sherlock
    ->deleteDocument()
    ->index('fde')
    ->type('booth')
    ->documents($documents);

$resp = $delete->execute();

However, if I've really missing something, shouldn't there be another method in DeleteDocumentRequest, such as:

    /**
     * Retrieve the BatchCommand array, for
     * instance for usage in the documents() method.
     * @return object BatchCommand object of Commands
     */
    public function retrieveBatch()
    {
        if (!$this->batch || !$this->batch instanceof BatchCommand) {
            Analog::error("There is no BatchCommand to be retrieved");
            throw new exceptions\RuntimeException("There is no BatchCommand to be retrieved. Did you forget to call document() ?");
        }

        return $this->batch;
    }

Thanks for shedding some lights onto this.

kwisatz commented 11 years ago

Never mind... I suppose I needed a break to figure out that I could just as easily:

                   $delete = $app['sherlock']
                        ->deleteDocument()
                        ->index('fde')
                        ->type('booth');
                    foreach($response->hits as $hit) {
                         $delete->document($hit['id']);
                    }
                    $resp = $delete->execute();
polyfractal commented 11 years ago

Yep, that's how you do it. The Batch stuff is really only exposed if you have a very large batch of documents that you need deleted (or indexed, which is where the notation concept originally came from). It allows you to write a custom BatchCommand class that streams from your data source rather than loading it entirely into memory.

As with most places, I'm not overly happy with the syntax. Check out some discussions that Mopster and I had about the document interface (index/delete/get/update) on issue #34