Search code examples
symfonyelasticsearchfoselasticabundle

ElasticSearch filter by wildcard


I'm trying to filter my results by wildcard character.

example of my records:

....
"_source" : {
      "urlSlug" : "entry-title",
      "revisions" : [
        {
          "title" : "Entry title",
          "context" : "NKD"
        }
      ]
 }

each revision can have different context with different order.

And when I search for record I want to search only entities with context like "N". So I perform nested query with match_all and wildcard.

{"query":{"bool":{"must":[{"query_string":{"query":"*entry*"}},{"nested":{"path":"revisions","query":{"bool":{"should":[{"match_all":{}}],"filter":[{"wildcard":{"revisions.context":{"value":"*N*","boost":1}}}]}}}}]}},"size":10}

When I run query I get zero results. And Can't figure out how to restrict results.

I'm using for this FosElastica with following config:

indexes:
        app:
            types:
              entity:
                properties:
                    urlSlug: ~
                    revisions:
                      type: "nested"
                      properties:
                        title: { type: text,boost: 10 }
                        context: { type: text }

and my query builder looks like this:

$boolQuery = new ESQuery\BoolQuery();

    $fieldQuery = new ESQuery\QueryString();
    $fieldQuery->setQuery('*' . $query . '*');
    $boolQuery->addMust($fieldQuery);

        $nestedQuery = new ESQuery\Nested();
        $nestedQuery->setPath('revisions');

            $nestedBoolQuery = new ESQuery\BoolQuery();
                $matchAllQuery = new ESQuery\MatchAll();
            $nestedBoolQuery->addShould($matchAllQuery);

                $filterQuery = new ESQuery\Wildcard();
                $filterQuery->setValue('revisions.context','*N*');
            $nestedBoolQuery->addFilter($filterQuery);

        $nestedQuery->setQuery($nestedBoolQuery);


    $boolQuery->addMust($nestedQuery);

$result = $finder->findHybrid($boolQuery,self::AUTOCOMPLETE_MAX_RESULTS);

ElasticSearch version 5.2.2


Solution

  • Well I did find out what is the problem in my query.

    this is working one:

    {"query":{"bool":{"must":[{"query_string":{"query":"*komi*"}}],"filter":[{"nested":{"path":"revisions","query":{"wildcard":{"revisions.context":{"value":"*n*","boost":1}}}}}]}},"size":10}
    

    whole problem was uppercase wildcard search. I was looking for *N* and have zero results and with *n* it fine.