当前位置: 首页 > wzjs >正文

涿州网站开发wordpress 首页缩略图

涿州网站开发,wordpress 首页缩略图,长沙推广软件,中国企业排行榜前十名以下是 Spring Boot 集成 Lucene 的详细步骤&#xff1a; 添加依赖 在 Spring Boot 项目的 pom.xml 文件中添加 Lucene 的依赖&#xff0c;常用的核心依赖和中文分词器依赖如下&#xff1a; <dependency><groupId>org.apache.lucene</groupId><artifac…

以下是 Spring Boot 集成 Lucene 的详细步骤:

添加依赖

在 Spring Boot 项目的 pom.xml 文件中添加 Lucene 的依赖,常用的核心依赖和中文分词器依赖如下:

<dependency><groupId>org.apache.lucene</groupId><artifactId>lucene-core</artifactId><version>8.11.0</version>
</dependency>
<dependency><groupId>org.apache.lucene</groupId><artifactId>lucene-analyzers-common</artifactId><version>8.11.0</version>
</dependency>
<dependency><groupId>org.wltea</groupId><artifactId>ik-analyzer</artifactId><version>20200623</version>
</dependency>

创建配置类

创建一个配置类,对 Lucene 的相关组件进行配置,如索引目录、分词器等:

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.nio.file.Paths;@Configuration
public class LuceneConfig {private final String indexPath = "indexDir"; // 索引存储路径@Beanpublic Directory directory() throws Exception {return FSDirectory.open(Paths.get(indexPath));}@Beanpublic Analyzer analyzer() {return new StandardAnalyzer(); // 可替换为其他分词器,如 IKAnalyzer}
}

创建实体类

根据实际需求创建一个实体类,用于表示要索引的文档对象,例如:

public class Book {private String id;private String title;private String author;private String content;// 省略getter、setter等方法
}

创建索引服务类

创建一个服务类,用于处理索引相关的操作,如创建索引、添加文档、删除文档等:

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.*;
import org.apache.lucene.search.*;
import org.apache.lucene.store.Directory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.io.IOException;
import java.util.ArrayList;
import java.util.List;@Service
public class LuceneIndexService {@Autowiredprivate Directory directory;@Autowiredprivate Analyzer analyzer;// 创建索引public void createIndex(List<Book> bookList) throws IOException {IndexWriterConfig config = new IndexWriterConfig(analyzer);IndexWriter writer = new IndexWriter(directory, config);for (Book book : bookList) {Document doc = new Document();doc.add(new TextField("id", book.getId(), Field.Store.YES));doc.add(new TextField("title", book.getTitle(), Field.Store.YES));doc.add(new TextField("author", book.getAuthor(), Field.Store.YES));doc.add(new TextField("content", book.getContent(), Field.Store.YES));writer.addDocument(doc);}writer.close();}// 添加文档到索引public void addDocument(Book book) throws IOException {IndexWriterConfig config = new IndexWriterConfig(analyzer);IndexWriter writer = new IndexWriter(directory, config);Document doc = new Document();doc.add(new TextField("id", book.getId(), Field.Store.YES));doc.add(new TextField("title", book.getTitle(), Field.Store.YES));doc.add(new TextField("author", book.getAuthor(), Field.Store.YES));doc.add(new TextField("content", book.getContent(), Field.Store.YES));writer.addDocument(doc);writer.close();}// 删除文档public void deleteDocument(String id) throws IOException {IndexWriterConfig config = new IndexWriterConfig(analyzer);IndexWriter writer = new IndexWriter(directory, config);writer.deleteDocuments(new Term("id", id));writer.forceMergeDeletes();writer.close();}
}

创建搜索服务类

创建一个服务类,用于处理搜索相关的操作,如简单搜索、高亮搜索等:

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.*;
import org.apache.lucene.search.highlight.*;
import org.apache.lucene.store.Directory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;@Service
public class LuceneSearchService {@Autowiredprivate Directory directory;@Autowiredprivate Analyzer analyzer;// 简单搜索public List<Document> search(String queryStr) throws Exception {DirectoryReader reader = DirectoryReader.open(directory);IndexSearcher searcher = new IndexSearcher(reader);QueryParser parser = new QueryParser("content", analyzer);Query query = parser.parse(queryStr);TopDocs results = searcher.search(query, 10);List<Document> docs = new ArrayList<>();for (ScoreDoc scoreDoc : results.scoreDocs) {docs.add(searcher.doc(scoreDoc.doc));}reader.close();return docs;}// 高亮搜索public List<Map<String, String>> searchWithHighlight(String queryStr) throws Exception {DirectoryReader reader = DirectoryReader.open(directory);IndexSearcher searcher = new IndexSearcher(reader);QueryParser parser = new QueryParser("content", analyzer);Query query = parser.parse(queryStr);TopDocs results = searcher.search(query, 10);List<Map<String, String>> docs = new ArrayList<>();SimpleHTMLFormatter htmlFormatter = new SimpleHTMLFormatter("<span style='color:red'>", "</span>");Highlighter highlighter = new Highlighter(htmlFormatter, new QueryScorer(query));for (ScoreDoc scoreDoc : results.scoreDocs) {Document doc = searcher.doc(scoreDoc.doc);String content = doc.get("content");TokenStream tokenStream = analyzer.tokenStream("content", new StringReader(content));String highlightedText = highlighter.getBestFragment(tokenStream, content);Map<String, String> docMap = new HashMap<>();docMap.put("id", doc.get("id"));docMap.put("title", doc.get("title"));docMap.put("author", doc.get("author"));docMap.put("content", highlightedText != null ? highlightedText : content);docs.add(docMap);}reader.close();return docs;}
}

创建控制器类

创建一个控制器类,用于处理 HTTP 请求,并调用相应的服务类方法:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;import java.io.IOException;
import java.util.List;
import java.util.Map;@RestController
@RequestMapping("/search")
public class SearchController {@Autowiredprivate LuceneIndexService luceneIndexService;@Autowiredprivate LuceneSearchService luceneSearchService;// 创建索引@PostMapping("/index")public String createIndex(@RequestBody List<Book> bookList) {try {luceneIndexService.createIndex(bookList);return "索引创建成功";} catch (IOException e) {e.printStackTrace();return "索引创建失败";}}// 搜索结果@GetMappingpublic List<Document> search(@RequestParam String query) {try {return luceneSearchService.search(query);} catch (Exception e) {e.printStackTrace();return new ArrayList<>();}}// 高亮搜索@GetMapping("/highlight")public List<Map<String, String>> searchWithHighlight(@RequestParam String query) {try {return luceneSearchService.searchWithHighlight(query);} catch (Exception e) {e.printStackTrace();return new ArrayList<>();}}
}

使用示例

此外,还可以根据实际需求对上述代码进行扩展和优化,例如添加更复杂的查询条件、实现分页功能、优化索引的性能等。

  • 创建索引 :启动 Spring Boot 应用后,发送一个 POST 请求到http://localhost:8080/search/index,请求体中包含要索引的图书列表,如:

    [{"id": "1","title": " Lucene in Action ","author": "Robert Muir","content": "Lucene is a search library from Apache"},{"id": "2","title": " Java编程思想 ","author": "Bruce Eckel","content": "Java is a programming language"}
    ]

  • 简单搜索 :发送一个 GET 请求到http://localhost:8080/search/?query=Java,即可搜索出与“Java”相关的文档。

  • 高亮搜索 :发送一个 GET 请求到http://localhost:8080/search/highlight/?query=Java,即可搜索出与“Java”相关的文档,并且搜索结果中的“Java”会以高亮显示。


文章转载自:

http://xTxcSLcl.ddrdt.cn
http://9Zu9ZRWG.ddrdt.cn
http://SimUO3uQ.ddrdt.cn
http://HaIXCdhf.ddrdt.cn
http://XC5bHdyF.ddrdt.cn
http://ozct9ACy.ddrdt.cn
http://Q6w8RY8n.ddrdt.cn
http://kPJaarhW.ddrdt.cn
http://dPa2ZHMP.ddrdt.cn
http://Zm74xkfj.ddrdt.cn
http://F9FDZyVd.ddrdt.cn
http://kQkdTC52.ddrdt.cn
http://Rw1SYcuk.ddrdt.cn
http://VckrWDBD.ddrdt.cn
http://mpTxrwHI.ddrdt.cn
http://2nopakgO.ddrdt.cn
http://J0QVlLHj.ddrdt.cn
http://UfqsrokB.ddrdt.cn
http://uRqTvfMq.ddrdt.cn
http://5N0mffNe.ddrdt.cn
http://c2mjfiPz.ddrdt.cn
http://qb7UFXxO.ddrdt.cn
http://C4ffhyEX.ddrdt.cn
http://lP0sQrUF.ddrdt.cn
http://c6kaGHb2.ddrdt.cn
http://BY4DyyAV.ddrdt.cn
http://eoDV9uhe.ddrdt.cn
http://aATGxdzn.ddrdt.cn
http://rIiYOkfR.ddrdt.cn
http://XntSbvaB.ddrdt.cn
http://www.dtcms.com/wzjs/710034.html

相关文章:

  • 网站开发用什么编辑器手机网站建设规划图
  • 避免网站 40418种禁用软件黄app入口
  • 北京网站seo收费标准wordpress 判断页面id
  • 购物网站分为几个模块网站推广关键词
  • 谈谈对电子商务网站建设与管理国内网站建设
  • 塑胶制品 东莞网站建设三门峡做网站的公司
  • 苏州市建设交通高等学校网站高端网站建站公司
  • wordpress站内跳转做民宿要给网站多少钱
  • 内网网站建设主流语言青岛电商网站制作
  • 做网站后期费用深圳 网站托管
  • 如何看网站是否有做网站地图建设银行信用卡网站是哪个
  • 重庆南岸网站建设免费推广软件排行榜
  • 公司网站开发人员的的工资多少钱彩票网站开发制作模版
  • 内丘附近网站建设价格wordpress文章统计
  • 厦门网站建设多少钱连云区住房和城乡建设局网站
  • 名片在哪个网站做施工企业损益类科目
  • 赤水市建设局官方网站中国城市建设网站
  • 漳州网站开发wordpress 广告公司主题
  • 做推广便宜的网站dw用层还是表格做网站快
  • 免费域名注册服务网站网站建设资格预审公告
  • 网站如何做h5动态页面设计个人网站备案查询
  • 百度公司做网站服务青岛建设公司网站
  • 绿色简单网站合肥网站设计建设公司
  • wordpress建站邮件在线网页游戏免费玩
  • 网站怎么进入电脑更新wordpress
  • 佛山网站建设seo优化WordPress如何设置邮箱验证
  • 济南网站建设方案详细单位微信公众号怎么创建
  • 做销售网站的公司哪家最好wordpress百度小程序
  • 高职示范校建设专题网站qq网页版在线登录官网
  • 山西省住房和城乡建设厅门户网官方网站擦边球做网站挣钱