```html Apache Solr Configuration with Spring Boot

Apache Solr Configuration with Spring Boot

What is Apache Solr?

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.

Solr is commonly used for product search, document search, log analytics, and enterprise search applications.

Step 1: Configure data-config.xml

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>

Step 2: Configure managed-schema

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" />

Step 3: Add Maven Dependency

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

Step 4: Configure application.properties

spring.data.solr.host=http://localhost:8983/solr

Step 5: Create Solr Entity

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
}

Step 6: Create Repository

import org.springframework.data.solr.repository.SolrCrudRepository;

public interface ProductRepository
        extends SolrCrudRepository<Product, String> {

}

Step 7: Use Repository in Service Layer

@Service
public class ProductService {

    @Autowired
    private ProductRepository repository;

    public Iterable<Product> getAllProducts() {
        return repository.findAll();
    }

    public Product save(Product product) {
        return repository.save(product);
    }
}

Step 8: Create REST Controller

@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);
    }
}

Step 9: Run and Test

Insert Data

POST
http://localhost:8080/products
{
   "id":"101",
   "productName":"Laptop",
   "price":65000
}

Fetch Data

GET
http://localhost:8080/products

Solr Import Commands

Full Import

http://localhost:8983/solr/products/dataimport?command=full-import

Delta Import

http://localhost:8983/solr/products/dataimport?command=delta-import

Solr Architecture Flow

Database
    |
    v
data-config.xml
    |
    v
Solr Index
    |
    v
Spring Data Solr Repository
    |
    v
Service Layer
    |
    v
REST Controller
    |
    v
Client Application

Advantages of Solr

Common Interview Questions

  1. What is Apache Solr?
  2. Difference between Solr and Elasticsearch?
  3. What is data-config.xml?
  4. What is managed-schema?
  5. What is Delta Import?
  6. What is Full Import?
  7. What is SolrDocument annotation?
  8. How does Solr indexing work?
  9. What is faceted search?
  10. How do you optimize Solr performance?
```