标签:
package com.fxr.demo3; import java.io.IOException; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; 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.TopDocCollector; import org.apache.lucene.search.TopDocs; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; public class TestSearcher { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { Analyzer analyzer = new StandardAnalyzer();//创建标准的分词 String indexDir = "f:/lucenefxrindex"; Directory directory = FSDirectory.getDirectory(indexDir); IndexSearcher indexSearcher = new IndexSearcher(directory); ScoreDoc []hits = null; Term term = new Term("address","shanghai"); TermQuery termQuery = new TermQuery(term); TopDocs topDocs = indexSearcher.search(termQuery, 10); hits= topDocs.scoreDocs; for(int i=0;i<hits.length;i++){ Document document = indexSearcher.doc(hits[i].doc); System.out.println(hits[i].score); System.out.println(document.get("id")); System.out.println(document.get("name")); System.out.println(document.get("address")); } indexSearcher.close(); directory.close(); } }
通配符的查询:
在Lucene中也可以使用通配符查询:"*"是匹配任意长度的字符,"?"是匹配定长的字符。
package com.fxr.demo3; import java.io.IOException; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; 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.TopDocCollector; import org.apache.lucene.search.TopDocs; import org.apache.lucene.search.WildcardQuery; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; public class TestWildcardSearcher { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { Analyzer analyzer = new StandardAnalyzer();//创建标准的分词 String indexDir = "f:/lucenefxrindex"; Directory directory = FSDirectory.getDirectory(indexDir); IndexSearcher indexSearcher = new IndexSearcher(directory); ScoreDoc []hits = null; Term term = new Term("address","z*"); WildcardQuery wildcardQuery = new WildcardQuery(term); TopDocs topDocs = indexSearcher.search(wildcardQuery, 10); hits= topDocs.scoreDocs; for(int i=0;i<hits.length;i++){ Document document = indexSearcher.doc(hits[i].doc); System.out.println(hits[i].score); System.out.println(document.get("id")); System.out.println(document.get("name")); System.out.println(document.get("address")); } indexSearcher.close(); directory.close(); } }
如何把查询结合起来使用?我们在实际需求中有时候需要这样做,这里需要BooleanQuery,BooleanQuery里面有几个参数,参数可以实现几个Query结合起来的使用
package com.fxr.demo3; import java.io.IOException; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.index.Term; import org.apache.lucene.search.BooleanClause; import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.search.TermQuery; import org.apache.lucene.search.TopDocCollector; import org.apache.lucene.search.TopDocs; import org.apache.lucene.search.WildcardQuery; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; public class TestWildcardSearcher { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { Analyzer analyzer = new StandardAnalyzer();//创建标准的分词 String indexDir = "f:/lucenefxrindex"; Directory directory = FSDirectory.getDirectory(indexDir); IndexSearcher indexSearcher = new IndexSearcher(directory); ScoreDoc []hits = null; Term wterm = new Term("address","z*"); WildcardQuery wildcardQuery = new WildcardQuery(wterm); Term tterm = new Term("name","zhangsan"); TermQuery termQuery = new TermQuery(tterm); BooleanQuery booleanQuery = new BooleanQuery(); booleanQuery.add(wildcardQuery, BooleanClause.Occur.MUST);//必须出现 booleanQuery.add(termQuery, BooleanClause.Occur.SHOULD);//或的关系 TopDocs topDocs = indexSearcher.search(wildcardQuery, 10); hits= topDocs.scoreDocs; for(int i=0;i<hits.length;i++){ Document document = indexSearcher.doc(hits[i].doc); System.out.println(hits[i].score); System.out.println(document.get("id")); System.out.println(document.get("name")); System.out.println(document.get("address")); } indexSearcher.close(); directory.close(); } }
指定范围的查询RangeQuery,可以查询区间的数据,
package com.firstproject.testsearch; import java.io.IOException; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.index.Term; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.RangeQuery; 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; public class TestRangeQuery { public static void main(String[] args) throws IOException { Analyzer analyzer=new StandardAnalyzer(); String indexDir = "f:/lucenefxrindex"; Directory dir=FSDirectory.getDirectory(indexDir); IndexSearcher searcher=new IndexSearcher(dir); ScoreDoc [] hits=null; Term beginTerm=new Term("birthday","19820720"); Term endTerm=new Term("birthday","19830130"); RangeQuery query=new RangeQuery(beginTerm,endTerm,true); TopDocs topDocs=searcher.search(query, 2); hits=topDocs.scoreDocs; for(int i=0;i<hits.length;i++){ Document doc=searcher.doc(hits[i].doc); // System.out.print(hits[i].score+" "); System.out.print(doc.get("id")+" "); System.out.print(doc.get("name")+" "); System.out.print(doc.get("address")+" "); System.out.println(doc.get("birthday")+" "); } searcher.close(); dir.close(); } }
标签:
原文地址:http://www.cnblogs.com/aicpcode/p/4300538.html