简体   繁体   中英

php full text search in elasticsearch

I have a json file which I looped through it and index it in elastic. and after that I want to be able to search through my data.

this is my json file :

https://github.com/mhndev/iran-geography/blob/master/tehran_intersection.json

which looks like :

{
   "RECORDS":[
      {
         "first":{
            "name":"ابن بابویه",
            "slug":"Ibn Babawayh"
         },
         "second":{
            "name":"میرعابدینی",
            "slug":"Myrabdyny"
         },
         "latitude":"35.601605",
         "longitude":"51.444208",
         "type":"intersection",
         "id":1,
         "search":"ابن بابویه میرعابدینی,Ibn Babawayh Myrabdyny,ابن بابویه تقاطع میرعابدینی,Ibn Babawayh taghato Myrabdyny",
         "name":"ابن بابویه میرعابدینی",
         "slug":"Ibn Babawayh Myrabdyny"
      },

...
]
}

when my query is : "mir", I expect my result to be records which has "mirabedini", "mirdamad", "samir", and every other word which contains this string.

but I just get words which are exactly "mir"

and this is my php code for search :

$fields = ['search','slug'];

$params = [
    'index' => 'digipeyk',
    'type' => 'location',
    'body' => [
        'query' => [
            'match' => [
                'fields' => $fields,
                'query' => $_GET['query']
            ]
        ],

    'from' => 0,
    'size' => 10
    ]
];



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

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

and also this my php code for indexing documents.

$client = ClientBuilder::create()->build();
$deleteParams = ['index' => 'digipeyk'];
$response = $client->indices()->delete($deleteParams);

$intersections = json_decode(file_get_contents('data/tehran_intersection.json'), true)['RECORDS'];
$i = 1;

foreach($intersections as $intersection){

    echo $i."\n";

    $params['index'] = 'digipeyk';
    $params['id'] = $intersection['id'];
    $params['type'] = 'location';
    $params['body'] = $intersection;

    $response = $client->index($params);

    $i++;
}

I'm using php 7 and elasticsearch 2.3

match query doesn't support wildcard query by default, so you have to use wildcard instead of that.

$fields = ['search','slug'];

$params = [
    'index' => 'digipeyk',
    'type' => 'location',
    'body' => [
        'query' => [
            'wildcard' => [
                'query' => '*'.$_GET['query'].'*'
            ]
        ],

    'from' => 0,
    'size' => 10
    ]
];



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

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

For more information about wildcard in elastic visit following link : https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-wildcard-query.html

For that:

'query' => $_GET['query']

You will be burn in hell :D

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM