Last active
August 24, 2016 08:50
custom-solr-repository-implementation
This file contains hidden or 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
solr.server.url=http://localhost:8983/solr/basic_core |
This file contains hidden or 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
package org.programmingmitra.customSolrRepo; | |
import org.programmingmitra.customSolrRepo.model.Employee; | |
import org.programmingmitra.customSolrRepo.repository.EmployeeRepository; | |
import org.springframework.boot.SpringApplication; | |
import org.springframework.boot.autoconfigure.SpringBootApplication; | |
import org.springframework.context.ApplicationContext; | |
import org.springframework.context.annotation.Import; | |
/** | |
* @author Naresh Joshi | |
* @since 30-Jan-2016 | |
*/ | |
@SpringBootApplication | |
@Import(SolrConfig.class) | |
public class CustomSolrRepoApp { | |
public static void main(String[] args) { | |
ApplicationContext context = SpringApplication.run(CustomSolrRepoApp.class, args); | |
EmployeeRepository repository = context.getBean(EmployeeRepository.class); | |
createSampleSchema(repository); | |
System.out.println("All records in solr"); | |
repository.findAll().forEach(System.out::println); | |
System.out.println("==========================="); | |
System.out.println("Unique lastnames in solr"); | |
repository.getLastNames().forEach(System.out::println); | |
} | |
private static void createSampleSchema(EmployeeRepository repository) { | |
Employee emp1 = new Employee("1", "Naresh", "Joshi"); | |
repository.save(emp1); | |
Employee emp2 = new Employee("2", "Mahesh", "Joshi"); | |
repository.save(emp2); | |
Employee emp3 = new Employee("3", "Dinesh", "Joshi"); | |
repository.save(emp3); | |
Employee emp4 = new Employee("4", "Rishi", "Mahawar"); | |
repository.save(emp4); | |
Employee emp5 = new Employee("5", "Yogesh", "Kapadne"); | |
repository.save(emp5); | |
Employee emp6 = new Employee("6", "Akash", "Pawar"); | |
repository.save(emp6); | |
Employee emp7 = new Employee("7", "Anil", "Chowdhary"); | |
repository.save(emp7); | |
Employee emp8 = new Employee("8", "Ishank", "Gupta"); | |
repository.save(emp8); | |
Employee emp9 = new Employee("9", "Harsh", "Chowdhary"); | |
repository.save(emp9); | |
} | |
} |
This file contains hidden or 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
package org.programmingmitra.customSolrRepo.repository; | |
import java.util.List; | |
/** | |
* @author Naresh Joshi | |
* @since 30-Jan-2016 | |
*/ | |
public interface CustomSolrRepository { | |
List<String> getLastNames(); | |
} |
This file contains hidden or 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
package org.programmingmitra.customSolrRepo.model; | |
import org.apache.solr.client.solrj.beans.Field; | |
import org.springframework.data.annotation.Id; | |
/** | |
* @author Naresh Joshi | |
* @since 30-Jan-2016 | |
*/ | |
public class Employee { | |
@Id | |
@Field | |
private String id; | |
@Field | |
private String firstName; | |
@Field | |
private String lastName; | |
public Employee() { | |
} | |
public Employee(String id, String firstName, String lastName) { | |
this.id = id; | |
this.firstName = firstName; | |
this.lastName = lastName; | |
} | |
public String getId() { | |
return id; | |
} | |
public void setId(String id) { | |
this.id = id; | |
} | |
public String getFirstName() { | |
return firstName; | |
} | |
public void setFirstName(String firstName) { | |
this.firstName = firstName; | |
} | |
public String getLastName() { | |
return lastName; | |
} | |
public void setLastName(String lastName) { | |
this.lastName = lastName; | |
} | |
@Override | |
public int hashCode() { | |
final int prime = 31; | |
int result = 1; | |
result = prime * result + ((firstName == null) ? 0 : firstName.hashCode()); | |
result = prime * result + ((id == null) ? 0 : id.hashCode()); | |
result = prime * result + ((lastName == null) ? 0 : lastName.hashCode()); | |
return result; | |
} | |
@Override | |
public boolean equals(Object obj) { | |
if (this == obj) | |
return true; | |
if (obj == null) | |
return false; | |
if (getClass() != obj.getClass()) | |
return false; | |
Employee other = (Employee) obj; | |
if (firstName == null) { | |
if (other.firstName != null) | |
return false; | |
} else if (!firstName.equals(other.firstName)) | |
return false; | |
if (id == null) { | |
if (other.id != null) | |
return false; | |
} else if (!id.equals(other.id)) | |
return false; | |
if (lastName == null) { | |
if (other.lastName != null) | |
return false; | |
} else if (!lastName.equals(other.lastName)) | |
return false; | |
return true; | |
} | |
@Override | |
public String toString() { | |
return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + "]"; | |
} | |
} |
This file contains hidden or 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
package org.programmingmitra.customSolrRepo.repository; | |
import org.programmingmitra.customSolrRepo.model.Employee; | |
import org.springframework.data.solr.repository.SolrCrudRepository; | |
/** | |
* @author Naresh Joshi | |
* @since 30-Jan-2016 | |
*/ | |
public interface EmployeeRepository extends CustomSolrRepository, SolrCrudRepository<Employee, String> { | |
} |
This file contains hidden or 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
/** | |
* | |
*/ | |
package org.programmingmitra.customSolrRepo.repository.impl; | |
import java.util.ArrayList; | |
import java.util.List; | |
import javax.annotation.Resource; | |
import org.apache.solr.client.solrj.SolrQuery; | |
import org.apache.solr.client.solrj.SolrServerException; | |
import org.apache.solr.client.solrj.response.GroupResponse; | |
import org.programmingmitra.customSolrRepo.repository.CustomSolrRepository; | |
import org.springframework.data.solr.core.SolrTemplate; | |
import org.springframework.stereotype.Repository; | |
/** | |
* @author Naresh Joshi | |
* @since 30-Jan-2016 | |
*/ | |
@Repository | |
public class EmployeeRepositoryImpl implements CustomSolrRepository { | |
@Resource | |
private SolrTemplate solrTemplate; | |
@Override | |
public List<String> getLastNames() { | |
List<String> lastNames = new ArrayList<String>(); | |
SolrQuery query = new SolrQuery("*:*"); | |
query.set("group", true); | |
query.set("group.field", "lastName"); | |
try { | |
GroupResponse response = solrTemplate.getSolrServer().query(query).getGroupResponse(); | |
response.getValues().forEach( | |
groupList -> groupList.getValues().forEach( | |
group -> group.getResult().forEach( | |
solrDoc -> lastNames.add(solrDoc.getFieldValue("lastName").toString())))); | |
} catch (SolrServerException e) { | |
e.printStackTrace(); | |
} | |
return lastNames; | |
} | |
} |
This file contains hidden or 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
<?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>org.programmingmitra</groupId> | |
<artifactId>CustomSolrRepo</artifactId> | |
<version>0.0.1</version> | |
<packaging>jar</packaging> | |
<properties> | |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
<java.version>1.8</java.version> | |
</properties> | |
<parent> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-parent</artifactId> | |
<version>1.3.0.RELEASE</version> | |
<relativePath /> | |
</parent> | |
<dependencies> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.data</groupId> | |
<artifactId>spring-data-solr</artifactId> | |
</dependency> | |
</dependencies> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-maven-plugin</artifactId> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
This file contains hidden or 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
package org.programmingmitra.customSolrRepo; | |
import org.apache.solr.client.solrj.SolrServer; | |
import org.apache.solr.client.solrj.impl.HttpSolrServer; | |
import org.programmingmitra.customSolrRepo.repository.EmployeeRepository; | |
import org.springframework.beans.factory.annotation.Value; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.data.solr.core.SolrTemplate; | |
import org.springframework.data.solr.repository.config.EnableSolrRepositories; | |
/** | |
* @author Naresh Joshi | |
* @since 30-Jan-2016 | |
*/ | |
@Configuration | |
@EnableSolrRepositories(basePackageClasses = EmployeeRepository.class) | |
public class SolrConfig { | |
@Bean | |
public SolrServer solrServer(@Value("${solr.server.url}") String solrHost) { | |
return new HttpSolrServer(solrHost); | |
} | |
@Bean | |
public SolrTemplate solrTemplate(SolrServer solrServer) { | |
return new SolrTemplate(solrServer); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment