2

我正在尝试使用FOSElastica Bundle通过全局索引搜索返回亮点。

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

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

我按照文档(这里)使用它:

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

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

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

我想我需要定义一个更合适的 \Query 对象,例如:

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

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

但我找不到任何信息。有什么提示吗?

非常感谢 !!

4

2 回答 2

5

首先用 JSON 编写查询:

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

当它工作时,转换为 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())
));
于 2014-06-28T15:00:18.993 回答
0

上面的代码对我不起作用,但更改\stdObject()\stdClass() 使其完美!(参见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())
));
于 2016-06-14T09:00:10.100 回答