如何在Elastica / FOSElasticaBundle中使用highlight_query?

时间:2016-07-28 13:29:10

标签: php elasticsearch elastica foselasticabundle

我是Elastica的新手,我正在寻找一种通过highlight_query突出显示嵌套查询结果的方法。我检查了代码,$ query-> setHighlight()只接受一个数组作为参数。也许还有另一种方法可以使用Elastica来实现这个结果。

这是我想要翻译成Elastica的json查询:

{
    "query": {
        "bool": {
            "must": [
        {
            "match": {
            "publishAt": "2016"
          }
        },
        {
            "nested": {
            "path": "translations",
            "query": {
                "multi_match": {
                    "query": "leadership",
                "fields": [
                        "translations.*"
                    ]
              }
            }
          }
        },
        {
            "nested": {
            "path": "translations",
            "query": {
                "bool": {
                    "must": [
                  {
                      "match": {
                      "translations.locale": "fr"
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  },
  "highlight": {
        "highlight_query": {
            "match": {
                "translations.*": "leadership"
      }
    },
    "fields": {
            "translations.*": {}
    }
  }

我正在使用FosElasticaBundle并且我没有突出显示这个查询:

    $query = new Query();
    $bool = new Bool();

    $yearQuery     = new Match();
    $yearQuery->setField('publishAt', 2016);
    $bool->addMust($yearQuery);

    $nestedQuery  = new Query\Nested();
    $nestedQuery->setPath('translations');

    $multiMatch = new Query\MultiMatch();
    $multiMatch->setQuery($string);
    $multiMatch->setFields('translations.*');
    $nestedQuery->setQuery($multiMatch);

    $nestedQuery2  = new Query\Nested();
    $nestedQuery2->setPath('translations');

    $nestedBool  = new Bool();
    $localeQuery = new Match();
    $localeQuery->setField('translations.locale', $request->getLocale());
    $nestedBool->addMust($localeQuery);
    $nestedQuery2->setQuery($nestedBool);

    $bool->addMust($nestedQuery);
    $bool->addMust($nestedQuery2);

    $query->setQuery($bool);

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

0 个答案:

没有答案