-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
81 additions
and
33 deletions.
There are no files selected for viewing
31 changes: 5 additions & 26 deletions
31
src/main/java/com/gentaliti/elasticsearch/ElasticsearchApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,16 @@ | ||
package com.gentaliti.elasticsearch; | ||
|
||
import lombok.extern.log4j.Log4j2; | ||
import org.elasticsearch.client.Client; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.common.transport.TransportAddress; | ||
import org.elasticsearch.transport.client.PreBuiltTransportClient; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; | ||
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories; | ||
|
||
import java.net.InetAddress; | ||
import java.net.UnknownHostException; | ||
|
||
/** | ||
* Elasticsearch app | ||
* | ||
* @author Gent Aliti <[email protected]/> | ||
*/ | ||
@SpringBootApplication | ||
@Log4j2 | ||
@EnableElasticsearchRepositories | ||
public class ElasticsearchApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(ElasticsearchApplication.class, args); | ||
} | ||
|
||
@Bean | ||
public Client client() throws UnknownHostException { | ||
Settings settings = Settings.builder().put("cluster.name", "docker-cluster").put("network.host","127.0.0.1").build(); | ||
return new PreBuiltTransportClient(settings).addTransportAddress(new TransportAddress(InetAddress.getByName("localhost"), 9300)); | ||
} | ||
|
||
@Bean | ||
public ElasticsearchTemplate elasticsearchTemplate(Client client) throws Exception { | ||
return new ElasticsearchTemplate(client); | ||
} | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
src/main/java/com/gentaliti/elasticsearch/configuration/ElasticsearchConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.gentaliti.elasticsearch.configuration; | ||
|
||
import org.elasticsearch.client.Client; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.common.transport.TransportAddress; | ||
import org.elasticsearch.transport.client.PreBuiltTransportClient; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; | ||
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories; | ||
|
||
import java.net.InetAddress; | ||
import java.net.UnknownHostException; | ||
|
||
/** | ||
* Elasticsearch configuruation | ||
* | ||
* @author Gent Aliti <[email protected]/> | ||
*/ | ||
@Configuration | ||
@EnableElasticsearchRepositories | ||
public class ElasticsearchConfig { | ||
@Value("${elasticsearch.cluster-name}") | ||
private String clusterName; | ||
|
||
@Value("${elasticsearch.port}") | ||
private int port; | ||
|
||
@Value("${elasticsearch.host}") | ||
private String host; | ||
|
||
@Bean | ||
public Client client() throws UnknownHostException { | ||
Settings settings = Settings.builder().put("cluster.name", clusterName).put("network.host", host).build(); | ||
return new PreBuiltTransportClient(settings).addTransportAddress(new TransportAddress(InetAddress.getByName(host), port)); | ||
} | ||
|
||
@Bean | ||
public ElasticsearchTemplate elasticsearchTemplate(Client client) { | ||
return new ElasticsearchTemplate(client); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,11 @@ | |
import java.util.Objects; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Search controller | ||
* | ||
* @author Gent Aliti <[email protected]/> | ||
*/ | ||
@RestController("/accounts") | ||
public class SearchController { | ||
private AccountSearchService accountSearchService; | ||
|
@@ -25,10 +30,9 @@ public SearchController(AccountSearchService accountSearchService) { | |
} | ||
|
||
@GetMapping | ||
public Page searchObjects( | ||
@RequestParam(value = "q", required = false) String query, | ||
@RequestParam(value = "filter", required = false) String filter, | ||
@PageableDefault(value = 10, page = 0) Pageable pageable) { | ||
public Page searchObjects(@RequestParam(value = "q", required = false) String query, | ||
@RequestParam(value = "filter", required = false) String filter, | ||
@PageableDefault(value = 10, page = 0) Pageable pageable) { | ||
return this.accountSearchService.search(query, this.map(filter), pageable); | ||
} | ||
|
||
|
@@ -51,10 +55,10 @@ private List<Filter> map(String filters) { | |
|
||
private List<Filter> mapValues(String filter) { | ||
List<Filter> filters = new ArrayList<>(); | ||
// check if filter string is valid | ||
if (!filter.contains(":")) { | ||
return filters; | ||
} | ||
|
||
final String[] values = filter.split(":"); | ||
if (values[1].contains(",")) { | ||
for (String f : values[1].split(",")) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,11 @@ | |
import org.springframework.data.elasticsearch.annotations.FieldType; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* Account | ||
* | ||
* @author Gent Aliti <[email protected]/> | ||
*/ | ||
@Data | ||
@Component | ||
@Builder | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,11 @@ | |
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
/** | ||
* Filter | ||
* | ||
* @author Gent Aliti <[email protected]/> | ||
*/ | ||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,11 @@ | |
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
/** | ||
* Account repository | ||
* | ||
* @author Gent Aliti <[email protected]/> | ||
*/ | ||
@Repository | ||
public interface AccountRepository extends ElasticsearchRepository<Account, String> { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
package com.gentaliti.elasticsearch.service; | ||
|
||
import com.gentaliti.elasticsearch.repository.AccountRepository; | ||
import com.gentaliti.elasticsearch.dto.Filter; | ||
import com.gentaliti.elasticsearch.repository.AccountRepository; | ||
import org.elasticsearch.index.query.BoolQueryBuilder; | ||
import org.elasticsearch.index.query.MultiMatchQueryBuilder; | ||
import org.elasticsearch.index.query.QueryBuilders; | ||
|
@@ -14,6 +14,11 @@ | |
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Account search service | ||
* | ||
* @author Gent Aliti <[email protected]/> | ||
*/ | ||
@Service | ||
public class AccountSearchService { | ||
private AccountRepository accountRepository; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
|
||
elasticsearch.cluster-name=docker-cluster | ||
elasticsearch.host=localhost | ||
elasticsearch.port=9300 |