大数据-Elasticsearch的API操作

Elasticsearch的API操作

(1)连接Elasticsearch客户端

public void getClient() throws UnknownHostException {
    //设置连接的集群名称
    Settings setting = Settings.builder().put("cluster.name", CLUSTER_NAME).build();
    //连接集群
    client = new PreBuiltTransportClient(setting);
    client.addTransportAddresses(new TransportAddress(new InetSocketAddress(InetAddress.getByName("192.168.138.130"),9300)));
}

(2)创建索引(表)

public void getClient() throws UnknownHostException {
    //设置连接的集群名称
    Settings setting = Settings.builder().put("cluster.name", CLUSTER_NAME).build();
    //连接集群
    client = new PreBuiltTransportClient(setting);
    client.addTransportAddresses(new TransportAddress(new InetSocketAddress(InetAddress.getByName("192.168.138.130"),9300)));
}

(3)删除索引(表)

public void deleteIndex(){
    //删除索引
    client.admin().indices().prepareDelete("blog1").get();
}

(4)创建文档(行)

//使用Json创建Document
public void createDocByJson(){
    //准备文档数据
    String json = "{" + "\"id\":\"1\"," + "\"title\":\"基于Lucene的搜索服务器\","
            + "\"content\":\"它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口\"" + "}";
    //创建文档
    IndexResponse indexResponse = client.prepareIndex("destiny", "freedom", "2").setSource(json,XContentType.JSON).execute().actionGet();
    //打印结果
    System.out.println("index:"+indexResponse.getIndex());
    System.out.println("type:"+indexResponse.getType());
    System.out.println("id:"+indexResponse.getId());
    System.out.println("version:"+indexResponse.getVersion());
    System.out.println("result:"+indexResponse.getResult());
}

//使用Map创建Document
public void createDocByMap(){
    //准备文档数据
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("id","1");
    map.put("title","基于Lucene的搜索服务器");
    map.put("content","它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口");
    //创建文档
    IndexResponse indexResponse = client.prepareIndex("destiny", "freedom","1").setSource(map).execute().actionGet();
    //打印结果
    System.out.println("index:"+indexResponse.getIndex());
    System.out.println("type:"+indexResponse.getType());
    System.out.println("id:"+indexResponse.getId());
    System.out.println("version:"+indexResponse.getVersion());
    System.out.println("result:"+indexResponse.getResult());
}

//使用XContentBuilder类创建Document
public void createDocByXContentBuilder() throws IOException {
    //准备文档数据
    XContentBuilder builder = XContentFactory.jsonBuilder().startObject().field("id", "1").field("title", "基于Lucene的搜索服务器").field("content", "它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口").endObject();
    IndexResponse indexResponse = client.prepareIndex("destiny", "freedom","3").setSource(builder).execute().actionGet();
    //打印结果
    System.out.println("index:"+indexResponse.getIndex());
    System.out.println("type:"+indexResponse.getType());
    System.out.println("id:"+indexResponse.getId());
    System.out.println("version:"+indexResponse.getVersion());
    System.out.println("result:"+indexResponse.getResult());
}

(5)查询文档(行)

//查询文档
public void getDoc(){
    GetResponse response = client.prepareGet("destiny", "freedom", "1").get();
    //打印搜索的结果
    System.out.println(response.getSourceAsString());
}

//查询多个文档
public void getMultiDoc(){
    MultiGetResponse multiGetItemResponses = client.prepareMultiGet().add("destiny", "freedom", "1").add("destiny", "freedom", "2").add("destiny", "freedom", "3").get();
    //遍历打印搜索的结果
    for (MultiGetItemResponse response : multiGetItemResponses){
        System.out.println(response.getResponse());
    }
}

(6)更新文档(行)

public void updateDoc() throws IOException, ExecutionException, InterruptedException {
    //创建更新数据的请求对象
    UpdateRequest updateRequest = new UpdateRequest();
    updateRequest.index("destiny");
    updateRequest.type("freedom");
    updateRequest.id("1");

    updateRequest.doc(XContentFactory.jsonBuilder().startObject().field("title","基于Lucene的搜索服务器").field("content","它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口").field("createDate","2019-04-08").endObject());

    //获取更新后的值
    UpdateResponse updateResponse = client.update(updateRequest).get();
    //打印结果
    System.out.println("index:"+updateResponse.getIndex());
    System.out.println("type:"+updateResponse.getType());
    System.out.println("id:"+updateResponse.getId());
    System.out.println("version:"+updateResponse.getVersion());
    System.out.println("result:"+updateResponse.getResult());
}

(7)添加和更新文档(行)

public void upSert() throws IOException, ExecutionException, InterruptedException {
    //设置查询条件,查询不到则添加
    IndexRequest indexRequest = new IndexRequest("destiny", "freedom", "4").source(XContentFactory.jsonBuilder().startObject().field("title", "搜索服务器").field("content", "它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticserch是用Java开发").endObject());
    //设置更新,查询到更新
    UpdateRequest upsert = new UpdateRequest("destiny", "freedom", "4").doc(XContentFactory.jsonBuilder().startObject().field("id","1").field("user", "freedom").endObject()).upsert(indexRequest);

    UpdateResponse updateResponse = client.update(upsert).get();
    //打印结果
    System.out.println("index:"+updateResponse.getIndex());
    System.out.println("type:"+updateResponse.getType());
    System.out.println("id:"+updateResponse.getId());
    System.out.println("version:"+updateResponse.getVersion());
    System.out.println("result:"+updateResponse.getResult());
}

(8)全部查询

public void matchAllQuery(){
    //执行查询
    SearchResponse searchResponse = client.prepareSearch("destiny").setTypes("freedom").setQuery(QueryBuilders.matchAllQuery()).get();
    //打印查询结果
    SearchHits searchHits = searchResponse.getHits();
    System.out.println("查询结果有"+searchHits.getTotalHits()+"条");
    for (SearchHit hit:searchHits){
        System.out.println(hit.getSourceAsString());
    }
}

(9)条件查询

public void query(){
    //条件查询
    SearchResponse searchResponse = client.prepareSearch("destiny").setTypes("freedom").setQuery(QueryBuilders.queryStringQuery("Elasticserch")).get();
    //打印查询结果
    SearchHits searchHits = searchResponse.getHits();
    System.out.println("查询的结果有"+searchHits.getTotalHits()+"条");
    SearchHit[] hits1 = searchHits.getHits();
    for (SearchHit hit: hits1){
        System.out.println(hit.getSourceAsString());
    }
}

(10)通配符查询

public void wildcardQuery(){
    //通配符查询
    SearchResponse searchResponse = client.prepareSearch("destiny").setTypes("freedom").setQuery(QueryBuilders.wildcardQuery("content", "*全*")).get();
    //打印查询的结果
    SearchHits searchHits = searchResponse.getHits();
    System.out.println("查询的结果有"+searchHits.getTotalHits()+"条");
    Iterator<SearchHit> iterator = searchHits.iterator();
    while (iterator.hasNext()){
        SearchHit hit = iterator.next();
        System.out.println(hit.getSourceAsString());
    }
}

(11)字段查询

public void termQuery(){
    //字段查询
    SearchResponse searchResponse = client.prepareSearch("destiny").setTypes("freddom").setQuery(QueryBuilders.termQuery("title", "lucene")).get();
    //打印查询的结果
    SearchHits searchHits = searchResponse.getHits();
    System.out.println("查询的结果有"+searchHits.getTotalHits()+"条");
    for (SearchHit hit: searchHits){
        System.out.println(hit.getSourceAsString());
    }
}

(12)模糊查询

public void fuzzy(){
    //模糊查询
    SearchResponse searchResponse = client.prepareSearch("destiny").setTypes("freedom").setQuery(QueryBuilders.fuzzyQuery("title", "Lucene")).get();
    //打印查询的结果
    SearchHits hits = searchResponse.getHits();
    System.out.println("查询的结果有"+hits.getTotalHits()+"条");
    Iterator<SearchHit> iterator = hits.iterator();
    while(iterator.hasNext()){
        System.out.println(iterator.next().getSourceAsString());
    }
}

(13)创建Mapping

curl -H "Content-Type:application/json" -XGET 'http://192.168.138.130:9200/_analyze?pretty' -d '{"analyzer":"ik_smart","text":"五四青年节"}'
curl -H "Content-Type:application/json" -XGET 'http://192.168.138.130:9200/_analyze?pretty' -d '{"analyzer":"ik_max_word","text":"五四青年节"}'
public void createMapping() throws IOException, ExecutionException, InterruptedException {
    //设置Mapping
    XContentBuilder xContentBuilder = XContentFactory.jsonBuilder().startObject().startObject("freedom").startObject("properties").startObject("id").field("type", "text").field("store", "true").endObject()
            .startObject("title").field("type", "text").field("store", "true").endObject().startObject("content").field("type", "text").field("store", "true").endObject().endObject().endObject().endObject();
    //添加Mapping
    PutMappingRequest putMappingRequest = Requests.putMappingRequest("destiny").type("freedom").source(xContentBuilder);

    client.admin().indices().putMapping(putMappingRequest).get();
}

//分词Mapping
public void createMappingIk() throws IOException, ExecutionException, InterruptedException {
    //设置Mapping
    XContentBuilder builder = XContentFactory.jsonBuilder().startObject().startObject("freedom").startObject("properties").startObject("id").field("type", "text").field("store", "true").field("analyzer","ik_smart").endObject()
            .startObject("title").field("type", "text").field("store", "false").field("analyzer","ik_smart").endObject().startObject("content").field("type", "text").field("store", "true")
            .field("analyzer","ik_smart").endObject().endObject().endObject().endObject();
    //添加Mapping
    PutMappingRequest putMappingRequest = Requests.putMappingRequest("destiny").type("freedom").source(builder);

    client.admin().indices().putMapping(putMappingRequest).get();
}
发布了131 篇原创文章 · 获赞 12 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/JavaDestiny/article/details/91348838
今日推荐