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

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 !!

图片转代码服务由CSDN问答提供 功能建议

我正在尝试使用 FOSElastica Bundle

我有一个全球索引查找器 在我的配置(yml文件)中:

  fos_elastica:
 clients:
 default:{host:%elastic_host%,port:%elastic_port%} 
 index:\  n myIndex:
 client:default 
 finder:〜
 types:
#different types here 
   
 
 

我按照文档使用它(< a href =“https://github.com/FriendsOfSymfony/FOSElasticaBundle/blob/master/Resources/doc/usage.md#searching-the-entire-index"rel =”nofollow“>这里):< / p>

  $ finder = $ this-&gt; container-&gt; get('fos_elastica.finder.myIndex'); 
 
 //返回映射的任何对象的混合数组 
 $ results = $ finder-&gt; find('whatever'); 
   
 
 

完美运行并返回预期 结果。 现在我想突出显示结果中的单词,例如使用ES的快速矢量荧光笔。 但我没有找到任何示例或任何文档。

我想我需要用以下内容定义更合适的\ Query对象: < pre> $ query = new \ Elastica \ Query(); $ query-&gt; setHighlights(array(“whatever”)); $ query-&gt; setTerm(“whatever”); \ n $ results = $ finder-&gt; find($ query);

但我找不到任何信息。 任何提示?

非常感谢!!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doushi1847
    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())
    ));
    
    点赞 评论
  • dongxiaoshe0737
    dongxiaoshe0737 2016-06-14 09:00

    The code above wasn't working for me but changing \stdObject() to \stdClass() made this perfect! (cf. https://github.com/FriendsOfSymfony/FOSElasticaBundle/blob/62061993640d0894d512587a0cda10ca7eb13c28/Resources/doc/cookbook/attachments.md)

    $searchQuery = new \Elastica\Query('This is a test');
    $searchQuery->setFields(["content"]);
    
    $searchQuery->setHighlight(array(
        "fields" => array("content" => new \stdClass())
    ));
    
    点赞 评论

相关推荐