步骤 2 : 先运行,看到效果,再学习 步骤 3 : 模仿和排错 步骤 4 : TestElasticSearch4J
当前查询建立在 JavaAPI-批量操作 之后得到的数据的基础之上
老规矩,先下载右上角的可运行项目,配置运行起来,确认可用之后,再学习做了哪些步骤以达到这样的效果。
运行 TestElasticSearch4J 看到如图所示的效果
在确保可运行项目能够正确无误地运行之后,再严格照着教程的步骤,对代码模仿一遍。
模仿过程难免代码有出入,导致无法得到期望的运行结果,此时此刻通过比较正确答案 ( 可运行项目 ) 和自己的代码,来定位问题所在。 采用这种方式,学习有效果,排错有效率,可以较为明显地提升学习速度,跨过学习路上的各个槛。 推荐使用diffmerge软件,进行文件夹比较。把你自己做的项目文件夹,和我的可运行项目文件夹进行比较。 这个软件很牛逼的,可以知道文件夹里哪两个文件不对,并且很明显地标记出来 这里提供了绿色安装和使用教程:diffmerge 下载和使用教程
根据关键字进行分页查询
package com.how2java;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.http.HttpHost;
import org.elasticsearch.ElasticsearchStatusException;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.admin.indices.open.OpenIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.ScoreSortBuilder;
import org.elasticsearch.search.sort.SortOrder;
public class TestElasticSearch4J {
private static RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("localhost", 9200, "http")
));
private static String indexName = "how2java";
public static void main(String[] args) throws IOException {
String keyword = "时尚连衣裙";
int start = 0;
int count = 10;
SearchHits hits = search(keyword, start, count);
SearchHit[] searchHits = hits.getHits();
for (SearchHit hit : searchHits) {
System.out.println(hit.getSourceAsString());
}
client.close();
}
private static SearchHits search(String keyword, int start, int count) throws IOException {
SearchRequest searchRequest = new SearchRequest(indexName);
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//关键字匹配
MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("name",keyword );
//模糊匹配
matchQueryBuilder.fuzziness(Fuzziness.AUTO);
sourceBuilder.query(matchQueryBuilder);
//第几页
sourceBuilder.from(start);
//第几条
sourceBuilder.size(count);
searchRequest.source(sourceBuilder);
//匹配度从高到低
sourceBuilder.sort(new ScoreSortBuilder().order(SortOrder.DESC));
SearchResponse searchResponse = client.search(searchRequest);
SearchHits hits = searchResponse.getHits();
return hits;
}
private static void batchInsert(List<Product> products) throws IOException {
// TODO Auto-generated method stub
BulkRequest request = new BulkRequest();
for (Product product : products) {
Map<String,Object> m = product.toMap();
IndexRequest indexRequest= new IndexRequest(indexName, "product", String.valueOf(product.getId())).source(m);
request.add(indexRequest);
}
client.bulk(request);
System.out.println("批量插入完成");
}
private static void deleteDocument(int id) throws IOException {
DeleteRequest deleteRequest = new DeleteRequest (indexName,"product", String.valueOf(id));
client.delete(deleteRequest);
System.out.println("已经从ElasticSearch服务器上删除id="+id+"的文档");
}
private static void updateDocument(Product product) throws IOException {
UpdateRequest updateRequest = new UpdateRequest (indexName, "product", String.valueOf(product.getId()))
.doc("name",product.getName());
client.update(updateRequest);
System.out.println("已经在ElasticSearch服务器修改产品为:"+product);
}
private static void getDocument(int id) throws IOException {
// TODO Auto-generated method stub
GetRequest request = new GetRequest(
indexName,
"product",
String.valueOf(id));
GetResponse response = client.get(request);
if(!response.isExists()){
System.out.println("检查到服务器上 "+"id="+id+ "的文档不存在");
}
else{
String source = response.getSourceAsString();
System.out.print("获取到服务器上 "+"id="+id+ "的文档内容是:");
System.out.println(source);
}
}
private static void addDocument(Product product) throws IOException {
Map<String, Object> jsonMap = new HashMap<>();
jsonMap.put("name", product.getName());
IndexRequest indexRequest = new IndexRequest(indexName, "product", String.valueOf(product.getId()))
.source(jsonMap);
client.index(indexRequest);
System.out.println("已经向ElasticSearch服务器增加产品:"+product);
}
private static boolean checkExistIndex(String indexName) throws IOException {
boolean result =true;
try {
OpenIndexRequest openIndexRequest = new OpenIndexRequest(indexName);
client.indices().open(openIndexRequest).isAcknowledged();
} catch (ElasticsearchStatusException ex) {
String m = "Elasticsearch exception [type=index_not_found_exception, reason=no such index]";
if (m.equals(ex.getMessage())) {
result = false;
}
}
if(result)
System.out.println("索引:" +indexName + " 是存在的");
else
System.out.println("索引:" +indexName + " 不存在");
return result;
}
private static void deleteIndex(String indexName) throws IOException {
DeleteIndexRequest request = new DeleteIndexRequest(indexName);
client.indices().delete(request);
System.out.println("删除了索引:"+indexName);
}
private static void createIndex(String indexName) throws IOException {
// TODO Auto-generated method stub
CreateIndexRequest request = new CreateIndexRequest(indexName);
client.indices().create(request);
System.out.println("创建了索引:"+indexName);
}
}
HOW2J公众号,关注后实时获知最新的教程和优惠活动,谢谢。
问答区域
2020-02-11
站长为啥我的检索出来的结果是没有进行分词的?
2019-08-28
怎么进行关联查询
2018-07-11
这里的RestClient是单例吧。。。
2018-07-10
有高亮吗 老大
2018-07-04
MultiMatchQueryBuilder //多字段查询字段
提问之前请登陆
提问已经提交成功,正在审核。 请于 我的提问 处查看提问记录,谢谢
|