简体   繁体   中英

multi match elastic search in multi word field

I have for example following documents in elastic search:

{ make: 'honda',
  model: 'civic 5-door'
  year: '2011'
}

{ make: 'honda',
  model: 'CIVIC TOURER 5-DOOR '
  year: '2011'
}

{ make: 'honda',
  model: 'JAZZ 5-DOOR'
  year: '2012'
}

{ make: 'mazda',
  model: 'some multi word name'
  year: '2012'
}

I want to make a single search bar which could search across all the three fields. I tried the following query:

$query = 'some query string'

        $results = $search->search([
            'index' => 'cars',
            'body' => [
                "query" =>[
                    'multi_match' => [
                        "query" => $query,
                        "type" => "cross_fields",
                        "analyzer"=> "standard",
                        "fields" => ['model', 'make', 'year']                        ]
                ]
            ]
        ]);

when i use search query like 'honda 2011', the query returns the result (it matches the query terms in 'make', and 'year'), but if i just type 'civic' (see the multi word field 'model'), it does not show any result. Why is the search into multi work field not working? Am i missing something? Please help.

My index mapping looks like the following:

$params = ['index' => 'cars'];

        $params['body'] = [
            'mappings' => [
                'properties' => [
                    'body_type' => [
                        'type' => 'keyword'
                    ],
                    'fuel_type' => [
                        'type' => 'keyword'
                    ],
                    'engine_size' => [
                        'type' => 'short'
                    ],
                    'make' => [
                        'type' => 'keyword'
                    ],
                    'model' => [
                        'type' => 'keyword'
                    ],
                    'dealership' => [
                        'type' => 'keyword'
                    ],
                    'transmission' => [
                        'type' => 'keyword'
                    ],
                    'colour' => [
                        'type' => 'keyword',
                    ],
                    'location' => [
                        'type' => 'geo_point'
                    ]
                ]
            ]
        ];

In order for multi_match to work you need to change your model field to text instead of keyword .

With a keyword field you can only run exact matches because keyword field values are not analyzed.

Try changing to

                'model' => [
                    'type' => 'text'
                ],

And then reindex your data and your query will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM