Highlighting is not working in complex query


(NM) #1

Hi,
I wanted to use highlight in my query,

My Query is

$query = [
    "filtered" => [
        "query" => [
            "bool" => [
                "should" => [
                    [
                        'query_string' => [
                            'fields' => [
                                'Title.title^4',
                                'Title.ngrams_front^2',
                                'Title.ngrams_back'
                            ],
                            'defaultOperator' => 'or',
                            'query' => $paramsObj->q
                        ]
                    ],
                    [
                        'query_string' => [
                            'auto_generate_phrase_queries' => 0,
                            'enable_position_increments' => false,
                            'fields' => [
                                'Title.title',
                                'Address',
                                'keys'
                            ],
                            'query' => $paramsObj->q,
                            'use_dis_max' => false,
                            'boost' => 2
                        ]
                    ],
                    [
                        'fuzzy' => [
                            'Title.title' => [
                                'value' => $paramsObj->q,
                                'boost' => 1,
                                'min_similarity' => 0.5,
                                'max_expansions' => 20,
                                'prefix_length' => 0
                            ]
                        ]
                    ]
                ]   
            ]
        ],

        "filter" => $filters
    ]	
];

I have tried nested query inside query because i knew highlight will come on same level as query, but still its not working,
can anyone help me stating where the highlight part should go in my query?


(Zachary Tong) #2

The highlighter goes as another "top level" parameter, next to query:

$request = [
  'query' => [
    'filtered' => [
      ...
    ]
  ],
  'highlight' => [
      'fields' => [
        'my_higlighted_field' => (object) []
    ]
  ]
];

(system) #3