lucene Term查询
查詢demo
Path path = Paths.get(util.Directory.GetAppPath("indexDir"));IndexReader reader = DirectoryReader.open(FSDirectory.open(path));//獲取IndexSearcher對象IndexSearcher indexSearcher = new IndexSearcher(reader);Query query = new TermQuery(new Term("title", "國"));//查詢數(shù)據(jù)TopDocs topDocs = indexSearcher.search(query, 10);ScoreDoc[] docs = topDocs.scoreDocs;for (ScoreDoc scoreDoc : docs) {Document res = indexSearcher.doc(scoreDoc.doc);System.out.println(res.get("title"));}容易犯錯警告!!!
在構(gòu)造Term對象容易大意寫成如下
Query query = new Term("國");則此時利用Term查詢不出來結(jié)果,因?yàn)榇藭r“國”被當(dāng)做成一個域,并沒有檢索內(nèi)容。
切記:利用lucene Term查詢一定要記得將域加上去,用來標(biāo)識搜索詞所在的字段名稱!!!
Query query = new TermQuery(new Term("title", "國"));Term類說明文檔
介紹:Term術(shù)語表示文本中的單詞。這是搜索單位。它由兩個元素組成,一個字符串形式的單詞文本,另一個文本所在字段的名稱。請注意,術(shù)語可能表示的不僅僅是文本字段中的單詞,還包括日期、電子郵件地址、url等。
構(gòu)造方法:
| Term(String?fld) | fld為域,傳入值為空 |
| Term(String?fld,?BytesRef?bytes) | fld為域,值為字節(jié)流類型 |
| Term(String?fld,?BytesRefBuilder?bytesBuilder) | fld為域,值為BytesRefBuilder類型 |
| Term(String?fld,?String?text) | fld為域,值為String類型(最常用) |
Term方法:
-
field
public final?String?field()Returns the field of this term. The field indicates the part of a document which this term came from.
-
text
public final?String?text()Returns the text of this term. In the case of words, this is simply the text of the word. In the case of dates and other types, this is an encoding of the object as a string.
-
toString
public static final?String?toString(BytesRef?termText)Returns human-readable form of the term text. If the term is not unicode, the raw bytes will be printed instead.
-
bytes
public final?BytesRef?bytes()Returns the bytes of this term, these should not be modified.
-
equals
public?boolean?equals(Object?obj)Overrides:
equals?in class?Object
-
hashCode
public?int?hashCode()Overrides:
hashCode?in class?Object
-
compareTo
public final?int?compareTo(Term?other)Compares two terms, returning a negative integer if this term belongs before the argument, zero if this term is equal to the argument, and a positive integer if this term belongs after the argument. The ordering of terms is first by field, then by text.
Specified by:
compareTo?in interface?Comparable<Term>
-
toString
public final?String?toString()Overrides:
toString?in class?Object
-
ramBytesUsed
public?long?ramBytesUsed()Description copied from interface:?Accountable
Return the memory usage of this object in bytes. Negative values are illegal.
說明文檔出處:http://lucene.apache.org/core/8_2_0/core/index.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的lucene Term查询的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【使用注意】特殊中括号[]的特殊json
- 下一篇: spring与junit整合测试