Lucene學習之二搜尋(計分Score)

2021-06-02 06:04:35 字數 2333 閱讀 7126

// from chapter 3

public class scoretest extends testcase

public void teardown() throws exception

public void test******() throws exception );

indexsearcher searcher = new indexsearcher(directory);

searcher.setsimilarity(new ******similarity());

query query = new termquery(new term("contents", "x"));

explanation explanation = searcher.explain(query, 0);

system.out.println(explanation);

topdocs matches = searcher.search(query, 10);

assertequals(1, matches.totalhits);

assertequals(1f, matches.scoredocs[0].score, 0.0);

searcher.close();

} private void indexsinglefielddocs(field fields) throws exception

writer.optimize();

writer.close();

} public void testwildcard() throws exception );

indexsearcher searcher = new indexsearcher(directory);

query query = new wildcardquery(new term("contents", "?ild*")); //#a

topdocs matches = searcher.search(query, 10);

assertequals("child no match", 3, matches.totalhits);

assertequals("score the same", matches.scoredocs[0].score,

matches.scoredocs[1].score, 0.0);

assertequals("score the same", matches.scoredocs[1].score,

matches.scoredocs[2].score, 0.0);

searcher.close();

} /*

#a construct wildcardquery using term

*/public void testfuzzy() throws exception );

indexsearcher searcher = new indexsearcher(directory);

query query = new fuzzyquery(new term("contents", "wuzza"));

topdocs matches = searcher.search(query, 10);

assertequals("both close enough", 2, matches.totalhits);

asserttrue("wuzzy closer than fuzzy",

matches.scoredocs[0].score != matches.scoredocs[1].score);

document doc = searcher.doc(matches.scoredocs[0].doc);

assertequals("wuzza bear", "wuzzy", doc.get("contents"));

searcher.close();

} public static class ******similarity extends similarity

public float querynorm(float sumofsquaredweights)

public float tf(float freq)

public float sloppyfreq(int distance)

public float idf(vector terms, searcher searcher)

public float idf(int docfreq, int numdocs)

public float coord(int overlap, int maxoverlap)

}}

Lucene學習總結之二 Lucene的總體架構

lucene總的來說是 在lucene in action中,lucene 的構架和過程如下圖,說明lucene是有索引和搜尋的兩個過程,包含索引建立,索引,搜尋三個要點。讓我們更細一些看lucene的各元件 那麼如何應用這些元件呢?讓我們再詳細到對lucene api 的呼叫實現索引和搜尋過程。搜...

Lucene學習總結之二 Lucene的總體架構

lucene總的來說是 在lucene in action中,lucene 的構架和過程如下圖,說明lucene 是有索引和搜尋的兩個過程,包含索引建立,索引,搜尋三個要點。讓我們更細一些看lucene的各元件 那麼如何應用這些元件呢?讓我們再詳細到對lucene api 的呼叫實現索引和搜尋過程。...

Lucene學習總結之二 Lucene的總體架構

lucene總的來說是 在lucene in action中,lucene 的構架和過程如下圖,說明lucene是有索引和搜尋的兩個過程,包含索引建立,索引,搜尋三個要點。讓我們更細一些看lucene的各元件 那麼如何應用這些元件呢?讓我們再詳細到對lucene api 的呼叫實現索引和搜尋過程。搜...