Search code examples
reactjselasticsearchsearchkickelastic-search-ui

Failed to create query: Can only use phrase prefix queries on text fields - not on [customer_name] which is of type [keyword]


While using ElastichSearch Search UI, with this conifg:

const configurationOptions = {
  apiConnector: connector,
  alwaysSearchOnInitialLoad: true,
  searchQuery: {
    search_fields: {
      customer_name: {
      }
    },
    result_fields: {
      customer_name: {
        snippet: {}
      },
     
    },
  },
};

This is the connector:

const connector = new ElasticsearchAPIConnector({
  host: "http://localhost:9200",
  index: "test_development"
});

it runs as expected without query, but as soon as a value get written to SearchBox, I'm getting the following error:

error: {root_cause: [{type: "query_shard_exception",…}], type: "search_phase_execution_exception",…}
failed_shards: [{shard: 0, index: "test_development_20220708164434410", node: "IvfYKvrQRtCnYM3vMBTIAw",…}]
grouped: true
phase: "query"
reason: "all shards failed"
root_cause: [{type: "query_shard_exception",…}]
0: {type: "query_shard_exception",…}
index: "test_development_20220708164434410"
index_uuid: "6ma7I3JbTl2l5CLkIfDLqw"
reason: "failed to create query: Can only use phrase prefix queries on text fields - not on [customer_name] which is of type [keyword]"
type: "query_shard_exception"
type: "search_phase_execution_exception"
status: 400

I've attached my text_index:

{
  "test_development_20220721163305175": {
    "mappings": {
      "dynamic_templates": [
        {
          "string_template": {
            "match": "*",
            "match_mapping_type": "string",
            "mapping": {
              "fields": {
                "analyzed": {
                  "analyzer": "searchkick_index",
                  "index": true,
                  "type": "text"
                }
              },
              "ignore_above": 30000,
              "type": "keyword"
            }
          }
        }
      ],
      "properties": {
        "customer_name": {
          "type": "keyword",
          "ignore_above": 30000,
          "fields": {
            "analyzed": {
              "type": "text",
              "analyzer": "searchkick_index"
            }
          }
        }
      }
    }
  }
}
}

What am I doing wrong please? Is this a problem with SearchUI, my configuration or something else? While querying the index via SearchAPI I get proper responses.


Solution

  • You need to use field name as customer_name.text for text type as default type of field is keyword as per your index mapping.

    const configurationOptions = {
      apiConnector: connector,
      alwaysSearchOnInitialLoad: true,
      searchQuery: {
        search_fields: {
          "customer_name.text": {}
        },
        result_fields: {
          customer_name: {
            snippet: {}
          },
         
        },
      },
    };