Elasticsearch PHP

I do not know if this is the right place to post this or if I've explained the problem well enough. If there is a problem with how I explained it or if there is a more appropiate place to post this I would be glad to know.

I've a simple search engine in PHP that uses Elasticsearch. It works fine for searching through most of my database but have problem with two of the fields. These fields are "title" and "director". They are used exclusively in documents mapped in localhost:9200/movies/movie/. Another field that is used here is "year". When searching for the document with the title "The Godfather" and the director "Francis Ford Coppola" I get no results by searching for "The Godfather" or "Francis Ford Coppola". If i search for 1977, which is the value of the "year" field, I do get the result and is told the "title", "director", and "year". All other fields in the search seems to be working fine.

This is the PHP code. I'm using Elasticserach 6.2.

<?php
error_reporting(0);
require_once 'vendor/autoload.php';
use Elasticsearch\ClientBuilder;
$client = ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();
$es = $client;
if (isset($_GET['q'])) {
    $q = $_GET['q'];
    $query = $es->search([
        'body' => [
            'query' => [
                'bool' => [
                    'should' => [
                        'multi_match' => [
                            'query' => $q,
                            'type' => 'phrase_prefix',
                            'fields' => [
                                'name', 'type', 'content',
                                'title', 'director', 'year',
                                'play_name', 'speaker'],
                            'operator' => 'and'
                            ]
                        ]
                    ]
                ]
            ]
        ]);
}   
if($query['hits']['total'] >=1 ) {
    $results = $query['hits']['hits'];
}
?>
<!doctype html> 
<html>
    <head>  
        <meta charset="utf-8">
        <title>Søkemotor</title>
        <link rel="stylesheet" href="css/main.css">
    </head>
    <body>
        <div class="img">
            <img src="img/DigRevLogo" alt="Logo" width="600" height="150" class="img">
        </div>

        <div class="search">
            <form action="index.php" method="get" autocomplete="off" class="search_form">
                <label>

                    <input type="text" name="q" placeholder="Søk her">
                </label>

                <label><input type="submit" value="Søk" name="s"></label>
            </form>
        </div>
        <?php
        $noresult = "Ingen resultat på søket av $q.";
        if(isset($results)) {
            foreach($results as $r) {
        ?>
            <div class="result">
                <a href="#<?php echo $r['_id']; ?>"><?php echo $r['_source']['name'];?></a> 
                <a href="#<?php echo $r['_id']; ?>"><?php echo $r['_source']['title'];?></a>
                <a href="#<?php echo $r['_id']; ?>"><?php echo $r['_source']['play_name'];?></a>
                <br>
                <?php echo $r['_source']['type'];?> 
                <?php echo $r['_source']['director'];?> 
                <br>
                <?php echo $r['_source']['content'];?>
                <?php echo $r['_source']['year'];?>
                <?php echo $r['_source']['speaker']?>
                <?php echo $r['_source']['description'];?>
            </div>
            <div class="noresult">
        <?php 
            }
        }
            else echo "<CENTER>$noresult</CENTER>";
        ?>
            </div>
    </body>
</html>

Could you provide a full recreation script as described in About the Elasticsearch category. It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

A full reproduction script will help readers to understand, reproduce and if needed fix your problem. It will also most likely help to get a faster answer.

Please do it using Kibana console as lot of people might not be able to understand PHP code.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.