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);
1

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.