```html
Apache Solr is an open-source enterprise search platform built on Apache Lucene. It provides powerful full-text search, faceted search, indexing, replication, and distributed search capabilities.
Define database queries used for full imports and incremental imports.
<dataConfig>
<dataSource type="JdbcDataSource"
driver="com.mysql.cj.jdbc.Driver"
url="jdbc:mysql://localhost:3306/testdb"
user="root"
password="root"/>
<document>
<entity name="product"
query="SELECT * FROM products"
deltaImportQuery="
SELECT * FROM products
WHERE id='${dih.delta.id}'"
deltaQuery="
SELECT id FROM products
WHERE modified_date > '${dih.last_index_time}'">
</entity>
</document>
</dataConfig>
Declare all searchable and indexed fields in the schema file.
<field name="AUDIT_ID"
type="string"
indexed="true"
stored="true"
multiValued="false" />
<field name="PRODUCT_NAME"
type="text_general"
indexed="true"
stored="true" />
<field name="PRICE"
type="pdouble"
indexed="true"
stored="true" />
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-solr</artifactId>
</dependency>
spring.data.solr.host=http://localhost:8983/solr
import org.springframework.data.annotation.Id;
import org.springframework.data.solr.core.mapping.SolrDocument;
import org.springframework.data.solr.core.mapping.Indexed;
@SolrDocument(collection = "products")
public class Product {
@Id
@Indexed(name = "id")
private String id;
@Indexed(name = "product_name")
private String productName;
@Indexed(name = "price")
private Double price;
// Getters and Setters
}
import org.springframework.data.solr.repository.SolrCrudRepository;
public interface ProductRepository
extends SolrCrudRepository<Product, String> {
}
@Service
public class ProductService {
@Autowired
private ProductRepository repository;
public Iterable<Product> getAllProducts() {
return repository.findAll();
}
public Product save(Product product) {
return repository.save(product);
}
}
@RestController
@RequestMapping("/products")
public class ProductController {
@Autowired
private ProductService service;
@GetMapping
public Iterable<Product> getAllProducts() {
return service.getAllProducts();
}
@PostMapping
public Product save(
@RequestBody Product product) {
return service.save(product);
}
}
POST http://localhost:8080/products
{
"id":"101",
"productName":"Laptop",
"price":65000
}
GET http://localhost:8080/products
http://localhost:8983/solr/products/dataimport?command=full-import
http://localhost:8983/solr/products/dataimport?command=delta-import
Database
|
v
data-config.xml
|
v
Solr Index
|
v
Spring Data Solr Repository
|
v
Service Layer
|
v
REST Controller
|
v
Client Application