0

I am Using Elasticsearch 2.3 along with the official php driver. The updateByQuery is giving me troubles to use in php. A little help on how to use it will be appreciated.

        $client = \Elasticsearch\ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();
        # Request
        $updateRequest = [
            'index'     => 'gorocket',
            'type'      => 'logs',
            'body' => [
                'query' => [ 
                    'filtered' => [
                        'filter' => [
                            'bool' =>   [
                                            'must' =>
                                            [
                                                [
                                                    'match' => [ 'enabled' => 1 ],
                                                ],
                                            ]
                                        ]
                                    ]
                                ]
                            ]
                  ]
            ]
        ];
        # Update 
        $results = $client->updateByQuery($updateRequest);

Basically i want to update a couple of document fields(name,price) that matches a certain query

Thank you.

1
  • See above, i've included a sample code( I don't see why it was necessary because all i asked was a sample code or a clear documentation and i could take it from there ). Who ever down-voted this please provide a reason. Commented Aug 26, 2016 at 7:57

2 Answers 2

2

So, with the help of how the CURL api works i managed to come up with a way.

First you need to edit your elasticsearch.yml to allow Scripting. Append the following lines at the end.

script.engine.groovy.inline.search: on
script.engine.groovy.inline.aggs: on
script.engine.groovy.inline.update: on

There after you can start doing batch updates using queries as the example bellow.

    $client = \Elasticsearch\ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();
    # Request
    $updateRequest = [
        'index'     => 'testindex',
        'type'      => 'logs',
        'conflicts' => 'proceed',
        'body' => [
            'query' => [ 
                'filtered' => [
                    'filter' => [
                        'bool' =>   [
                                        'must' =>
                                        [
                                            [
                                                'match' => [ 'enabled' => 1 ],
                                            ],
                                        ]
                                    ]
                                ]
                            ]
                        ],
            'script' => [
                    'inline' => 'ctx._source.enabled = value',
                    'params' => [
                        'value' => 0
                    ]
            ]
            ]
        ]
    ];
    # Update 
    $results = $client->updateByQuery($updateRequest);

That's it. It is not easily and well documented as of now so.

Sign up to request clarification or add additional context in comments.

Comments

1

I want to add a small addition to the previous answer

You may not add the following params in elasticsearch.yml

script.engine.groovy.inline.search: on
script.engine.groovy.inline.aggs: on
script.engine.groovy.inline.update: on

And your query will be:

$client = \Elasticsearch\ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();
# Request
$updateRequest = [
    'index'     => 'testindex',
    'type'      => 'logs',
    'conflicts' => 'proceed',
    'body' => [
        'query' => [ 
            'filtered' => [
                'filter' => [
                    'bool' =>   [
                        'must' => [
                            [
                                'match' => [ 'enabled' => 1 ],
                            ],
                        ]
                    ]
                ]
            ]
        ],
        'script' => [
            'lang' => 'painless',
            'source' => 'ctx._source.enabled = params.value',
            'params' => [
                'value' => 0
            ]
        ]
    ]
];

# Update 
$results = $client->updateByQuery($updateRequest);

2 Comments

you possibly have an extra ] or two
sorry, CopyPaste from previous answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.