2

I'm new to Elastica and I'm searching for a way to highlight nested query results with highlight_query. I checked the code and $query->setHighlight() accepts only an array as a parameter. Maybe there's another way to achieve this result using Elastica.

This is the json query I'm trying to translate to Elastica:

{
    "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.*": {}
    }
  }

I'm using FosElasticaBundle and I have this query without the highlight:

    $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);
unadivadantan
  • 363
  • 4
  • 14
  • Here's the solution I used that I described on Github: https://github.com/ruflin/Elastica/issues/1154#issuecomment-236843954 – unadivadantan Aug 02 '16 at 14:19

0 Answers0