摄取搜索(弹性搜索)在PHP中不起作用

时间:2017-01-22 16:28:59

标签: php elasticsearch full-text-search elasticsearch-plugin

我已经安装了弹性搜索,并在我的本地服务器中安装了摄取插件。弹性引擎运行良好。我设置了以下任务:

  1. 映射
  2. 索引
  3. 现在我被困在搜索中,它不能正常工作。它返回null数组。这是我在PHP中的代码:

    public function ingest_processor_searching($query)
    {
        $client = $this->client;
        $params = [
            'index' => 'ingest_index',
            'type' => 'attachment',
            'body' => [
                'query' => [
                    'match' => [
                        'textField' => $query,
                    ]
                ],
            ],
        ];
    
        $response = $client->search($params);
        return $response;
    }
    

    结果:

    {
    "took": 7,
    "timed_out": false,
    "_shards": {
       "total": 5,
       "successful": 5,
       "failed": 0
     },
      "hits": {
      "total": 0,
      "max_score": null,
      "hits": []
     } 
    }
    

    但我有GET http://localhost:9200/ingest_index/attachment/2

    的数据
    {
      "_index": "ingest_index",
      "_type": "attachment",
      "_id": "2",
      "_version": 1,
      "found": true,
      "_source": {
        "file_path": "/Users/selimreza/Sites/haber_dev/public/uploads/files/bower.txt",
        "attachment": {
          "content_type": "text/plain; charset=ISO-8859-1",
          "language": "en",
          "content": "Welcome to Dhaka",
          "content_length": 18
        },
        "textField": "V2VsY29tZSB0byBEaGFrYQo="
      }
    }
    

    我犯了什么错误?

1 个答案:

答案 0 :(得分:0)

尝试从,删除'textField' => $query,因为您没有匹配多个值。如果仍然无效,请尝试使用term查询而不是match

 $params = [
        'index' => 'ingest_index',
        'type' => 'attachment',
        'body' => [
            'query' => [
                'term' => [ <-- have this
                    'textField' => $query <-- try removing the comma
                ]
            ],
        ],
    ];