Elasticsearch multi match

I have index with dynamic mapping

'mappings' => [
	'index_type' => [
		'dynamic_templates' => [
			[
				'property_name' => [
				   'match_mapping_type' => '*',
				   'match' => 'name_property_*',
				   'mapping' => [
					'type' => 'text',
					'index'	=> TRUE,
				   ],
                                ],
			],
		],
	],
],

Now i queried like this

[
	'size' => 1,
	'from' => 0,
	'_source' => [],
	'query' => [
		'multi_match' => [
		'query' => 'query',
		'type' => 'best_fields',
		'fields' => ['name_property_*'],
	],
],

and query successfully executed like this

 ['_index'] => 'index_name'
 ['_type'] => 'index_type'
 ['_id'] => 1325
 ['_score'] => 0.28
 ['_source'] => Array
 (
     ['name_property_1'] => 'country',
     ['name_property_2'] => 'query',
     ['name_property_3'] => 'state'
 )

Now my question is how to return only fields that match the actual query in this example how to achieve output like this

['_index'] => 'index_name'
['_type'] => 'index_type'
['_id'] => 1325
['_score'] => 0.28
['_source'] => Array
(
    ['name_property_2'] => 'query',
)

only name_property_2 because this field only match with the query i do not want to return whole document.

1 Like

Maybe try to highlight on the fields. I'm not sure, but I think the highlighter will only return the matched. You can combine that with "_source": false and just get the highlighted fields

1 Like

yes thanks @aclowkey highlighter is worked. :grinning:

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.