ElasticSearch(5.5.2)在java中的使用

ElasticSearch版本:5.5.2 

jdk版本:1.8+

Elasticsearch基本内容:

    集群(Cluster): ES是一个分布式的搜索引擎,一般由多台物理机组成。这些物理机,通过配置一个相同的cluster name,互相发现,把自己组织成一个集群。

    节点(Node):同一个集群中的一个Elasticearch主机。

  Node类型:

    1)data node: 存储index数据。

    2)client node: 不存储index,处理客户端请求。

    3)master node: 不存储index,集群管理,如管理路由信息(routing infomation),判断node是否available,当有node出现或消失时重定位分片(shards),当有node failure时协调恢复。(所有的master node会选举出一个master leader node)

    主分片(Primary shard):索引(下文介绍)的一个物理子集。同一个索引在物理上可以切多个分片,分布到不同的节点上。分片的实现是Lucene 中的索引。

    注意:ES中一个索引的分片个数是建立索引时就要指定的,建立后不可再改变。所以开始建一个索引时,就要预计数据规模,将分片的个数分配在一个合理的范围。

    副本分片(Replica shard):每个主分片可以有一个或者多个副本,个数是用户自己配置的。ES会尽量将同一索引的不同分片分布到不同的节点上,提高容错性。对一个索引,只要不是所有shards所在的机器都挂了,就还能用。

    索引(Index):逻辑概念,一个可检索的文档对象的集合。类似与DB中的database概念。同一个集群中可建立多个索引。比如,生产环境常见的一种方法,对每个月产生的数据建索引,以保证单个索引的量级可控。

    类型(Type):索引的下一级概念,大概相当于数据库中的table。同一个索引里可以包含多个 Type。 

文档(Document):即搜索引擎中的文档概念,也是ES中一个可以被检索的基本单位,相当于数据库中的row,一条记录。

    字段(Field):相当于数据库中的column。ES中,每个文档,其实是以json形式存储的。而一个文档可以被视为多个字段的集合。比如一篇文章,可能包括了主题、摘要、正文、作者、时间等信息,每个信息都是一个字段,最后被整合成一个json串,落地到磁盘。

    映射(Mapping):相当于数据库中的schema,用来约束字段的类型,不过 Elasticsearch 的 mapping 可以不显示地指定、自动根据文档数据创建。

    Elasticsearch集群可以包含多个索引(indices),每一个索引可以包含多个类型(types),每一个类型包含多个文档(documents),然后每个文档包含多个字段(Fields)

1.在idea中创建maven项目,导入maven依赖

	<dependencies>
		<dependency>
			<groupId>org.elasticsearch</groupId>
			<artifactId>elasticsearch</artifactId>
			<version>5.5.2</version>
		</dependency>
		<dependency>
			<groupId>org.elasticsearch.client</groupId>
			<artifactId>transport</artifactId>
			<version>5.5.2</version>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-core</artifactId>
			<version>2.7</version>
		</dependency>
		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-api</artifactId>
			<version>2.7</version>
		</dependency>
		<dependency>
			<groupId>com.google.code.gson</groupId>
			<artifactId>gson</artifactId>
			<version>2.8.0</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.data</groupId>
			<artifactId>spring-data-elasticsearch</artifactId>
		</dependency>
	</dependencies>
			<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<configuration>
					<skip>true</skip>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<compilerVersion>1.8</compilerVersion>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>
		</plugins>
	</build>

2.1 在src目录下的test目录下新建ElasticsearchTest类

2.2 先定义基本常量,方便后面的使用。

    public final static String HOST = "127.0.0.1"; //本地服务器部署

    public final static int PORT = 9300; //http请求的端口是9200,客户端是9300

    private TransportClient client = null; 客户端连接

2.3 添加@Before注解,指定测试程序开始前使用的client

 @SuppressWarnings({ "resource", "unchecked" })
    @Before
    public void getConnect() throws UnknownHostException {
        client = new PreBuiltTransportClient(Settings.EMPTY).addTransportAddresses(
                new InetSocketTransportAddress(InetAddress.getByName(HOST),PORT));
        System.out.println("连接信息:" + client.toString());
    }

2.4 添加@After注解,测试程序运行后关闭client连接

 @After
    public void closeConnect() {
        if(null != client) {
            System.out.println("执行关闭连接操作...");
            client.close();
        }
    }

2.5 创建索引(XContentBuilder)

 @Test
    public void addIndex() throws IOException {
        System.out.println("测试中");
        /*
         *建立文档对象
         * 参数一blog1:表示索引对象
         * 参数二article:类型
         * 参数三1:建立id
         */
            XContentBuilder builder = XContentFactory.jsonBuilder()
                    .startObject()
                    .field("id", 1)
                    .field("title", "elasticSearch搜索引擎")
                    .field("content",
                            "全球搜索服务器")
                    .endObject();
       
            IndexResponse indexResponse = client.prepareIndex("blog1", "article",Integer.toString(1)).setSource(builder).get();
        }
         // 结果获取
        String index = indexResponse.getIndex()
        String type = indexResponse.getType();
        String id = indexResponse.getId();
        long version = indexResponse.getVersion();
        RestStatus status = indexResponse.status();
        System.out.println("index索引:"+index + "--类型:" + type + "--索引id:" + id + "--版本:" + version + "--状态:" + status);
    }

2.5.2 创建索引(json)

 @Test
    public void addIndex() throws IOException {
        System.out.println("测试中");
		String json = "{" +
               "\"name\":\"深入浅出Node.js\"," +
               "\"author\":\"朴灵 \"," +
               "\"pubinfo\":\"人民邮电出版社 \"," +
               "\"pubtime\":\"2013-12-1 \"," +
               "\"desc\":\"本书从不同的视角介绍了 Node 内在的特点和结构。由首章Node 介绍为索引,涉及Node 的各个方面,主要内容包含模块机制的揭示、异步I/O 实现原理的展现、异步编程的探讨、内存控制的介绍、二进制数据Buffer 的细节、Node 中的网络编程基础、Node 中的Web 开发、进程间的消息传递、Node 测试以及通过Node 构建产品需要的注意事项。最后的附录介绍了Node 的安装、调试、编码规范和NPM 仓库等事宜。本书适合想深入了解 Node 的人员阅读。\"" +
               "}";
       
            IndexResponse indexResponse =  client.prepareIndex("blog2","goods").setSource(json).get();
        
         // 结果获取
        String index = indexResponse.getIndex()
        String type = indexResponse.getType();
        String id = indexResponse.getId();
        long version = indexResponse.getVersion();
        RestStatus status = indexResponse.status();
        System.out.println("index索引:"+index + "--类型:" + type + "--索引id:" + id + "--版本:" + version + "--状态:" + status);
    }

2.5.3 创建索引(map)

 @Test
    public void addIndex() throws IOException {
        System.out.println("测试中");
		
		Map<String, Object>  map = new HashMap<String, Object>();
		map.put("name","Go Web编程");
        map.put("author","谢孟军 ");
		map.put("pubinfo","电子工业出版社");
        map.put("pubtime","2013-6");
		map.put("desc","《Go Web编程》介绍如何使用Go语言编写Web,包含了Go语言的入门、Web相关的一些知识、Go中如何处理Web的各方面设计(表单、session、cookie等)、数据库以及如何编写GoWeb应用等相关知识。通过《Go Web编程》的学习能够让读者了解Go的运行机制,如何用Go编写Web应用,以及Go的应用程序的部署和维护等,让读者对整个的Go的开发了如指掌。");
        
        IndexResponse indexResponse = client.prepareIndex("blog2", "goods").setSource(map).execute().actionGet();
        
         // 结果获取
        String index = indexResponse.getIndex()
        String type = indexResponse.getType();
        String id = indexResponse.getId();
        long version = indexResponse.getVersion();
        RestStatus status = indexResponse.status();
        System.out.println("index索引:"+index + "--类型:" + type + "--索引id:" + id + "--版本:" + version + "--状态:" + status);
    }

2.6 删除索引

@Test
    public void deleteByObject() throws Exception {
        client.delete(new DeleteRequest("blog1", "article", Integer.toString(1)).get();
    }
2.7 获取文档信息
@Test
    public void getIndexNoMapping() throws Exception {
        GetResponse actionGet = client.prepareGet("blog1", "article", "1").execute().actionGet();
        System.out.println(actionGet.getSourceAsString());
    }

2.8 查询所有文档信息

@Test
    public void getMatchAll() throws IOException {

        // get() === execute().actionGet()
        SearchResponse searchResponse = client.prepareSearch("blog1")
                .setTypes("article").setQuery(QueryBuilders.matchAllQuery())
                .get();
        // 获取命中次数,查询结果有多少对象
        SearchHits hits = searchResponse.getHits();
        System.out.println("查询结果有:" + hits.getTotalHits() + "条");
        Iterator<SearchHit> iterator = hits.iterator();
        while (iterator.hasNext()) {
            // 每个查询对象
            SearchHit searchHit = iterator.next();
            System.out.println(searchHit.getSourceAsString()); // 获取字符串格式打印
            System.out.println("title:" + searchHit.getSource().get("title"));
        }
    }

2.9 关键字查询

@Test
    public void getKeyWord() throws IOException {
        long time1 = System.currentTimeMillis();
        SearchResponse searchResponse = client.prepareSearch("blog1")
                .setTypes("article").setQuery(QueryBuilders.queryStringQuery("你们"))
                .get();
        // 获取命中次数,查询结果有多少对象
        SearchHits hits = searchResponse.getHits();
        System.out.println("查询结果有:" + hits.getTotalHits() + "条");
        Iterator<SearchHit> iterator = hits.iterator();
        while (iterator.hasNext()) {
            // 每个查询对象
            SearchHit searchHit = iterator.next();
            System.out.println(searchHit.getSourceAsString()); // 获取字符串格式打印
            System.out.println("title:" + searchHit.getSource().get("title"));
        }
        long time2 = System.currentTimeMillis();
        System.out.println("花费"+(time2-time1)+"毫秒");
    }

2.10 通配符,词条查询

“*”表示0到 多个字符,“?”表示一个字符

 @Test
    public void getByLike() throws IOException {
        long time1 = System.currentTimeMillis();
        SearchResponse searchResponse = TransportTemplate.getClient().prepareSearch("blog1")
                .setTypes("article").setQuery(QueryBuilders.wildcardQuery("desc","可爱*")) //通配符查询
//                .setTypes("article").setQuery(QueryBuilders.wildcardQuery("content","服务器"))
//                .setTypes("article").setQuery(QueryBuilders.termQuery("content","全文")) //词条查询
                //一般情况下只显示十条数据
                //from + size must be less than or equal to: [10000]
                //Scroll Search->支持1万以上的数据量
                // .setSize(10000)
                .get();
        // 获取命中次数,查询结果有多少对象
        SearchHits hits = searchResponse.getHits();
        System.out.println("查询结果有:" + hits.getTotalHits() + "条");
        Iterator<SearchHit> iterator = hits.iterator();
        while (iterator.hasNext()) {
            // 每个查询对象
            SearchHit searchHit = iterator.next();
            System.out.println(searchHit.getSourceAsString()); // 获取字符串格式打印
            System.out.println("title:" + searchHit.getSource().get("title"));
        }
        long time2 = System.currentTimeMillis();
        System.out.println("花费"+(time2-time1)+"毫秒");
    }

2.11 组合查询

下面的.must()方法这个位置,一般常用的有.must(), .should(), mustNot()这三种方法,表示交集,并集,不一定。

@Test
    public void combinationQuery() throws Exception{
        SearchResponse searchResponse = client.prepareSearch("blog1").setTypes("article")
                .setQuery(QueryBuilders.boolQuery().must(QueryBuilders.termQuery("title", "搜索"))// 词条查询
                        //.must(QueryBuilders.rangeQuery("id").from(1).to(5))  // 范围查询
                        //因为IK分词器,在存储的时候将英文都变成了小写
                        .must(QueryBuilders.wildcardQuery("content", "Rest*".toLowerCase())) // 模糊查询
                        .must(QueryBuilders.queryStringQuery( "服电风扇丰盛的分器")) // 关键字(含有)
                )
                .get();
        SearchHits hits = searchResponse.getHits();
        System.out.println("总记录数:"+hits.getTotalHits());
        Iterator<SearchHit> iterator = hits.iterator();
        while(iterator.hasNext()){
            SearchHit searchHit = iterator.next();
            System.out.println(searchHit.getSourceAsString());
            Map<String, Object> source = searchHit.getSource();
//            System.out.println(source.get("id")+"    "+source.get("title")+"    "+source.get("content"));
        }

    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值