1

I'm hoping someone can help with the syntax for a simple query of ElasticSearch in a Laravel controller.

I have managed to index and output to a blade template in the view but I can't get the query right to enable the search form variable to perform a search on my seed data.

Search method from controller:

public function searchPlugins() {

    $client = Elasticsearch\ClientBuilder::create()->build();

    $query2 = Request::input('query2');

    $params = [
        'index' => 'partnerpages',
        'type' => 'plugins',
        'body' => [
            'query' => $query2['query2']
        ]
    ];

    $plugins = $client->search($params);

    return View::make('search2')->with('plugins', $plugins);
}

I just can't get the query in the params array right - I could only get it to output for a specific field and keyword.

Any help much appreciated, thanks in advance.

EDIT

Code to output in the view in my blade template:

<!-- Search engine -->
            <div class="col-md-8">

                {{ Form::open(array('route' => 'search-plugins2', 'class' => 'form')) }}

                {{ Form::input('search', 'query2', Input::get('query2', ''))}}
                {{ Form::submit('Search plugins') }}

                {{ Form:: close() }} 

            </div><!-- end of Search engine -->

            <div class="col-md-8">

                 <!-- insert Search engine -->
                <br/>
                <h1>Plugin results</h1>
                <br/>


                <div class="panel panel-default">
                    <div class="panel-body"><h2></h2>
                    <div><?php print_r($plugins);?></div>
                    <div></div>
                    <div><small></small></div>
                </div>
                     </div>
            </div><!-- end of row -->

2 Answers 2

1

You need to construct the query using the query DSL. You can start using the query_string query like this:

$params = [
    'index' => 'partnerpages',
    'type' => 'plugins',
    'body' => [
        'query' => [
            'query_string' => [
                'query' => $query2['query2']
            ]
        ]
    ]
];
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @Val, I'm getting the error "Illegal string offset 'query2'" which I understand to means I'm trying to access the value of an array using a key which doesn't exist i.e. query2. If I hard code the query then fine. I'll add the code from the view incase there's an issue there??
Just including the variable 'query' => $query2 worked - great! Thanks again Val. Still having problems outputting the array in a foreach loop though. Maybe because it's a multidimensional array, have resorted to print_r
0

Just including the variable in the form of 'query' => $query2 worked for me. Thanks again Val for pointing me in the right direction.

EDIT

With updated code:

    public function searchPlugins() {

        $client = Elasticsearch\ClientBuilder::create()->build();

        $query2 = Input::get('query2', 'RSS');

        $params = [
    'index' => 'partnerpages',
    'type' => 'plugins',
    'body' => [
        'query' => [
            'query_string' => [
                'query' => $query2
            ]
        ]
    ]
];

$plugins = $client->search($params);

        return View::make('search2')->with('plugins', $plugins);
    }

1 Comment

This is NOT an answer. This should be a comment. Please delete this, and make it a comment on the answer @val provided - OR, turn this in to a FULL answer, demonstrating the code that you used.

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.