duanhuhong5255 2016-11-03 05:38
浏览 29
已采纳

php全文搜索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."
";

    $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

  • 写回答

1条回答 默认 最新

  • douzhun5971 2016-11-08 19:17
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么