4,2 java using es

1, import-dependent

<dependency>
     <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

  

 

2, Configuration

server:
  port: 9007
  
spring:
  application:
    name: tensquare-search # specified service name
  data:
    elasticsearch:
      cluster-nodes: 127.0.0.1:9300

  

 

3、entity

package com.bw.entity;

import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

@Document(indexName ="db",type="table" )
public class Article {
	@Id
	private String id;
	@Field(index = true,analyzer = "ik_max_word",searchAnalyzer = "ik_max_word",type = FieldType.Text)
	private String title;
	@Field(index = true,analyzer = "ik_max_word",searchAnalyzer = "ik_max_word",type = FieldType.Text)
	private String content;
	private String picture;
	private String status;
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public String getPicture() {
		return picture;
	}
	public void setPicture(String picture) {
		this.picture = picture;
	}
	public String getStatus() {
		return status;
	}
	public void setStatus(String status) {
		this.status = status;
	}
	public Article() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Article(String id, String title, String content, String picture, String status) {
		this.id = id;
		this.title = title;
		this.content = content;
		this.picture = picture;
		this.status = status;
	}
	@Override
	public String toString() {
		return "Article [id=" + id + ", title=" + title + ", content=" + content + ", picture=" + picture + ", status="
				+ status + "]";
	}
	
	

}

  

 

4、repository

package com.bw.repository;

import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

import com.bw.entity.Article;

public interface ArticleRepository extends ElasticsearchRepository<Article, String> {

}

  

5, test

package com.bw;

import java.util.Iterator;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import com.bw.entity.Article;
import com.bw.repository.ArticleRepository;

@RunWith(SpringRunner.class)
@SpringBootTest
public class TensquareSearchApplicationTests {
	@Resource private ArticleRepository articleRepository;

	@Test
	public void contextLoads() {
		/*
		 * Article article = new
		 * Article ( "22", "successful programmer", "These are well-known programmer on the one hand, on the other hand is also very successful in business, they affect the entire industry. They seem to determine the development of the industrial sector in direction",
		 * "11", "0");
		 */
		Article article = new Article ( "11", "immortal programmer", "your code is longer than the life you live", "11", "0");
		
		articleRepository.save(article);
	}
	@Test
	public void b() {
		
		  Article article = new Article ( "22", "successful programmer", "These are well-known programmer on the one hand, on the other hand is also very successful in business, they affect the entire industry. They seem determined industry direction "in development.
		  "11", "0");
		 

		
		articleRepository.save(article);
	}
	@Test
	public void list() {
		
		 

		
		Iterable<Article> iterable = articleRepository.findAll();
		Iterator<Article> iterator = iterable.iterator();
		while (iterator.hasNext()) {
			Article next = iterator.next();
			System.out.println(next);
		}
	}


}

  

 

Guess you like

Origin www.cnblogs.com/zwyzwy/p/11983320.html