lucene根据数据库记录id删除索引无效?
Analyzeranal=newIKAnalyzer(true);MultiFieldQueryParserparser=newMultiFieldQueryParser...
Analyzer anal=new IKAnalyzer(true);
MultiFieldQueryParser parser = new MultiFieldQueryParser(Version.LUCENE_36, new String[] {"id"}, anal);
Query query = parser.parse(id);
Directory dir = FSDirectory.open(new File(index));
IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_36,anal);
IndexWriter indexWriter = new IndexWriter(dir, config);
indexWriter.deleteDocuments(query);
indexWriter.commit();
indexWriter.close();
删除后还是可以查到啊 没有效果啊,怎么弄 ?》 展开
MultiFieldQueryParser parser = new MultiFieldQueryParser(Version.LUCENE_36, new String[] {"id"}, anal);
Query query = parser.parse(id);
Directory dir = FSDirectory.open(new File(index));
IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_36,anal);
IndexWriter indexWriter = new IndexWriter(dir, config);
indexWriter.deleteDocuments(query);
indexWriter.commit();
indexWriter.close();
删除后还是可以查到啊 没有效果啊,怎么弄 ?》 展开
1个回答
展开全部
看一下是不是建索引的问题(Field选取的不对,一般id字段用Field.Index.NOT_ANALYZED),
我用lucene 3.6.2, IKAnalyzer2012_u6.jar测试的
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.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
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 LucenDemo {
public static void main(String[] args) {
String id = "456";
createIndex();
printTotalHits(id);
deleteById(id);
printTotalHits(id);
}
static String indexPath = "E:\\lucene-demo-index\\";
public static void printTotalHits(String id) {
try {
Analyzer anal = new IKAnalyzer(true);
MultiFieldQueryParser parser = new MultiFieldQueryParser(Version.LUCENE_36, new String[] {"id"}, anal);
Query query = parser.parse(id);
Directory dir = FSDirectory.open(new File(indexPath));
IndexReader indexReader = IndexReader.open(dir);
IndexSearcher searcher = new IndexSearcher(indexReader);
System.out.println(searcher.search(query, 10).totalHits);
indexReader.close();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void deleteById(String id) {
try {
Analyzer anal = new IKAnalyzer(true);
MultiFieldQueryParser parser = new MultiFieldQueryParser(Version.LUCENE_36, new String[] {"id"}, anal);
Query query = parser.parse(id);
Directory dir = FSDirectory.open(new File(indexPath));
IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_36,anal);
IndexWriter indexWriter = new IndexWriter(dir, config);
indexWriter.deleteDocuments(query);
indexWriter.commit();
indexWriter.close();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void createIndex() {
try {
Analyzer analyzer = new IKAnalyzer(true);
Directory dir = FSDirectory.open(new File(indexPath));
IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_36, analyzer);
IndexWriter indexWriter = new IndexWriter(dir, config);
Document doc1 = new Document();
doc1.add(new Field("id", false, "123", Field.Store.YES, Field.Index.NOT_ANALYZED, Field.TermVector.NO));
indexWriter.addDocument(doc1);
Document doc2 = new Document();
doc2.add(new Field("id", false, "456", Field.Store.YES, Field.Index.NOT_ANALYZED, Field.TermVector.NO));
indexWriter.addDocument(doc2);
Document doc3 = new Document();
doc3.add(new Field("id", false, "789", Field.Store.YES, Field.Index.NOT_ANALYZED, Field.TermVector.NO));
indexWriter.addDocument(doc3);
indexWriter.commit();
indexWriter.close();
} catch(Exception e) {
e.printStackTrace();
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询