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   数据派  阅读(15)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示