Using Solr for full-text search in Java API development
With the development of the Internet and the explosive growth of information, the amount of information we can now access and obtain is very large. Whether it is information obtained from web pages, literature or daily life, we need an efficient way to process and manage. Full-text search is a very efficient and commonly used way to locate and extract the information we need through keywords or phrases, and Solr is a tool that is very suitable for full-text search development.
This article will introduce the basic concepts of Solr and how to use Solr for full-text search in Java API development.
1. Introduction to Solr
Solr is essentially an open source enterprise search platform, which is based on the Apache Lucene Java library and provides some convenient REST APIs. According to Solr’s official introduction, it mainly includes the following features:
- Full-text search
- Real-time search
- Website search
- Distributed search
- Database integration
- Parsing of multiple file formats
- Export of multiple file formats
- External configuration
As a An enterprise-level search platform, Solr provides very complete search functions and flexible configuration methods, which makes it easy for us to develop search modules in actual development. Below we will focus on how to use Solr for full-text search in Java API development.
2. Use Solr for full-text search
2.1 Install Solr
First, we need to install Solr in the local environment or server. Solr officially provides a very convenient way to install and run Solr:
- Download Solr address: https://lucene.apache.org/solr/downloads.html.
- Unzip the file: tar -zxvf solr-x.x.x.zip.
- Enter the decompressed folder: cd solr-x.x.x.
- Start Solr: bin/solr start.
2.2 Create Solr Core and define Schema
Solr Core is a very core concept in Solr. It is equivalent to an independent search environment, including data index and search request deal with. In Solr, we can create multiple Cores, and each Core can have independent configuration and data sets.
In Solr, we need to first create a Core and then define the Schema for search. Schema defines data fields, types, indexes and other attributes, which will affect the efficiency and accuracy of search.
You can use Solr's own Schema or user-defined Schema to define the field attributes required for search. Here we introduce the use of Solr's default Schema.
Use Solr's own default Schema:
-
Create core:
bin/solr create_core -c <core name>
Copy after loginCopy after login Clear all created fields:
curl http://localhost:8983/solr/<core name>/schema -H 'Content-type:application/json' -d '{"delete-all-fields":{}}'
Copy after loginAdd fields:
curl http://localhost:8983/solr/<core name>/schema -H 'Content-type:application/json' -d ' { "add-field":{ "name":"field_name", "type":"string", "stored":true } }'
Copy after login- Restart Solr service
Use user-defined Schema:
Users can Customize Schema to meet different application needs. Solr provides Schema API. Provides easy Schema addition for creators.
Create core:
bin/solr create_core -c <core name>
Copy after loginCopy after loginAdd field:
- Add field type:
Add field to Solr server Send a POST request with content_type set to application/json
{ "add-field-type": { "name":"test_fieldType","class":"solr.TextField","indexAnalyzer":{ "tokenizer":{ "class":"solr.StandardTokenizerFactory" },"filters":[ { "class":"solr.StopFilterFactory", "ignoreCase":true, "words":"stopword.txt", "format":"snowball", "enablePositionIncrements":true },{ "class":"solr.SnowballPorterFilterFactory", "language":"English" } ], "queryAnalyzer":{ "tokenizer":{ "class":"solr.StandardTokenizerFactory" },"filters":[ { "class":"solr.StopFilterFactory", "ignoreCase":true, "words":"stopword.txt", "format":"snowball", "enablePositionIncrements":true },{ "class":"solr.SnowballPorterFilterFactory", "language":"English" } ] } } }
Copy after login- Add field (title): A response will be returned after the
{ "add-field": { "name": "title", "type": "text_en", "stored": true } }
Copy after loginfield is added successfully.
- Add field type:
Specify this Schema in Solr config:
Enter the ${core}/conf directory to modify solrconfig.xml and add the following code:
<schemaFactory class="ClassicIndexSchemaFactory"/> <schema name="schema_name" version="1.5"> <field name="title" type="text_en" indexed="true" stored="true" /> </schema>
Copy after login
2. Use Solr in Java API for full-text search
Next, we will introduce how to use Solr's Java API for full-text search in Java Web applications.
- Add SolrJ dependency
In the Maven project, we need to add SolrJ dependency. Solr officially provides a very convenient dependency:
<dependency> <groupId>org.apache.solr</groupId> <artifactId>solr-solrj</artifactId> <version>8.2.0</version> </dependency>
- Use SolrJ to search in code
In Java code, we can use QueryResponse and SolrClient provided by SolrJ to perform search operations. The queried data can be viewed as key-value pairs.
First, we need to create an instance of SolrClient. In SolrJ, SolrClient is the client of Solr and provides a method to directly call the Solr REST API.
String solrUrl = "http://localhost:8983/solr/core_name"; SolrClient solr = new HttpSolrClient.Builder(solrUrl).build();
Then, we need to create a SolrQuery object, which is used to set search parameters, including search keywords, sorting rules, filter conditions, etc.
SolrQuery query = new SolrQuery(); query.setQuery("keyword"); query.setStart(0); query.setRows(10); query.setSort("sort_field", SolrQuery.ORDER.asc); query.addFilterQuery("filter_field:filter_value");
Finally, we can perform a search through the solr.query(query) method, and the returned QueryResponse contains the search results and related metadata information. We need to get the search results list through the response.getResults() method.
QueryResponse response = solr.query(query); SolrDocumentList results = response.getResults();
In actual development, we can also perform secondary processing on the search results as needed, such as converting the results into JSON format or Java objects.
for (SolrDocument result : results) { String id = (String) result.getFieldValue("id"); String title = (String) result.getFieldValue("title"); String content = (String) result.getFieldValue("content"); // 将搜索结果转换为 Java 实体类 SearchResult item = new SearchResult(id, title, content); // 或将搜索结果转换为 JSON 字符串 String json = new ObjectMapper().writeValueAsString(result); }
The above is the basic process of using Solr for full-text search in Java API development. Solr provides very complete search functions and Java API support, allowing us to easily develop efficient and accurate search modules to provide users with a better search experience.
The above is the detailed content of Using Solr for full-text search in Java API development. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.
