4

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

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

Comments

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.