# Query string query doesn't work when field has customer analyzer

**URL:** <https://discuss.elastic.co/t/query-string-query-doesnt-work-when-field-has-customer-analyzer/26320>\
**Category:** Elasticsearch\
**Created:** [July 27, 2015, 5:14am UTC](https://discuss.elastic.co/t/query-string-query-doesnt-work-when-field-has-customer-analyzer/26320 "2015-07-27T05:14:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![alexyee](https://avatars.discourse-cdn.com/v4/letter/a/6de8d8/32.png) [@alexyee](https://discuss.elastic.co/u/alexyee)\
**Post date:** [July 27, 2015, 5:14am UTC](https://discuss.elastic.co/t/query-string-query-doesnt-work-when-field-has-customer-analyzer/26320/1 "2015-07-27T05:14:36Z")

</div>

My intention is to apply a custom analyzer to this example path, then to search for this document based on keyword "bar" using Elasticsearch "query string query":

```auto

```

The mapping file "test\_map.txt":

```auto
{
    {
    "settings" : {
	"analysis" : {
            "char_filter" : {
                "replace_slash_with_space" : {
                    "type" : "pattern_replace",
                    "pattern" : "\\\\",
                    "replacement" : " "
                }
            },
            "filter" : {
                "custom_path_filter" : {
                    "type" : "pattern_capture",
                    "preserve_original" : true,
                    "patterns" : [
                        "(([a-z|A-Z]+)_([a-z|A-Z]+))"
                    ]
                }
            },
	    "analyzer" : {
                "custom_path_analyzer" : {
                    "type" : "custom",
                    "char_filter" : ["replace_slash_with_space"],
                    "tokenizer" : "whitespace",
                    "filter" : ["custom_path_filter"]
                }
	    }
	}
    },
    "mappings" : {
	"_default_" : {
	    "properties" : {
		"Path" : {"type" : "string", "analyzer" : "custom_path_analyzer"}
	    }
	}
    }
}
```

The data file "test.txt":

```auto
{"index": {"_type": "pathtype", "_id": "P1", "_index": "path_index"}}
{"Path": "C:\\example\\foo_bar\\test"}
```

Indexing data:

```auto
curl -XPOST http://localhost:9200/path_index -d @.\test_map.txt
curl -s -XPOST http://localhost:9200/_bulk --data-binary @test.txt
```

Tokens generated by using the custom\_path\_analyzer (note that foo\_bar, foo and bar are tokens):

```auto
curl.exe -XGET http://localhost:9200/path_index/_analyze?analyzer=custom_path_analyzer -d 'C:\example\foo_bar\test'

{
    "tokens": [
        {
            "end_offset": 3,
            "position": 1,
            "start_offset": 0,
            "token": "'c:",
            "type": "word"
        },
        {
            "end_offset": 11,
            "position": 2,
            "start_offset": 4,
            "token": "example",
            "type": "word"
        },
        {
            "end_offset": 19,
            "position": 3,
            "start_offset": 12,
            "token": "foo_bar",
            "type": "word"
        },
        {
            "end_offset": 19,
            "position": 3,
            "start_offset": 12,
            "token": "foo",
            "type": "word"
        },
        {
            "end_offset": 19,
            "position": 3,
            "start_offset": 12,
            "token": "bar",
            "type": "word"
        },
        {
            "end_offset": 25,
            "position": 4,
            "start_offset": 20,
            "token": "test'",
            "type": "word"
        }
    ]
}
```

Failed Queries:

```auto
curl.exe -XGET http://localhost:9200/path_index/_search?q=bar
curl.exe -XGET http://localhost:9200/path_index/_search?q=foo
```

Successful Queries:

```auto
curl.exe -XGET http://localhost:9200/path_index/_search?q=Path:bar
curl.exe -XGET http://localhost:9200/path_index/_search?q=Path:foo
curl.exe -XGET http://localhost:9200/path_index/_search?q=foo_bar
```

  
Why didn't [code]curl.exe -XGET http://localhost:9200/path\_index/\_search?q=bar[/code] returned any hits?

---

<div class="post-metadata">

**Author:** ![Joshua\_Rich](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/joshua_rich/32/44953_2.png) [@Joshua\_Rich](https://discuss.elastic.co/u/Joshua_Rich)\
**Post date:** [July 27, 2015, 6:39am UTC](https://discuss.elastic.co/t/query-string-query-doesnt-work-when-field-has-customer-analyzer/26320/2 "2015-07-27T06:39:44Z")

</div>

I believe this is because by default the `query_string` query (which is invoked when you do a URI search) searches on the `_all` field by default, which won't have your custom analyzer applied to it. Where you explicitly search against the _Path_ field (i.e. `q=Path:bar`) work correctly, for all other searches all bets are off.

---

<div class="post-metadata">

**Author:** ![alexyee](https://avatars.discourse-cdn.com/v4/letter/a/6de8d8/32.png) [@alexyee](https://discuss.elastic.co/u/alexyee)\
**Post date:** [July 27, 2015, 6:58am UTC](https://discuss.elastic.co/t/query-string-query-doesnt-work-when-field-has-customer-analyzer/26320/3 "2015-07-27T06:58:47Z")

</div>

Hi Joshua\_Rich,

Thanks for your quick reply. Appreciated.

So which search method (e.g. simple query, multi fields etc) should I use to achieve the following?:

1. User provides one or more keywords
2. Then, search all available fields and return result when a match is found. The keywords must be analysed by the analyser defined for the respective field in the mappings file.

Cheers  
Alex

---

<div class="post-metadata">

**Author:** ![Joshua\_Rich](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/joshua_rich/32/44953_2.png) [@Joshua\_Rich](https://discuss.elastic.co/u/Joshua_Rich)\
**Post date:** [July 27, 2015, 7:17am UTC](https://discuss.elastic.co/t/query-string-query-doesnt-work-when-field-has-customer-analyzer/26320/4 "2015-07-27T07:17:17Z")

</div>

You can probably either:

- Explicitly specify the fields to search on.
- Set the analyzer on the `_all` field to the same as the `Path` field.
- Use [copy\_to](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-core-types.html#copy-to) to create a meta-field like `_all` that contains the tokens from all fields that use the same analyzer, then set this as the default field for searches like `query_string`, etc.

---

<div class="post-metadata">

**Author:** ![alexyee](https://avatars.discourse-cdn.com/v4/letter/a/6de8d8/32.png) [@alexyee](https://discuss.elastic.co/u/alexyee)\
**Post date:** [July 27, 2015, 7:48am UTC](https://discuss.elastic.co/t/query-string-query-doesnt-work-when-field-has-customer-analyzer/26320/5 "2015-07-27T07:48:15Z")

</div>

Hi Joshua,

I have thought about the 1st and 2nd options, while the 3rd seems interesting. I think the best would be the first option with multi-fields.

Thanks again.  
Alex

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 5, 2017, 11:58pm UTC](https://discuss.elastic.co/t/query-string-query-doesnt-work-when-field-has-customer-analyzer/26320/6 "2017-07-05T23:58:52Z")

</div>


