方法类
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", "强化"); }}
结果如下:
这里采用一元分词 可以随意匹配