# Can only use prefix queries on keyword and text fields - not on \[soCode\] which is of type

**URL:** https://discuss.elastic.co/t/can-only-use-prefix-queries-on-keyword-and-text-fields-not-on-socode-which-is-of-type/225358
**Category:** Elasticsearch
**Created:** [March 27, 2020, 8:15am UTC](https://discuss.elastic.co/t/can-only-use-prefix-queries-on-keyword-and-text-fields-not-on-socode-which-is-of-type/225358 "2020-03-27T08:15:11Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Oh\_Chee\_Peng](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/oh_chee_peng/32/65171_2.png) [@Oh\_Chee\_Peng](https://discuss.elastic.co/u/Oh_Chee_Peng)
#### Post date: [March 27, 2020, 8:15am UTC](https://discuss.elastic.co/t/can-only-use-prefix-queries-on-keyword-and-text-fields-not-on-socode-which-is-of-type/225358/1 "2020-03-27T08:15:11Z")

</div>

I have this scenario, where the keyword is string, ex: customerName, which is string; as well as soCode, which is number.

First, I added a record into elasticsearch:

```
POST sales-orders/_doc/1234
{
    customerName: "hello world", //<---- this is string
    soCode: 5678 //<---- this is long
}

```

Then, I executed this:

```
GET sales-orders/_search
{
    simple_query_string: {
           query: 'hello*',
           fields: ['customerName']  
    },
}

```

It successfully return hits. Then I execute the following:

```
GET sales-orders/_search
{
    simple_query_string: {
           query: 'hello*',
           fields: ['customerName', 'soCode'] // <--- added soCode field.
    },
}

```

When query, I got this error:  
'Can only use prefix queries on keyword and text fields - not on [soCode] which is of type long'

I need the search to be able to return result, if the keyword is 'hello', or '5678'.

How to make this works?

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [March 27, 2020, 8:53am UTC](https://discuss.elastic.co/t/can-only-use-prefix-queries-on-keyword-and-text-fields-not-on-socode-which-is-of-type/225358/2 "2020-03-27T08:53:48Z")

</div>

Welcome!

Could you provide a full recreation script as described in [About the Elasticsearch category](https://discuss.elastic.co/t/about-the-elasticsearch-category/21). It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

A full reproduction script will help readers to understand, reproduce and if needed fix your problem. It will also most likely help to get a faster answer.

---

<div class="post-metadata">

### Author: ![Oh\_Chee\_Peng](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/oh_chee_peng/32/65171_2.png) [@Oh\_Chee\_Peng](https://discuss.elastic.co/u/Oh_Chee_Peng)
#### Post date: [March 27, 2020, 12:54pm UTC](https://discuss.elastic.co/t/can-only-use-prefix-queries-on-keyword-and-text-fields-not-on-socode-which-is-of-type/225358/3 "2020-03-27T12:54:28Z")

</div>

Hi, I had formatted the questions. Sorry as this is my first post, missed the notice.

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [March 27, 2020, 1:08pm UTC](https://discuss.elastic.co/t/can-only-use-prefix-queries-on-keyword-and-text-fields-not-on-socode-which-is-of-type/225358/4 "2020-03-27T13:08:17Z")

</div>

Why do you put a star?

Can this work?

```
GET sales-orders/_search
{
    simple_query_string: {
           query: 'hello',
           fields: ['customerName', 'soCode']
    }
}
```

---

<div class="post-metadata">

### Author: ![Oh\_Chee\_Peng](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/oh_chee_peng/32/65171_2.png) [@Oh\_Chee\_Peng](https://discuss.elastic.co/u/Oh_Chee_Peng)
#### Post date: [March 27, 2020, 11:38pm UTC](https://discuss.elastic.co/t/can-only-use-prefix-queries-on-keyword-and-text-fields-not-on-socode-which-is-of-type/225358/5 "2020-03-27T23:38:43Z")

</div>

Hi, it is not working.

I got this error:

```
   {
    "error": {
        "root_cause": [
            {
                "type": "query_shard_exception",
                "reason": "failed to create query: {\n \"simple_query_string\" : {\n \"query\" : \"hello\",\n \"fields\" : [\n \"soCode^1.0\",\n \"customerName^1.0\"\n],\n \"flags\" : -1,\n \"default_operator\" : \"or\",\n \"analyze_wildcard\" : false,\n \"auto_generate_synonyms_phrase_query\" : true,\n \"fuzzy_prefix_length\" : 0,\n \"fuzzy_max_expansions\" : 50,\n \"fuzzy_transpositions\" : true,\n \"boost\" : 1.0\n }\n}",
                "index_uuid": "nMhJWiQ0TaWkvftOV03zFg",
                "index": "sales-orders"
            }
        ],
        "type": "search_phase_execution_exception",
        "reason": "all shards failed",
        "phase": "query",
        "grouped": true,
        "failed_shards": [
            {
                "shard": 0,
                "index": "sales-orders",
                "node": "WU6IGN4tTwmP3eXgggBieg",
                "reason": {
                    "type": "query_shard_exception",
                    "reason": "failed to create query: {\n \"simple_query_string\" : {\n \"query\" : \"hello\",\n \"fields\" : [\n \"soCode^1.0\",\n \"customerName^1.0\"\n],\n \"flags\" : -1,\n \"default_operator\" : \"or\",\n \"analyze_wildcard\" : false,\n \"auto_generate_synonyms_phrase_query\" : true,\n \"fuzzy_prefix_length\" : 0,\n \"fuzzy_max_expansions\" : 50,\n \"fuzzy_transpositions\" : true,\n \"boost\" : 1.0\n }\n}",
                    "index_uuid": "nMhJWiQ0TaWkvftOV03zFg",
                    "index": "sales-orders",
                    "caused_by": {
                        "type": "number_format_exception",
                        "reason": "For input string: \"hello\""
                    }
                }
            }
        ]
    },
    "status": 400
}

```

I suspect the issue is because of field of different type. customerName is string, while soCode is long.

If my search keyword is number, like '4009', then no error; if it is 'hello', the above error shown.

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [April 22, 2020, 9:41am UTC](https://discuss.elastic.co/t/can-only-use-prefix-queries-on-keyword-and-text-fields-not-on-socode-which-is-of-type/225358/6 "2020-04-22T09:41:52Z")

</div>

Have a look at the `lenient` option in the documentation: [https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-simple-query-string-query.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-simple-query-string-query.html)

---

<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: [May 20, 2020, 9:41am UTC](https://discuss.elastic.co/t/can-only-use-prefix-queries-on-keyword-and-text-fields-not-on-socode-which-is-of-type/225358/7 "2020-05-20T09:41:54Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
