dongmei8760 2019-04-07 11:50
浏览 90
已采纳

使用Elasticsearch的Symfony Ajax搜索栏

I want to use Elasticsearch in my Project. I have the following code:

SearchController

/**
 * @Route("/api/elasticsearch" )
 */
public function viewElasticsearch(Request $request)
{
    $query = $request->get('term');

    if (empty($query)) {
        return new JsonResponse([]);
    }

    if (substr($query, -1) !== '*') {
        $query .= '';
    }

    $finder = $this->container->get('fos_elastica.finder.app.product');
    $products = $finder->find($query);

    foreach ($products as $entity) {
        $result['results'][] = [
            'id' => $entity->getId(),
            'text' => $entity->getName(),
            'sku' => $entity->getSku(),
        ];
    }

    return new JsonResponse($result);
}

Javascript

<script>
    $('.js-data-example-ajax').select2({
        ajax: {
            url: '/product/api/elasticsearch',
            dataType: 'json',
            width: 'resolve', // need to override the changed default
            minimumResultsForSearch: -1,
            dropdownCssClass: 'select2-hidden',
            }
    });
</script>

If I call a search query through the command line :

curl -X GET 'http://localhost:9200/app/product/_search?q=*iphone*&size=500'

I get 381 results from the search above, but if I do the same search from Symfony I get 8 results. How can I do a wildcard search in Symfony ?

  • 写回答

1条回答 默认 最新

  • dongque5529 2019-04-07 15:09
    关注

    The finder of Elastichsearch is showing 10 results by default. If you want to change it you have to add a second parameter to the find method with a value you want. In my case I want to show 500 results.

    $finder = $this->container->get('fos_elastica.finder.app.product');
    $products = $finder->find($query, '500');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置