Lucene(3.5)のいくつかのクエリー
いくつかのクエリの使用方法は次のコードです.
内
数値範囲クエリー:索引時のフィールドは次のように保存されます.
new NumericField(name, Field.Store.YES(NO), true(false).setIntValue( num );
Query numericRangeQuery = NumericRangeQuery.newIntRange("size", 20, 150, true, true);
時間範囲クエリーとは、Dateがlong型変数に基づいて構築されたことを知っているため、数値範囲クエリーです.
だから使い方は同じですが、入力した日付フォーマットをlongに変換する必要があります.
内
数値範囲クエリー:索引時のフィールドは次のように保存されます.
new NumericField(name, Field.Store.YES(NO), true(false).setIntValue( num );
Query numericRangeQuery = NumericRangeQuery.newIntRange("size", 20, 150, true, true);
時間範囲クエリーとは、Dateがlong型変数に基づいて構築されたことを知っているため、数値範囲クエリーです.
だから使い方は同じですが、入力した日付フォーマットをlongに変換する必要があります.
:
name: java
date:1334494024083
size: 148
content:
Hello
Lucene ( ) Lucene in action
the quick brown fox jumped over the lazy dog
public class QueryTest {
@Test
public void testTermQuery() throws Exception {
// Term t = new Term("content", " ");
Term term = new Term("size", "53");
TermQuery termQuery = new TermQuery(term);
doSearch(termQuery);
}
@Test
public void testRangeQuery() throws Exception {
// , , 20 150 ,
// Query termRangeQuery = new TermRangeQuery("size", "020", "150", true, true);
// 20 0 ( 020) 20 150 , , 0
//Lucene NumberTools.longToString(num),
// api, NumberTools NumericUtils
// ,
//
// Query termRangeQuery = new TermRangeQuery("size", NumericUtils.longToPrefixCoded(20L), NumericUtils.longToPrefixCoded(150L), true, true);
// new new , api:
//You create a new NumericRangeQuery with the static factory methods, eg:
//Query q = NumericRangeQuery.newFloatRange("weight", 0.03f, 0.10f, true, true);
// , : Field, NumericRangeQuery, NumericField:
//api:
//A Query that matches numeric values within a specified range. To use this,
//you must first index the numeric values using NumericField (expert: NumericTokenStream).
//If your terms are instead textual, you should use TermRangeQuery.
//NumericRangeFilter is the filter equivalent of this query.
//numericField Field :
//NumericField size = new NumericField("size", Field.Store.YES, true);
//size.setLongValue(file.length());
Query numericRangeQuery = NumericRangeQuery.newLongRange("size", 20L, 150L, true, true);
doSearch(numericRangeQuery);
}
@Test
public void testPrefixQuery() throws Exception {
//Term term = new Term("content", " ");// “ ” ,
Term term = new Term("name", "j");// “ ” ,
PrefixQuery prefixQuery = new PrefixQuery(term);
doSearch(prefixQuery);
}
@Test
public void testBooleanQuery() throws Exception {
//query 1
// min_time max_time
long min_time = 1334485513724L - 1000L;
long max_time = 1334485513724L + 1000L;
Query num_range_query = NumericRangeQuery.newLongRange("date", min_time, max_time, true, true);
//query 2
Query termQuery = new TermQuery(new Term("name", "test"));// test
//query 3
Query prefixQuery = new PrefixQuery(new Term("name", "j"));
BooleanQuery booleanQuery = new BooleanQuery();
booleanQuery.add(num_range_query, BooleanClause.Occur.MUST);//
booleanQuery.add(termQuery, BooleanClause.Occur.MUST_NOT);// name=test
booleanQuery.add(prefixQuery, BooleanClause.Occur.SHOULD);//
doSearch(booleanQuery);
}
@Test
public void testPhraseQuery() throws Exception {
PhraseQuery phraseQuery = new PhraseQuery();
// add(term) ?
// Query
//Query phraseQuery = new PhraseQuery();
String[] strs = {"quick", "brown", "jumped", "lazy"};
//the quick brown fox jumped over the lazy dog
//quick--1->brown, brown--2->jumped, jumped--3->lazy, slop 3
// slop ( ), brown-->jumped jumped-->brown slop
// 《Lucene ( )lucene in action》 P92
phraseQuery.setSlop(3);
for(String s: strs){
phraseQuery.add(new Term("content", s));
}
doSearch(phraseQuery);
}
/**
* ,
* @throws Exception
*/
@Test
public void testWildcardQuery() throws Exception {
//Term term = new Term("name", "ja?a");//?
Term term = new Term("content", " * ");//*
Query wildcardQuery = new WildcardQuery(new Term("name", "j*a"));
doSearch(wildcardQuery);
}
/**
*
* @throws Exception
*/
@Test
public void testFuzzyQuery() throws Exception {
Term term = new Term("name", "jeav");// jeav
Query fuzzyQuery = new FuzzyQuery(term);
doSearch(fuzzyQuery);
}
private void doSearch(Query query) throws Exception{
Directory dir = FSDirectory.open(new File("E:/lucene/index"));
IndexReader reader = IndexReader.open(dir);
IndexSearcher searcher = new IndexSearcher(reader);
TopDocs hits = searcher.search(query, 100);
System.out.println(" " + hits.totalHits + " ");
ScoreDoc[] docs = hits.scoreDocs;
for(int i = 0; i < docs.length; i++){
int docId = docs[i].doc;
Document doc = searcher.doc(docId);
System.out.println("name: " + doc.get("name"));
System.out.println("size: " + doc.get("size"));
System.out.println("content: " + doc.get("content"));
}
searcher.close();
}
}