<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 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.firewarm</groupId>
<artifactId>testLucene</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>testLucene Maven Webapp</name>
<url> http://maven.apache.org</url>
<repositories>
<repository>
<id>mine</id>
<name>public Releases</name>
<layout>default</layout>
<url> http://nexus.liuyingguang.cn:8081/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-core</artifactId>
<version>4.10.1</version>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-analyzers-common</artifactId>
<version>4.10.1</version>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-queryparser</artifactId>
<version>4.10.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-vfs2</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
</dependencies>
<build>
<finalName>testLucene</finalName>
</build>
</project>
|
package com.search.lucene;
import java.io.File;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.junit.Before;
import org.junit.Test;
public class IndexFile {
protected String[] ids={"1", "2"};
protected String[] content={"Amsterdam has lost of add cancals", "i love add this girl"};
protected String[] city={"Amsterdam", "Venice"};
private Directory dir;
/**
* 初始添加文档
* @throws Exception
*/
@Test
public void init() throws Exception {
String pathFile="D://lucene/index";
dir=FSDirectory.open(new File(pathFile));
IndexWriter writer=getWriter();
for(int i=0; i < ids.length; i++) {
Document doc=new Document();
doc.add(new StringField("id", ids[i], Store.YES));
doc.add(new TextField("content", content[i], Store.YES));
doc.add(new StringField("city", city[i], Store.YES));
writer.addDocument(doc);
}
System.out.println("init ok?");
writer.close();
}
/**
* 获得IndexWriter对象
* @return
* @throws Exception
*/
public IndexWriter getWriter() throws Exception {
Analyzer analyzer=new StandardAnalyzer(Version.LUCENE_40);
IndexWriterConfig iwc=new IndexWriterConfig(Version.LUCENE_40, analyzer);
return new IndexWriter(dir, iwc);
}
}
|
package com.search.lucene;
import java.io.File;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.junit.Test;
public class IndexSearch {
/**
* 查询
* @throws Exception
*/
@Test
public void search() throws Exception {
String filePath="D://lucene/index";
Directory dir=FSDirectory.open(new File(filePath));
IndexReader reader=DirectoryReader.open(dir);
IndexSearcher searcher=new IndexSearcher(reader);
Term term=new Term("content", "add");
TermQuery query=new TermQuery(term);
TopDocs topdocs=searcher.search(query, 5);
ScoreDoc[] scoreDocs=topdocs.scoreDocs;
System.out.println("查询结果总数---" + topdocs.totalHits+"最大的评分--"+topdocs.getMaxScore());
for(int i=0; i < scoreDocs.length; i++) {
int doc = scoreDocs[i].doc;
Document document = searcher.doc(doc);
System.out.println("content===="+document.get("content"));
System.out.println("id--" + scoreDocs[i].doc + "---scors--" + scoreDocs[i].score+"---index--"+scoreDocs[i].shardIndex);
}
reader.close();
}
}
|