dongren9739 2014-06-21 09:49
浏览 86
已采纳

FOSElastica Bundle:检索结果的亮点

I'm trying to have highlights returned with a global index search with the FOSElastica Bundle.

I have a global index finder in my configuration (yml file):

fos_elastica:
  clients:
    default: { host: %elastic_host%, port: %elastic_port% }
  indexes:
    myIndex:
      client: default
      finder: ~
      types: 
       # different types here

and I use it as per the doc (here) :

$finder = $this->container->get('fos_elastica.finder.myIndex');

// Returns a mixed array of any objects mapped
$results = $finder->find('whatever');

That works perfectly and returns the expected results. Now I would like to highlight the words found in the results using for instance the fast vector highlighter of ES. But I did not find any example or any documentation to do so.

I guess I need to define a more proper \Query object with something like :

$query = new \Elastica\Query();
$query->setHighlights(array("whatever"));
$query->setTerm("whatever");

$results = $finder->find($query);

But I cannot find any information. Any hint ?

Thanks a lot !!

  • 写回答

2条回答 默认 最新

  • doushi1847 2014-06-28 15:00
    关注

    Write the query in JSON first:

    {
        "query" : {
            "match" : {
                "content" : "this is a test"
            }
        },
        "highlight" : {
            "fields" : {
                "content" : {}
            }
        }
    }
    

    When it works, translate to Elastica:

    $matchQuery = new \Elastica\Query\Match();
    $matchQuery->setField('content', 'this is a test');
    
    $searchQuery = new \Elastica\Query();
    $searchQuery->setQuery($matchQuery);
    
    $searchQuery->setHighlight(array(
        "fields" => array("content" => new \stdObject())
    ));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥66 如何制作支付宝扫码跳转到发红包界面
  • ¥15 pnpm 下载element-plus
  • ¥15 解决编写PyDracula时遇到的问题
  • ¥15 有没有人能解决下这个问题吗,本人不会编程
  • ¥15 plotBAPC画图出错
  • ¥30 关于#opencv#的问题:使用大疆无人机拍摄水稻田间图像,拼接成tif图片,用什么方法可以识别并框选出水稻作物行
  • ¥15 Python卡尔曼滤波融合
  • ¥20 iOS绕地区网络检测