Home > Software design >  elasticsearch search string with slash
elasticsearch search string with slash

Time:04-06

I try to search for a string that contains a slash (in field firstname) but elasticsearch shows me an error "Failed to parse query ...".

Here is my mapping

[
        'index' => 'indexA',
        'body' => [
            'settings' => [
                'number_of_shards' => 1,
                'number_of_replicas' => 1,
                "analysis" => [
                    "analyzer" => [
                        "analyzer_asciifolding" => [
                            "tokenizer" => "standard",
                            "filter" => [ "lowercase", "my_ascii_folding" ]
                        ]
                    ],
                    "filter" => [
                        "my_ascii_folding" => [
                            "type" => "asciifolding",
                            "preserve_original" => true
                        ]
                    ]
                ]
            ],
            'mappings' => [
                'dynamic' => false,
                'properties' => [
                    ...,
                    'persons' => [
                        'type' => 'nested',
                        'properties' => [
                            ...,
                            'firstname' => [
                                'type' => 'text',
                                'analyzer' => 'analyzer_asciifolding'
                            ]
                        ]
                    ]
                ]
            ]
        ]
    ];

Can you help me please?

UPDATED 2022-04-01 15:10

here is the php code which query elasticsearch

    $response = $this->elasticsearchService->search([
        'index' => 'indexA',
        'body' => [
            'query' => [
                'bool' => [
                    'filter' => [
                        [
                            'query_string' => [
                                'query' => $attributes['person_firstname'],
                                'fields' => ['persons.firstname']
                            ]
                        ]
                    ],
                ]
            ],
            'from' => $from,
            'size' => $size
        ]
    ]);

CodePudding user response:

You should escape them with a leading backslash.

The reserved characters are: - = && || > < ! ( ) { } [ ] ^ " ~ * ? : \ /

{
  "query": {
    "query_string": {
      "fields": ["persons.firstname"], 
      "query": "use\\/share"
    }
  }
}
  • Related