Elasticsearch索引不存在时,查询接口报错怎么办?

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)
 

完美解决!!!!

更多细节参考官方文档:
es官方文档
spring-data-elasticsearch官方文档
参考说明

 

posted @ 2024-01-16 18:12  下午喝什么茶  阅读(797)  评论(0编辑  收藏  举报