Lucene 的使用

1、lucene的原理
            网络爬虫把资源存入索引库,再交给页面调用。而Lucene只负责管理索引库。


2、建 Java 项目Lucene_Test

3、导包

            lucene-core-3.0.1.jar
            lucene-analyzers-3.0.1.jar
            lucene-highlighter-3.0.1.jar
            lucene-memory-3.0.1.jar   

4、建类HelloWorld.java,分别创建“穿件“创建索引”和“查询”方法

			public  class  HelloWorld{
				// 创建索引
				@Test
				public  void  testCreateIndex()  throws  Exception{
					
				}

				// 搜索
				@Test
				public  void  testSearch()  throws Exception{

				}
			}

5、建类 Article.java 作为javabean保存数据
            public  class  Article{
                private  Integer id;
                private  String title;
                private  String  content;
                封装……
            }

6、实现“建立索引”和“搜索”功能

            // 索引库,设为全局变量
                private static Directory  directory;    // 索引库目录
                private  static Analyzer  analyzer;    // 分词器
                
                static{        //Shift + Alt + A
                    try{
                        directory  = FSDirectory.open(new  File("./indexDir"));
                        analyzer  = new StandardAnalyze(Version.LUCENE_3.0);
                    }catch(Exception  e){
                        throw  new RuntimeException(e);
                    }
                }

            // 创建索引
                @Test
                public  void  testCreateIndex()  throws  Exception{
                    Article   article  = new Article();
                    article.setTitle("标题");
                    article.setContent("内容……");

                    // 放到 Lucene 中
                    // 1、把Article 转为 Document
                        Document  doc = new Document();
                        String  idStr = article.getId().toString();
                        doc.add( new Field("id", idStr , Store.YES,  Index.ANALYZED ));
                        doc.add( new Field("title", article.getTitle(), Store.YES,  Index.ANALYZED ));
                        doc.add( new Field("content", article.getContent(), Store.YES,  Index.ANALYZED ));
                        
                    //2、把Document 放到索引库中(此处设为全局变量,见开头)
        
                        
                        IndexWriter  indexWriter = new  IndexWriter( directory, analyzer,  MaxFieldLength.LIMITED);
                        indexWriter.addDocument(doc);
                        indexWriter.close();
                }


            // 搜索
                @Test
                public  void  testSearch()  throws Exception{
                    // 准备查询条件
                        String  queryString = "lucene";

                    // 执行搜索
                        List<Article>  list = new ArrayList<Article>();
                    //======================================================

                        //1、把查询字符串转为Query对象(默认只从title中查询)
                            QueryParser  queryParser  = new  QueryParser(Version.LUCENE_30, "title",  analyzer);    // “title”表示从标题中查询
                            Query  query = queryParser.parse(queryString);

                        //2、执行查询,得到中间结果,从指定的索引库中搜索东西
                            IndexSearcher  indexSearcher = new IndexSearcher(directory);
                            TopDocs  topDocs  = indexSearcher.search(query,  100);    // 返回前 100 条结果,分页用

                            int  count = topDocs.totalHits;
                            ScoreDoc[]  scoreDoc = topDocs.scoreDocs;

                        // 3、处理结果
                            for(int i =0; i<scoreDocs.length; i++){
                                ScoreDoc  scoreDoc = scoreDocs[i];
                                float  score = scoreDoc.score;        //相关度得分
                                int  docId = scoreDoc.doc;        //Document的内部编号,唯一且自动生成的

                                // 根据编号拿到Document数据
                                    Document  doc = indexsearcher.doc(docId);

                                //把Document转为Article
                                    String  idStr = doc.get("id");    //等价于doc.getField("content").stringValue();
                                    String  title = doc.getField("title").stringValue();
                                    String  content = doc.get("content");

                                    Article  article = new Article();
                                    article.setId(integer.parseIn(idStr));
                                    article.setTitle(title);
                                    article.setContent(content);

                                    list.add(article);

                            }
                            indexSearcher.close();

                    //======================================================
                    // 显示结果
                        s.o.p("总结果数:"+list.size());
                        s.o.p("---------------------------------------------");
                        for(Aritcle  a : list){
                            s.o.p("id = " + a.getId() );
                            s.o.p("title = " + a.getTitle() );
                            s.o.p("content = " + a.getContent() );
                        }
                }




7、运行Junit

       结果:在项目中出现 indexDir 文件夹,里面出现三个索引库文件。

            总结果数:1
            id = ……
            title =……
            content =……

内容概要:本文介绍了一个基于冠豪猪优化算法(CPO)的无人机三维路径规划项目,利用Python实现了在复杂三维环境中为无人机规划安全、高效、低能耗飞行路径的完整解决方案。项目涵盖空间环境建模、无人机动力学约束、路径编码、多目标代价函数设计以及CPO算法的核心实现。通过体素网格建模、动态障碍物处理、路径平滑技术和多约束融合机制,系统能够在高维、密集障碍环境下快速搜索出满足飞行可行性、安全性与能效最优的路径,并支持在线重规划以适应动态环境变化。文中还提供了关键模块的代码示例,包括环境建模、路径评估和CPO优化流程。; 适合人群:具备一定Python编程基础和优化算法基础知识,从事无人机、智能机器人、路径规划或智能优化算法研究的相关科研人员与工程技术人员,尤其适合研究生及有一定工作经验的研发工程师。; 使用场景及目标:①应用于复杂三维环境下的无人机自主导航与避障;②研究智能优化算法(如CPO)在路径规划中的实际部署与性能优化;③实现多目标(路径最短、能耗最低、安全性最高)耦合条件下的工程化路径求解;④构建可扩展的智能无人系统决策框架。; 阅读建议:建议结合文中模型架构与代码示例进行实践运行,重点关注目标函数设计、CPO算法改进策略与约束处理机制,宜在仿真环境中测试不同场景以深入理解算法行为与系统鲁棒性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值