1.maven添加依赖包
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-core</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-analyzers-common</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-queryparser</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-queries</artifactId>
<version>4.0.0</version>
</dependency>
2.索引基类
public void searchCreate(School school){ content = ""; try { Document document = new Document(); content = school.getName()+school.getRegion().getName(); document.add(new TextField("schoolIndex",content, Field.Store.YES)); document.add(new TextField("schoolName",school.getName(), Field.Store.YES)); document.add(new TextField("schoolId",school.getId().toString(), Field.Store.YES)); document.add(new TextField("region",school.getRegion().getName().toString(),Field.Store.YES)); document.add(new TextField("parentRegion",school.getRegion().getParent().getName().toString(),Field.Store.YES)); indexWriter.addDocument(document); } catch (IOException e) { e.printStackTrace(); } }
3.创建索引数据
public static String content = ""; private static String INDEX_DIR = "indexfile"; private static Analyzer analyzer = null; private static Directory directory = null; private static IndexWriter indexWriter = null; /** * 创建索引 * @param listSchool * @return */ public String createIndex(List<School> listSchool){ StringBuilder sb = new StringBuilder(); Date date = new Date(); Long total = 0l; try { deletedIndex(); analyzer = new StandardAnalyzer(Version.LUCENE_40); directory = FSDirectory.open(new File(INDEX_DIR)); IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_40,analyzer); indexWriter = new IndexWriter(directory,config); for(School school:listSchool){ searchCreate(school); total++; System.out.println("create index number "+total); } indexWriter.commit(); closeWriter(); } catch (IOException e) { e.printStackTrace(); } Date date1 = new Date(); sb.append("create index number "+total+" time cost "+(date1.getTime()-date.getTime())); System.out.println("create index number "+total+" time cost "+(date1.getTime()-date.getTime())); return sb.toString(); }
4.拼装数据返回结果
/** * 查找索引,返回符合条件的学校名称组合字符串 * @param text 查找的学校名称 */ public String searchIndex(String text){ JsonArray jsonArray = new JsonArray(); try { directory = FSDirectory.open(new File(INDEX_DIR)); analyzer = new StandardAnalyzer(Version.LUCENE_40); DirectoryReader directoryReader = DirectoryReader.open(directory); IndexSearcher indexSearcher = new IndexSearcher(directoryReader); QueryParser parser = new QueryParser(Version.LUCENE_40,"schoolIndex",analyzer); parser.setDefaultOperator(QueryParser.Operator.OR); Query query = parser.parse(text); Sort sort=new Sort(); ScoreDoc[] hits = indexSearcher.search(query,null,50,sort).scoreDocs; for(int i=0;i<hits.length;i++){ Document hitDoc = indexSearcher.doc(hits[i].doc); JsonObject jsonObject = new JsonObject(); jsonObject.addProperty("schoolName",hitDoc.get("schoolName")); jsonObject.addProperty("region",hitDoc.get("region")); jsonObject.addProperty("schoolId",hitDoc.get("schoolId")); jsonObject.addProperty("parentRegion",hitDoc.get("parentRegion")); jsonArray.add(jsonObject); } directoryReader.close(); } catch (IOException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } return jsonArray.toString(); }
5.删除索引
/** * 删除索引 * @throws IOException */ public void deletedIndex(){ Date date = new Date(); try { analyzer = new StandardAnalyzer(Version.LUCENE_40); directory = FSDirectory.open(new File(INDEX_DIR)); IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_40, analyzer); indexWriter = new IndexWriter(directory, config); indexWriter.deleteAll(); indexWriter.close(); } catch (IOException e) { e.printStackTrace(); } Date date1 = new Date(); System.out.println("delete index cost time "+(date1.getTime()-date.getTime())+"ms\n"); }