elasticsearch-php icon indicating copy to clipboard operation
elasticsearch-php copied to clipboard

Wildcard support in queries?

Open MarkGavalda opened this issue 12 years ago • 8 comments

Final edit: ah okay you have you to use $query['wildcard'] to be able to use the wildcard character in queries. It should be documented somewhere :)

MarkGavalda avatar Oct 16 '14 16:10 MarkGavalda

Okay I'm stuck with this. First of all any whitespace in the "wildcard" query breaks the whole thing.

Second, I believe this should work, but it only gives back results for the second wildcard:

            $params['body'] = array(
                'query' => array(
                    'filtered' => array(
                        'filter' => array(
                            'range' => array(
                                '@timestamp' => array(
                                    'gt' => 'now-1h'
                                )
                            )
                        ),
                        'query' => array(
                            'bool' => array(
                                'should' => array(
                                    'wildcard' => array(
                                        'file' => '*xyz1*'
                                    ),
                                    'wildcard' => array(
                                        'file' => '*xyz2*'
                                    )
                                )
                            )
                        )
                    )
                )
            );

MarkGavalda avatar Oct 16 '14 22:10 MarkGavalda

This can be related to mapping.

Can you add the mapping of the file field to this issue ?

mguillermin avatar Oct 17 '14 07:10 mguillermin

Sure thing! Thanks for the quick answer.

"file":{
   "type":"string",
   "norms":{
      "enabled":false
   },
   "fields":{
      "raw":{
         "type":"string",
         "index":"not_analyzed",
         "ignore_above":256
      }
   }
}

MarkGavalda avatar Oct 17 '14 12:10 MarkGavalda

This doesn't seem to be a problem with the PHP library but with the query.

Your mapping is using a multi-field type. The standard file field is tokenized but the field.raw version is not. To resolve your problem of whitespace in your query, you should use the wildcard search with the not-analyzed version of your field : file.raw instead of just file.

...
'query' => array(
    'bool' => array(
        'should' => array(
            'wildcard' => array(
                'file.raw' => '*xyz1*'
            ),
            'wildcard' => array(
                'file.raw' => '*xyz2*'
            )
        )
    )
)
...

For the other problem, I don't see anything wrong. Give another try after having switched to file.raw.

mguillermin avatar Oct 20 '14 05:10 mguillermin

@MarkGavalda Did you get this sorted out? Need any more help?

polyfractal avatar Nov 24 '14 23:11 polyfractal

Was also looking how the wildcard works.

From quick start

<?php
$searchParams['index'] = 'my_index';
$searchParams['type']  = 'my_type';
$searchParams['body']['query']['match']['testField'] = 'abc';
$retDoc = $client->search($searchParams);

Is it $searchParams['body']['query']['wildcard'] = true; ?

harikt avatar Dec 19 '14 06:12 harikt

@harikt No, to use wildcards you have to use a query that supports them (the match query does not support wildcards). There is no "wildcard" parameter that is enabled.

Instead, you would use the wildcard query:

$searchParams['body']['query']['wildcard']['testField'] = 'abc*';
$results = $client->search($searchParams);

Or you could also use the query_string query:

$searchParams['body']['query']['query_string']['query'] = 'testField:abc*';
$results = $client->search($searchParams);

polyfractal avatar Dec 19 '14 18:12 polyfractal

Thank you for your help @polyfractal .

Good if it is documented some where, so it will not be asked again by someone.

harikt avatar Dec 20 '14 01:12 harikt