Home >Java >javaTutorial >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:
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:
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>
Clear all created fields:
curl http://localhost:8983/solr/<core name>/schema -H 'Content-type:application/json' -d '{"delete-all-fields":{}}'
Add fields:
curl http://localhost:8983/solr/<core name>/schema -H 'Content-type:application/json' -d ' { "add-field":{ "name":"field_name", "type":"string", "stored":true } }'
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>
Add field:
{ "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" } ] } } }
{ "add-field": { "name": "title", "type": "text_en", "stored": true } }
field is added successfully.
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>
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.
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>
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!