pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 maven.apache.org/xsd/maven-4…"> <modelVersion>4.0.0</modelVersion> <groupId>cn.et</groupId> <artifactId>LuceneScoreSearch</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>com.janeluo</groupId> <artifactId>ikanalyzer</artifactId> <version>2012_u6</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.7</source> <target>1.7</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build> </project> package cn.et;
import java.io.File;
import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.document.TextField; import org.apache.lucene.index.DirectoryReader; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.queryparser.classic.QueryParser; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query; import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; import org.apache.lucene.util.Version; import org.wltea.analyzer.lucene.IKAnalyzer;
public class LueneTesting { //创建IKAnalyzer分词器 static Analyzer analyzer = new IKAnalyzer(); //创建索引,写入文件 public static void write() throws Exception { //索引存放目录 Directory directory = FSDirectory.open(new File("H:/Lucene/index")); //Lucene分词器配置 IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_47, analyzer); IndexWriter iwriter = new IndexWriter(directory, config); //创建文档对象,相当于数据库中的每条记录(MongoDB、Oracle、MySQL...) Document doc1 = new Document(); Field doc1field1 = new Field("AGE","20",TextField.TYPE_STORED); Field doc1field2 = new Field("NAME","路橙",TextField.TYPE_STORED); Field doc1field3 = new Field("BRIEF","来自中国湖南永州,是一名初级Java开发工程师,中国互联网技术博客:blog.csdn.net/phone131448…",TextField.TYPE_STORED); doc1.add(doc1field1); doc1.add(doc1field2); doc1.add(doc1field3); Document doc2 = new Document(); Field doc2field1 = new Field("AGE","21",TextField.TYPE_STORED); Field doc2field2 = new Field("NAME","谢飞",TextField.TYPE_STORED); Field doc2field3 = new Field("BRIEF","来自中国湖北武汉,是一名语文老师",TextField.TYPE_STORED); doc2.add(doc2field1); doc2.add(doc2field2); doc2.add(doc2field3); Document doc3 = new Document(); Field doc3field1 = new Field("AGE","22",TextField.TYPE_STORED); Field doc3field2 = new Field("NAME","邓娟",TextField.TYPE_STORED); Field doc3field3 = new Field("BRIEF","来自中国四川绵阳,是一名幼儿园老师",TextField.TYPE_STORED); doc3.add(doc3field1); doc3.add(doc3field2); doc3.add(doc3field3); Document doc4 = new Document(); Field doc4field1 = new Field("AGE","23",TextField.TYPE_STORED); Field doc4field2 = new Field("NAME","曹焰斌",TextField.TYPE_STORED); Field doc4field3 = new Field("BRIEF","来自中国广东广州,是一名建筑工人",TextField.TYPE_STORED); doc4.add(doc4field1); doc4.add(doc4field2); doc4.add(doc4field3); Document doc5 = new Document(); Field doc5field1 = new Field("AGE","24",TextField.TYPE_STORED); Field doc5field2 = new Field("NAME","SMISI",TextField.TYPE_STORED); Field doc5field3 = new Field("BRIEF","来自美国底特律,是一名外资企业经理",TextField.TYPE_STORED); doc5.add(doc5field1); doc5.add(doc5field2); doc5.add(doc5field3); iwriter.addDocument(doc1); iwriter.addDocument(doc2); iwriter.addDocument(doc3); iwriter.addDocument(doc4); iwriter.addDocument(doc5); iwriter.commit(); iwriter.close(); } //查找索引,收搜文档对象 public static void search(String content) throws Exception { Directory directory = FSDirectory.open(new File("H:/Lucene/index")); //指定索引查找目录 DirectoryReader ireader = DirectoryReader.open(directory); IndexSearcher isearcher = new IndexSearcher(ireader); //指定查询的field名和使用的分词解析器 QueryParser parser = new QueryParser(Version.LUCENE_47,"BRIEF",analyzer); Query query = parser.parse(content); //搜索得分(击中)排序的数组,文字中包含收搜内容的数量 ScoreDoc[] hits = isearcher.search(query, null,10).scoreDocs; for (int i = 0; i < hits.length; i++) { Document hitDoc = isearcher.doc(hits.doc); System.out.println(hitDoc.getField("AGE").stringValue()+hitDoc.getField("NAME").stringValue()+hitDoc.getField("BRIEF").stringValue()); } } public static void main(String[] args) throws Exception { write(); search("中国"); }
--------------------- 作者:phone13144830339 来源:CSDN 原文:blog.csdn.net/phone131448… 版权声明:本文为博主原创文章,转载请附上博文链接! |
|