在现代互联网时代,搜索引擎已经成为人们获取信息的主要方式之一。而对于一些需要实时性的应用场景,如在线聊天、实时监控等,则需要实时搜索技术的支持。本文将介绍 Java 实时搜索的相关技术,并提供实用的教程分享。 一、Lucene 实时搜索
在现代互联网时代,搜索引擎已经成为人们获取信息的主要方式之一。而对于一些需要实时性的应用场景,如在线聊天、实时监控等,则需要实时搜索技术的支持。本文将介绍 Java 实时搜索的相关技术,并提供实用的教程分享。
一、Lucene 实时搜索
Lucene 是一个高性能、全文检索引擎库,它可以被用于 Java 应用程序中。Lucene 支持实时搜索,可以在数据更新时立即响应用户的搜索请求。
首先,我们需要创建一个索引,用于存储我们要搜索的数据。下面是一个简单的示例代码:
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
public class Indexer {
public static void main(String[] args) throws Exception {
// 创建索引目录
Directory directory = new RAMDirectory();
// 创建索引写入器
IndexWriter writer = new IndexWriter(directory, new StandardAnalyzer(), true, IndexWriter.MaxFieldLength.UNLIMITED);
// 添加文档
Document doc = new Document();
doc.add(new Field("id", "1", Field.Store.YES, Field.Index.NOT_ANALYZED));
doc.add(new Field("content", "Lucene is a high-perfORMance, full-featured text search engine library written entirely in Java.", Field.Store.YES, Field.Index.ANALYZED));
writer.aDDDocument(doc);
// 提交写入
writer.commit();
// 关闭写入器
writer.close();
}
}
一旦索引创建完成,我们就可以进行实时搜索了。下面是一个简单的示例代码:
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TerMQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
public class Searcher {
public static void main(String[] args) throws Exception {
// 创建索引目录
Directory directory = new RAMDirectory();
// 创建索引写入器
IndexWriter writer = new IndexWriter(directory, new StandardAnalyzer(), true, IndexWriter.MaxFieldLength.UNLIMITED);
// 添加文档
Document doc = new Document();
doc.add(new Field("id", "1", Field.Store.YES, Field.Index.NOT_ANALYZED));
doc.add(new Field("content", "Lucene is a high-performance, full-featured text search engine library written entirely in Java.", Field.Store.YES, Field.Index.ANALYZED));
writer.addDocument(doc);
// 提交写入
writer.commit();
// 创建索引读取器
IndexReader reader = writer.getReader();
// 创建搜索器
IndexSearcher searcher = new IndexSearcher(reader);
// 创建查询
Query query = new TermQuery(new Term("content", "Java"));
// 执行查询
TopDocs topDocs = searcher.search(query, 10);
// 输出结果
System.out.println("查询结果总数:" + topDocs.totalHits);
for (int i = 0; i < topDocs.scoreDocs.length; i++) {
System.out.println("文档 ID:" + topDocs.scoreDocs[i].doc + ",得分:" + topDocs.scoreDocs[i].score);
}
// 关闭搜索器
searcher.close();
// 关闭读取器
reader.close();
// 关闭写入器
writer.close();
}
}
二、elasticsearch 实时搜索
除了 Lucene,Elasticsearch 也是一个流行的实时搜索引擎。它基于 Lucene 构建,提供了更加灵活和易用的 api,同时还支持分布式搜索和数据存储。
首先,我们需要安装 Elasticsearch。可以从官网下载安装包,也可以使用 Docker 安装。这里介绍使用 Docker 安装的方法:
docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 elasticsearch:7.9.3
使用 Elasticsearch 创建索引非常简单,只需要向其发送一个 Http 请求即可。下面是一个简单的示例代码:
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;
public class Indexer {
public static void main(String[] args) throws Exception {
// 创建 HTTP 客户端
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建索引请求
String indexName = "my_index";
Request request = new Request("PUT", "/" + indexName);
String requestBody = "{
" +
" "mappings": {
" +
" "properties": {
" +
" "id": {
" +
" "type": "keyWord"
" +
" },
" +
" "content": {
" +
" "type": "text"
" +
" }
" +
" }
" +
" }
" +
"}";
HttpEntity entity = new StringEntity(requestBody, ContentType.APPLICATION_JSON);
request.setEntity(entity);
// 发送请求
RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200, "http")).build();
Response response = restClient.performRequest(request);
// 输出结果
System.out.println(response.getStatusLine().getStatusCode());
System.out.println(response.getEntity().getContent().toString());
// 关闭客户端
httpClient.close();
restClient.close();
}
}
使用 Elasticsearch 进行实时搜索也非常简单,只需要向其发送一个 HTTP 请求即可。下面是一个简单的示例代码:
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.lucene.search.TotalHits;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.ScoreSortBuilder;
import org.elasticsearch.search.sort.SortOrder;
public class Searcher {
public static void main(String[] args) throws Exception {
// 创建 HTTP 客户端
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建搜索请求
String indexName = "my_index";
String query = "Java";
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(QueryBuilders.matchQuery("content", query));
sourceBuilder.sort(new ScoreSortBuilder().order(SortOrder.DESC));
sourceBuilder.from(0);
sourceBuilder.size(10);
String requestBody = sourceBuilder.toString();
Request request = new Request("POST", "/" + indexName + "/_search");
HttpEntity entity = new StringEntity(requestBody, ContentType.APPLICATION_jsON);
request.setEntity(entity);
// 发送请求
RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200, "http")).build();
Response response = restClient.performRequest(request);
// 输出结果
System.out.println(response.getStatusLine().getStatusCode());
System.out.println(response.getEntity().getContent().toString());
// 关闭客户端
httpClient.close();
restClient.close();
}
}
三、总结
本文介绍了 Java 实时搜索的相关技术,并提供了实用的教程分享。Lucene 和 Elasticsearch 都是非常流行的实时搜索引擎,它们都提供了灵活和易用的 API。在实际应用中,需要根据具体的场景选择适合自己的搜索引擎。
--结束END--
本文标题: Java 实时搜索:实用教程分享
本文链接: https://lsjlt.com/news/407020.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-04-01
2024-04-03
2024-04-03
2024-01-21
2024-01-21
2024-01-21
2024-01-21
2023-12-23
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0