Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch one regexp for multiple fields

I'm new to Elasticsearch. I need to write query to find regexp match in one of the fields.

If I'm looking for regexp in one field everything works fine (PHP code):

$data = ['body' => ['query' => ['regexp' => ['abstract' => ".*searchtext.*"]]]];

what to do if I want to find documents in which at least one field satisfies regexp?

This query:

$data = [
  'body' => [
    'query' => [
      'multi_match' => [
        'query' => 'searchtext',
        'fields' => [
          'type',
          'title',
          'abstract',
          'body_text'
        ]
      ]
    ]
  ]
];

only finds documents with whole word "searchtext" match.

Regards, Tomas

like image 454
Tom1410 Avatar asked Sep 01 '25 02:09

Tom1410


1 Answers

Found answer:

$data = [
  'body' => [
    'query' => [
      'bool' => [
        'should' => [
          ['regexp' => ['type' => ".*searchtext.*"]],
          ['regexp' => ['title' => ".*searchtext.*"]],
          ['regexp' => ['abstract' => ".*searchtext.*"]],
          ['regexp' => ['body_text' => ".*searchtext.*"]],
        ]
      ]
    ]
  ]
];

not so elegant as multi_match query, but works.

like image 83
Tom1410 Avatar answered Sep 02 '25 17:09

Tom1410