自学内容网 自学内容网

Spring Data Elasticsearch使用事项

增删改查


1.大数据量根据条件批量删除(一次删不干净)

/索引名称/_delete_by_query?slices=3&wait_for_completion=false&scroll_size=9000&conflicts=proceed

2.嵌套结构Nested构造查询条件(注意es版本)

type = FieldType.Nested

//1.假设索引结构为{
    "a1": {
        "b1": {
            "c1": "",
            "c2": ""
        },
        "b2": {}
    },
    "a2": {}
}
//2.需要a1.b1.c1='aa'的数据
//3.nestedQuery("a1",nestedQuery("a1.b1",QueryBuilders.boolQuery().must(QueryBuilders.termsQuery("a1.b1.c1", "aa")))))
boolQuery.must(QueryBuilders.nestedQuery(ESFieldName.BASE_TRANSACTION,
                    QueryBuilders.nestedQuery(ESFieldName.QUERY_TRANSACTION,
                            QueryBuilders.boolQuery()
                                    .must(QueryBuilders.termsQuery(ESFieldName.QUERY_TRANSACTION_SOURCE_ADDRESS, req.getAddress()))
                            ,org.apache.lucene.search.join.ScoreMode.None)
                    ,org.apache.lucene.search.join.ScoreMode.None));

3.查询无限制

添加track_total_hits:true

/索引名/_search/

{
    "track_total_hits": true,
    "query": {
        "bool": {
            "must": [
                {
                    "term": {
                        "aa": "1"
                    }
                },
                {
                    "range": {
                        "bb": {
                            "gte": "0",
                            "lte": "1"
                        }
                    }
                }
            ]
        }
    }
}

在查询中,如果.withAggregations注意agg分组结果的组的数量也是限制1w,记得打开限制进行统计

NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
                    .withQuery(boolQuery).withTrackTotalHits(true)
                    .withAggregations(AggregationBuilders.terms(ApiStatisticsESField.ITEM_ID_GRP).field(ApiStatisticsESField.ITEM_ID).size(Integer.MAX_VALUE)
                            .subAggregation(AggregationBuilders.count(ApiStatisticsESField.TOTAL_COUNT).field(ApiStatisticsESField.ITEM_ID))
                            .order(BucketOrder.aggregation(ApiStatisticsESField.TOTAL_COUNT, true))
                    )
                    .build();

4.模糊搜索

type = FieldType.Wildcard

boolQuery.must(QueryBuilders.wildcardQuery("ids", "*" + 某变量 + "*"));


原文地址:https://blog.csdn.net/xeswzb/article/details/135847534

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!