Elasticsearch查询索引数据时报错no such index

1、索引不存在,报错:type=index_not_found_exception, reason=no such index
解决办法:
DSL:
GET /text_index_003/_search?ignore_unavailable=true
java 代码:
 NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withIndicesOptions(IndicesOptions.LENIENT_EXPAND_OPEN)
                .withQuery(xxxQuery)
                .withAggregations(xxxAgg)
                .build();

2、排序字段mapping不存在报错:"reason":"No mapping found for [xxx] in order to sort on"

解决办法:

DSL:
GET /text_005/_search?ignore_unavailable=true
{
  "sort": [
    {
      "price": {
        "order": "desc",
        "unmapped_type": "long"
      }
    }
  ],
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "xxx": {
              "gte": 10,
              "lte": 20
            }
          }
        },
        {"term": {
          "yyy": "ddd"
        }}
      ]
    }
  }
}
Java:
new FieldSortBuilder("xxx").unmappedType("long").order(SortOrder.DESC);
SortBuilders.fieldSort("xxx").unmappedType("long").order(SortOrder.DESC)

 

方法入参说明:

ignore_unavailable :是否忽略不可用的索引
allow_no_indices:是否允许索引不存在
expandToOpenIndices :通配符表达式将扩展为打开的索引
expandToClosedIndices :通配符表达式将扩展为关闭的索引

附加知识点:

新增数据时出现index_not_found_exception ,表明该index不存在。出现的原因:

es未开启自动创建索引功能
或者不想启动es自动创建索引,但又没手动创建索引

解决方法1:开启es自动创建索引
手动修改/etc/elasticsearch/elasticsearch.yml文件

#添加权限(默认为true)
action.auto_create_index: true

或者在kibana中执行命令

PUT _cluster/settings
{
    "persistent": {
        "action.auto_create_index": "true"
    }
}

解决方法2:不开启es自动创建索引

这个需要用户自己选择创建index的时机,例如

定时性创建index
触发性创建index

posted on 2025-02-28 09:51  数据派  阅读(336)  评论(0)    收藏  举报