一.es 后期修改分片数量
在Elasticsearch中一旦索引创建后,分片的数量就不能直接更改。如果需要更改分片的数量,你需要按照以下步骤操作:
-
创建一个新的索引,并指定所需的分片数量。
-
将旧索引的数据复制到新索引中。
-
关闭旧索引,并将新索引设置为激活状态。
-
<dependencies><!-- 添加Elasticsearch客户端依赖 --><dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-high-level-client</artifactId><version>7.10.2</version> <!-- 请根据需要替换为合适的版本号 --></dependency><!-- 其他依赖... --> </dependencies>
-
import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.client.indices.CreateIndexRequest; import org.elasticsearch.client.indices.CreateIndexResponse; import org.elasticsearch.client.indices.GetIndexRequest; import org.elasticsearch.client.indices.UpdateAliasesRequest; import org.elasticsearch.client.indices.UpdateIndexSettingsRequest; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentType;public class ChangeShardsExample {public static void main(String[] args) throws IOException {// 初始化Elasticsearch客户端try (RestHighLevelClient client = new RestHighLevelClient(...)) {// 创建新索引String newIndex = "new_index";CreateIndexRequest createIndexRequest = new CreateIndexRequest(newIndex);createIndexRequest.settings(Settings.builder().put("index.number_of_shards", 5) // 设置新的分片数量.put("index.number_of_replicas", 1));CreateIndexResponse createIndexResponse = client.indices().create(createIndexRequest, RequestOptions.DEFAULT);if (createIndexResponse.isAcknowledged()) {System.out.println("新索引创建成功");// 复制旧索引数据到新索引// ...// 关闭旧索引String oldIndex = "old_index";client.indices().close(new CloseIndexRequest(oldIndex), RequestOptions.DEFAULT);// 更新别名,使新索引可用AliasActions aliasAction = new AliasActions(AliasActions.Type.ADD).index(newIndex).alias(oldIndex);UpdateAliasesRequest aliasRequest = new UpdateAliasesRequest().actions(aliasAction);client.indices().updateAliases(aliasRequest, RequestOptions.DEFAULT);System.out.println("旧索引已关闭,新索引设置为可用");}}} }
es的分片数量的计算规则是最好保证每个分片的的数据在20G左右,根据实际情况测试
二。 es 查询 and or 等
es是使用DSL语句进行查询的,java提供了相关api,生成最终的DSL语句,下面代码中的boolQueryBuilder对象中就有最终的DSL语句。
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.search.SearchHit;// 假设你已经有了一个RestHighLevelClient实例client
RestHighLevelClient client;// 创建一个BoolQueryBuilder来构建AND和OR查询
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();// 添加AND查询条件
boolQueryBuilder.must(QueryBuilders.matchQuery("field1", "value1"));
boolQueryBuilder.must(QueryBuilders.matchQuery("field2", "value2"));// 添加OR查询条件
boolQueryBuilder.should(QueryBuilders.matchQuery("field3", "value3"));
boolQueryBuilder.should(QueryBuilders.matchQuery("field4", "value4"));// 设置查询的索引
String index = "your_index";// 创建一个搜索请求
SearchRequest searchRequest = new SearchRequest(index);// 创建搜索源构建器
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(boolQueryBuilder);// 设置搜索源
searchRequest.source(searchSourceBuilder);try {// 执行搜索SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);// 处理搜索结果for (SearchHit hit : searchResponse.getHits().getHits()) {System.out.println(hit.getSourceAsString());}
} catch (IOException e) {e.printStackTrace();
}
三. es 根据字段排序
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.search.sort.SortOrder;
import org.elasticsearch.search.sort.FieldSortBuilder;import java.io.IOException;public class ElasticsearchSortExample {public static void main(String[] args) throws IOException {// 初始化Elasticsearch客户端(这里需要根据实际情况进行配置)RestHighLevelClient client = new RestHighLevelClient(...);// 创建搜索请求并设置索引名SearchRequest searchRequest = new SearchRequest("your_index");// 构建搜索源构建器SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();// 设置查询条件(这里以匹配所有为例)searchSourceBuilder.query(QueryBuilders.matchAllQuery());// 添加排序searchSourceBuilder.sort(new FieldSortBuilder("your_sort_field").order(SortOrder.ASC)); // 根据字段升序排序// 将搜索源构建器设置到搜索请求中searchRequest.source(searchSourceBuilder);// 执行搜索SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);// 处理搜索结果(这里仅打印命中数量)System.out.println("Total hits: " + searchResponse.getHits().getTotalHits().value);// 关闭客户端client.close();}
}
四.es评分修改(评分是搜索的结果排序依据是es中一个重要的点)
在Elasticsearch中,评分是一个复杂的过程,它涉及到文档与查询的匹配程度。评分是基于文档与查询中的某个字段相关性来计算的。在Elasticsearch中,有多种方法来影响评分过程,例如:
-
使用不同的相关性模型,如TF-IDF,BM25等。
-
使用函数查询,调整字段的权重。
-
使用索引时的设置,如分析器,提高某些字段的重要性。
评分的计算过程如下:
-
计算查询与文档的匹配程度。
-
计算每个字段的评分。
-
将字段的评分累加到文档级别的评分中。
-
对所有文档进行排序,评分高的排在前面。
以下是一个简单的Java代码示例
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.metrics.AvgAggregationBuilder;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.metrics.AvgAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.ValueCountAggregationBuilder;
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorBuilders;
import org.elasticsearch.search.aggregations.pipeline.BucketSelectorPipelineAggregationBuilder;// 假设你已经有了一个RestHighLevelClient实例clientSearchRequest searchRequest = new SearchRequest("index_name"); // 替换为你的索引名
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchAllQuery());// 添加一个计分函数,比如使用一个辅导聚合
searchSourceBuilder.functionScore().scoreMode("sum") // 可以是 "sum", "multiply", "min", "max", "avg".setMinScore(1); // 设置最小分数,如果所有函数分数加起来低于这个值,则文档将被丢弃// 你可以添加多个计分函数,比如使用一个平均聚合
AvgAggregationBuilder avgAggregation = AggregationBuilders.avg("avg_price").field("price");
searchSourceBuilder.functionScore().add(new FunctionScoreQueryBuilder.FilterFunctionBuilder(new QueryBuilders.matchAllQuery()).scoreMode("multiply").setFilter(QueryBuilders.matchQuery("type", "electronics")).setWeight(1.5))).add(new FunctionScoreQueryBuilder.FilterFunctionBuilder().scoreFunction(new FunctionScoreQueryBuilder.WeightBuilder().setWeight(2.0)).setFilter(QueryBuilders.matchQuery("type", "books"))));searchRequest.source(searchSourceBuilder);SearchResponse searchResponse = client.search(searchRequest);// 处理搜索响应// 注意:确保在完成操作后关闭client以释放资源
5.es使用自定义的评分
script_score 是es中用于自定义脚本计算评分,Java也提供了相关api,script_score可以接受参数用于计算,比方说20岁左右的人优先查询
//client 创建代码省略// 初始化你的Elasticsearch客户端
String indexName = "your_index"; // 你的索引名SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
ScriptScoreQueryBuilder scriptScoreQueryBuilder = QueryBuilders.scriptScoreQuery(QueryBuilders.matchAllQuery());
//Script 可以支持从外部传参,参数是一个map,如果需要从客户端传入参数可以这样使用
Script script = new Script("doc['age'].value - 20"); // 假设age是你存储年龄的字段
scriptScoreQueryBuilder.script(script);searchSourceBuilder.query(scriptScoreQueryBuilder);
searchSourceBuilder.sort("_score"); // 根据得分排序SearchRequest searchRequest = new SearchRequest(indexName);
searchRequest.source(searchSourceBuilder);SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);