一.创建索引
1 @Test 2 public void indexCreate() throws IOException { 3 //创建分词器 4 Analyzer analyzer = new StandardAnalyzer(); 5 IndexWriterConfig config = new IndexWriterConfig(analyzer); 6 7 //指定索引存放目录以及配置参数 8 Directory directory =FSDirectory.open(Paths.get("F:/luceneIndex")); 9 IndexWriter writer = new IndexWriter(directory,config);10 11 //采集数据12 ListbookList = bookDao.findAll();13 /*for (Book book : bookList) {14 System.out.println(book.getName());15 }*/16 //将采集的数据封装到Document中17 Document doc = null;18 for (Book book : bookList) {19 doc = new Document();20 // store:如果是yes,则说明存储到文档域中21 //id:不分词,索引,存储22 Field id = new StringField("id",book.getId().toString(),Store.YES);23 24 //name:分词,索引,存储25 Field name = new TextField("name",book.getName(),Store.YES);26 //price:不分词,索引,存储,27 Field price = new FloatPoint("price",book.getPrice());28 29 //pic:不分词,不索引,存储,存储要添加一个同名的StoredField30 Field pic = new StoredField("pic",book.getPic());31 //desciption:分词,索引,不存储32 Field description = new TextField("description",book.getDescription(),Store.NO); 33 doc.add(id);34 doc.add(name);35 doc.add(price);36 doc.add(new StoredField("price",book.getPrice()));//存储37 doc.add(new NumericDocValuesField("price",book.getPrice().longValue()));//排序38 doc.add(pic);39 doc.add(description);40 41 //生成索引42 writer.addDocument(doc);43 }44 45 //关闭46 writer.close();47 }
注意:创建索引时,对应price采用了FloatPoint,如果要对此字段进行存储,要添加同名的StoredField,如果要对此字段进行排序,要添加同名的NumericDocValuesField,
否则会报unexpected docvalues type异常
二.排序
1 //查询 2 @Test 3 public void indexSearch() throws IOException, ParseException { 4 String[] fields = {"name","description"}; 5 MultiFieldQueryParser parser = new MultiFieldQueryParser(fields,new StandardAnalyzer()); 6 Query query = parser.parse("lucene"); 7 query = parser.parse("java"); 8 doSearch(query); 9 }10 11 private void doSearch (Query query) {12 Directory directory;13 try {14 directory = FSDirectory.open(Paths.get("F:/luceneIndex"));15 16 IndexReader reader = DirectoryReader.open(directory);17 //创建IndexSearcher18 IndexSearcher searcher = new IndexSearcher(reader);19 //true表示降序20 //SortField.Type.SCORE 根据相关度进行排序(默认)21 //SortField.Type.DOC 根据文档编号或者说是索引顺序22 //SortField.Type.FLOAT(Long等),根据fieldName的数值类型进行排序23 SortField sortField = new SortField("price",SortField.Type.FLOAT,true);24 Sort sort = new Sort(sortField);25 TopDocs topDocs = searcher.search(query,10,sort);26 long count = topDocs.totalHits;27 System.out.println("匹配的总条数是----" + count);28 29 //根据相关度排序后的结果30 ScoreDoc[] docs = topDocs.scoreDocs;31 for (ScoreDoc scoreDoc : docs) {32 int docId = scoreDoc.doc;33 Document document = searcher.doc(docId);34 System.out.println("id----"+document.get("id"));35 System.out.println("name----"+document.get("name"));36 System.out.println("price----"+document.get("price"));37 /*System.out.println("pic----"+document.get("pic"));38 System.out.println("description----"+document.get("description"));*/39 System.out.println("======================");40 }41 reader.close();42 } catch (IOException e) {43 // TODO Auto-generated catch block44 e.printStackTrace();45 }46 47 }
此外还有
//Sort.RELEVANCE 根据相关度排序
//Sort.INDEXORDER 根据索引顺序排序 TopDocs topDocs = searcher.search(query,10,Sort.INDEXORDER);