lucene in action

1.  索引——好比字典的索引一样,进行查询时使用

2. Field.Index.NO 则没有索引,则不能被搜索

3. 

 

 

 

第三章 

PhraseQuery 短语查询

按照顺序添加term

 

 

PharseQuery 短语查询的评分:

public class App 
{
    public static void main( String[] args ) throws IOException, ParseException
    {
        Directory dir = new RAMDirectory();
        IndexWriterConfig conf = new IndexWriterConfig(new WhitespaceAnalyzer());
        IndexWriter writer = new IndexWriter(dir, conf);
        Document doc = new Document();
        doc.add(new TextField("text", "lazy dog jump over cat", Field.Store.YES));
        writer.addDocument(doc);
        
        doc = new Document();
        doc.add(new TextField("text", "lazy haha dog lala jump ooo over this cat", Field.Store.YES));
        writer.addDocument(doc);
        
        doc = new Document();
        doc.add(new TextField("text", "dog lazy haha pig lala jump ooo over this cat", Field.Store.YES));
        writer.addDocument(doc);
        
        IndexReader reader = DirectoryReader.open(writer, true);
        IndexSearcher searcher = new IndexSearcher(reader);
        
//      Query query = new MatchAllDocsQuery();
        
        QueryParser parser = new  QueryParser("text", new WhitespaceAnalyzer());
        parser.setDefaultOperator(Operator.AND);
        Query query = parser.parse("\"dog cat\"~5");
        System.out.println("query["+query.toString()+"]");
        
        TopDocs hits = searcher.search(query, 10);
        System.out.println(hits.totalHits);
        ScoreDoc[] docs = hits.scoreDocs;
        for(ScoreDoc scoreDoc : docs){
        	int docID = scoreDoc.doc;
        	float score = scoreDoc.score;
        	Document docu = searcher.doc(docID);
        	System.out.println(docID+" - "+score+" - "+docu.get("text"));
        }
    }
}

  经测试,

  首先,只返回符合条件的查询结果。如:查找slop=2的,那么项间距大于2的不会被检索到。

  其次,短语查询不是布尔查询。它根据匹配所需要的编辑距离来进行评分。返回的结果中,项之间距离越小,评分越高,排序越靠前

    最后,松散的短语查询不需要按照顺序进行匹配,但是SpanNearQuery能够确保按照顺序匹配。

 

 第四章 分析器

同义词过滤器实现:

直观想法是在有同义词的位置插入同义词,位置增量为0.

实现起来和直观想法不一样,因为TokenStream是流。具体实现的思路是:

incrementToken每一次执行打印一个token, 我们应该是在有同义词的token的下一个token打印出需要插入的同义词。

判断有同义词- 》存储到缓存, 下一次循环 -》先判断缓存是否有需要展示的同义词, 有则打印。

 

 

  

  

posted @ 2016-07-12 11:26  搜索技术  阅读(212)  评论(0编辑  收藏  举报