Jul 192015
 

Elastic search is one of the best search engines out there. this is the 2nd search engine that I am currently using aside from solr.
At the time of posting this article, I can’t find any working example over the internet nor can find a decent example in the
documentation. I also posted this simple snippet on stackoverflow just incase this blog is gone, I can still find my
own solution there.

some guy from stackoverflow suggested these:

“It’s possible, but you can’t update just one field alone, because ES don’t works this way.
You need to retrieve full document first, change field value and save document to ES again (with the same id as before).
Except this, multiple upsert (there are no difference between add and update operations) looks like below:”

$client = new Elastica\Client();

// Call method from client
$client->addDocuments([
    new Elastica\Document(/*...*/),
    new Elastica\Document(/*...*/),
    new Elastica\Document(/*...*/),
]);

// Or from index
$client->getIndex('index')->addDocuments([
    new Elastica\Document(/*...*/),
    new Elastica\Document(/*...*/),
    new Elastica\Document(/*...*/),
]);

// Or from type
$client->getIndex('index')->getType('type')->addDocuments([
    new Elastica\Document(/*...*/),
    new Elastica\Document(/*...*/),
    new Elastica\Document(/*...*/),
]);

however, the solution to my problem is this

    $index = $this->client->getIndex($this->index);
    $type = $index->getType($this->type);
    $doc = new \Elastica\Document(
            $object->whateverIDisIt, $object, $this->type, $this->index
    );
    $response = $type->updateDocument($doc);

that code snippet above is for single document update. if you have more than one, then probably it’s inside an
object array. Therefore, the snippet above would probably be inside a foreach loop