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

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?

Welcome!

Could you provide a full recreation script as described in About the Elasticsearch category. 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.

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

Why do you put a star?

Can this work?

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

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.

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

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