duanhuhong5255 2016-11-03 05:38 采纳率: 0%
浏览 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 创建一个数据库(要创建的表和记事本的代码截图))
  • ¥15 有没有整苹果智能分拣线上图像数据
  • ¥20 有没有人会这个东西的
  • ¥15 cfx考虑调整“enforce system memory limit”参数的设置
  • ¥30 航迹分离,航迹增强,误差分析
  • ¥15 Chrome Manifest扩展引用Ajax-hook库拦截请求失败
  • ¥15 用Ros中的Topic通讯方式控制小乌龟的速度,走矩形;编写订阅器代码
  • ¥15 LLM accuracy检测
  • ¥15 pycharm添加远程解释器报错
  • ¥15 如何让子窗口鼠标滚动独立,不要传递消息给主窗口