lucene简单搜索demo
发布日期:2021-06-24 06:54:49 浏览次数:4 分类:技术文章

本文共 2392 字,大约阅读时间需要 7 分钟。

方法类

package com.wxf.Test;import com.wxf.pojo.Goods;import org.apache.lucene.analysis.standard.StandardAnalyzer;import org.apache.lucene.document.Document;import org.apache.lucene.document.Field;import org.apache.lucene.document.StringField;import org.apache.lucene.document.TextField;import org.apache.lucene.index.*;import org.apache.lucene.queryparser.classic.ParseException;import org.apache.lucene.queryparser.classic.QueryParser;import org.apache.lucene.search.*;import org.apache.lucene.store.Directory;import org.apache.lucene.store.FSDirectory;import java.io.IOException;import java.nio.file.Paths;/** * @Auther: wxf * @Date: 2018/6/29 15:40 */public class IndexCRUD {    private Directory dir;    {        try {            dir = FSDirectory.open(Paths.get( System.getProperty("user.dir")+"\\src\\main\\resources\\index"));        } catch (IOException e) {            e.printStackTrace();        }    }    /**     * 获取IndexWriter实例     * @return     * @throws Exception     */    public IndexWriter getWriter()throws Exception{        //中文分词器        StandardAnalyzer standardAnalyzer = new StandardAnalyzer();        IndexWriterConfig iwc=new IndexWriterConfig(standardAnalyzer);        IndexWriter writer=new IndexWriter(dir, iwc);        return writer;    }    public void setUp() throws Exception {        Goods goods=new Goods("123","红色强化门",360);        Goods goods2=new Goods("223","黑色强化门",370);        Goods goods3=new Goods("333","白色强化门",380);         String skuid[]={"123","223","333"};         String name[]={"红色强化门","黑色强化门","白色强化门"};         Object obj[]={goods,goods2,goods3};        IndexWriter writer=getWriter();        for(int i=0;i

测试类

package com.wxf.Test;/** * @Auther: wxf * @Date: 2018/6/29 15:46 */public class Test {    public static void main(String[] args) throws Exception {        IndexCRUD indexCRUD=new IndexCRUD();      // indexCRUD.setUp();        indexCRUD.testIndexWriter();        indexCRUD.testIndexReader();         indexCRUD.select("name", "黑");    }}

  

indexCRUD.setUp() 这个方法 调一次就可以了 结果如下

这次换个范围大的查询参数

public class Test {    public static void main(String[] args) throws Exception {        IndexCRUD indexCRUD=new IndexCRUD();      // indexCRUD.setUp();        indexCRUD.testIndexWriter();        indexCRUD.testIndexReader();         indexCRUD.select("name", "强化");    }}

  结果如下:

这里采用一元分词 可以随意匹配

转载于:https://www.cnblogs.com/wxf-com/p/9260209.html

转载地址:https://blog.csdn.net/weixin_30896763/article/details/98297871 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Byte Array To Image Function
下一篇:SVG交互动画制作

发表评论

最新留言

留言是一种美德,欢迎回访!
[***.207.175.100]2024年04月09日 02时27分42秒