Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
goujunyi committed May 14, 2018
0 parents commit a0ab5f7
Show file tree
Hide file tree
Showing 9 changed files with 346 additions and 0 deletions.
25 changes: 25 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Created by .ignore support plugin (hsz.mobi)
### Java template
# Compiled class file
*.class

# Log file
*.log

# BlueJ files
*.ctxt

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.ear
*.zip
*.tar.gz
*.rar

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

66 changes: 66 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.netease.junyi.springcloud</groupId>
<artifactId>elastic-search</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
</parent>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>

<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>

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

<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>5.5.0</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/org.elasticsearch/elasticsearch -->
<!-- <dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>6.1.3</version>
</dependency>-->
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
</dependencies>


<build>
<plugins>
<!-- Package as an executable jar/war -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>


</project>
28 changes: 28 additions & 0 deletions src/main/java/com/netease/demo/Application.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.netease.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;



/**
* Description:
*
* @author goujunyi
* Mail:[email protected]
* Date:2018-03-13 19:25
*/
@SpringBootApplication
public class Application {

/*@Bean
public ApplicationContextInit applicationContextInit() {
return new ApplicationContextInit();
}*/

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}


}
46 changes: 46 additions & 0 deletions src/main/java/com/netease/demo/conf/EsConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.netease.demo.conf;

import org.elasticsearch.client.Client;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Bean;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;

import java.net.InetAddress;

/**
* Description:
*
* @author goujunyi
* Mail:[email protected]
* Date:2018-03-13 17:32
*/

@Configurable
@EnableElasticsearchRepositories(basePackages = "com.netease.demo.repository")
public class EsConfig {
Logger logger = LoggerFactory.getLogger(EsConfig.class);

@Bean
public Client client() {
Client client = null;
try {
client = new PreBuiltTransportClient(Settings.EMPTY)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
} catch (Exception e) {
logger.error("ERR",e);
}
return client;
}

@Bean
public ElasticsearchOperations elasticsearchTemplate() {
return new ElasticsearchTemplate(client());
}
}
56 changes: 56 additions & 0 deletions src/main/java/com/netease/demo/es/bean/Home.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.netease.demo.es.bean;

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;
import org.springframework.data.elasticsearch.annotations.GeoPointField;
import org.springframework.data.elasticsearch.core.geo.GeoPoint;

/**
* Description:
*
* @author goujunyi
* Mail:[email protected]
* Date:2018-05-14 11:31
*/
@Document(indexName = Home.INDEX_NAME, type = Home.TYPE, shards = 2, replicas = 1)
public class Home {

public static final String INDEX_NAME = "productindex";

public static final String TYPE = "pro";

@Id
private String id;

public String getId() {
return this.id;
}

public void setId(String id) {
this.id = id;
}

@Field(type = FieldType.keyword)
private String title;

@GeoPointField
private GeoPoint location;

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public GeoPoint getLocation() {
return location;
}

public void setLocation(GeoPoint location) {
this.location = location;
}
}
13 changes: 13 additions & 0 deletions src/main/java/com/netease/demo/repository/HomeRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.netease.demo.repository;

import com.netease.demo.es.bean.Home;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

/**
* Description:
*
* @author Mail:[email protected]
* Date:2018-05-14 11:34
*/
public interface HomeRepository extends ElasticsearchRepository<Home,String> {
}
9 changes: 9 additions & 0 deletions src/main/resources/application-dev.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

#集群,有则使用,没有则创建
spring.data.elasticsearch.cluster-name = elasticsearch
#spring.data.elasticsearch.repositories.enabled = true
spring.data.elasticsearch.cluster-nodes = 127.0.0.1:9300
#spring.data.elasticsearch.elasticsearch.port = 9300
#spring.data.elasticsearch.properties.logs=D:\\elasticsearch-5.5.3\\springboot-elasticsearch\\data
#spring.data.elasticsearch.properties.data=D:\\elasticsearch-5.5.3\\springboot-elasticsearch\\log

2 changes: 2 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
spring.profiles.active=dev

101 changes: 101 additions & 0 deletions src/test/java/ProductSearchTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import com.netease.demo.Application;
import com.netease.demo.es.bean.Home;
import com.netease.demo.repository.HomeRepository;
import org.elasticsearch.common.unit.DistanceUnit;
import org.elasticsearch.index.query.GeoDistanceQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.geo.GeoPoint;

import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

/**
* Description:
*
* @author goujunyi
* Mail:[email protected]
* Date:2018-05-08 17:47
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class ProductSearchTest {

@Autowired
private HomeRepository homeRepository;

@Autowired
private ElasticsearchTemplate esTemplate;

/**
* init data;
*/
@Test
public void testHome() {

esTemplate.deleteIndex(Home.class);
esTemplate.createIndex(Home.class);
esTemplate.refresh(Home.class);
esTemplate.putMapping(Home.class);

Home home = new Home();
home.setId("1");
home.setTitle("home A");
home.setLocation(new GeoPoint(22.8334, 108.301));
homeRepository.save(home);
System.out.println("===============success--1======");

Home home2 = new Home();
home2.setId("2");
home2.setTitle("home B_sh");
home2.setLocation(new GeoPoint(31.318, 121.3840));
homeRepository.save(home2);
System.out.println("===============success--2======");

Home home3 = new Home();
home3.setId("3");
home3.setTitle("home C");
home3.setLocation(new GeoPoint(22.7879, 108.301));
homeRepository.save(home3);
System.out.println("===============success--1======");

}

@Test
public void getByHomeTitle() {
double lat = 108.31d;
double lon = 22.81d;

//Location 10KM
GeoDistanceQueryBuilder geoDistanceBuilder = QueryBuilders.geoDistanceQuery("location")
.point(lat, lon)
.distance(10, DistanceUnit.KILOMETERS);
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(QueryBuilders.boolQuery().filter(geoDistanceBuilder))
.withPageable(PageRequest.of(0, 10)).build();
List<Home> productInfoList = esTemplate.queryForList(searchQuery, Home.class);
for (Home hm : productInfoList) {
System.out.println(hm.getId() + "==" + hm.getTitle() + "===");
}
}

@Test
public void getNealHomeByTitle() {
QueryBuilder matchAllBuilder = QueryBuilders.matchAllQuery();
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllBuilder)
.withPageable(PageRequest.of(0, 10)).build();
List<Home> productInfoList = esTemplate.queryForList(searchQuery, Home.class);
for (Home hm : productInfoList) {
System.out.println(hm.getId() + "==" + hm.getTitle() + "===");
}
}

}

0 comments on commit a0ab5f7

Please sign in to comment.