📄 lucenesearchquerytype.java
字号:
Field.Store.YES,Field.Index.TOKENIZED);
document.add(field_content);
TextIndex.addDocument(document);
}
TextIndex.optimize();
TextIndex.close();
}catch (IOException e) {
e.printStackTrace();
}
System.out.println("Index success");
}
/*================================================================
* 名 称:IndexBuilder2
* 功 能:构造磁盘索引,添加内容到指定目录,为后续检索查询做好准备。
===============================================================*/
public static void IndexBuilder2(){
try {
Analyzer TextAnalyzer = new SimpleAnalyzer();
IndexWriter TextIndex = new IndexWriter(Dest_Index_Path2,TextAnalyzer,true);
TextIndex.setUseCompoundFile(true);
for(int i = 0; i < 5 ; i++){
Document document = new Document();
Field field_id = new Field("id", keywords2[i],
Field.Store.YES,Field.Index.UN_TOKENIZED);
document.add(field_id);
Field field_content = new Field("content", textdetail2[i],
Field.Store.YES,Field.Index.TOKENIZED);
document.add(field_content);
TextIndex.addDocument(document);
}
TextIndex.optimize();
TextIndex.close();
}catch (IOException e) {
e.printStackTrace();
}
System.out.println("Index success");
}
/*================================================================
* 名 称:DataIndexBuilder
* 功 能:构造数据类型值的磁盘索引,添加内容到指定目录,为后续检索查询做好准备。
===============================================================*/
public static void DataIndexBuilder(){
try {
Integer nNum;
Analyzer TextAnalyzer = new SimpleAnalyzer();
IndexWriter TextIndex = new IndexWriter(Dest_Index_Path,TextAnalyzer,true);
TextIndex.setUseCompoundFile(true);
System.out.println("Index Value:");
for(int i = 100; i < 160 ; i++){
Document document = new Document();
nNum = i;
String sortvalue = nNum.toString();
System.out.print(sortvalue);
System.out.print(" ");
if( i%20 == 19){
System.out.println("");
}
Field field_data = new Field("numval", sortvalue,
Field.Store.YES,Field.Index.UN_TOKENIZED);
document.add(field_data);
TextIndex.addDocument(document);
}
TextIndex.optimize();
TextIndex.close();
}catch (IOException e) {
e.printStackTrace();
}
System.out.println("Index success");
}
/*================================================================
* 名 称:FuzzyQueryTest
* 功 能:构造模糊检索查询器,对指定的目录进行查询,找到指定的值,并输出相应结果。
===============================================================*/
public static void FuzzyQueryTest(){
try {
IndexSearcher searcher = new IndexSearcher(Dest_Index_Path);
// Term term = new Term("content","登录");
// Term term = new Term("content","记者");
Term term = new Term("content","记录");
FuzzyQuery query = new FuzzyQuery(term,0.1f,1);
System.out.println(query.toString());
Hits hits = searcher.search(query);
System.out.println("Search result:");
for(int i=0; i < hits.length(); i++)
{
System.out.println(hits.doc(i));
}
}catch (IOException e) {
e.printStackTrace();
}
System.out.println("Search success");
}
/*================================================================
* 名 称:FuzzyQueryParserTest
* 功 能:测试QueryParser生成FuzzyQuery对象,并利用检索查询器,对指定的目录进行查询,
* 找到指定的值,并输出相应结果。
===============================================================*/
public static void FuzzyQueryParserTest(){
try {
IndexSearcher searcher = new IndexSearcher(Dest_Index_Path);
String searchWords = "记录一~0.1";
Analyzer analyzer = new SimpleAnalyzer();
QueryParser parser = new QueryParser("content",analyzer);
try{
Query query = parser.parse(searchWords);
System.out.println(query.toString());
System.out.println(query.getClass());
Hits hits = searcher.search(query);
System.out.println("Search result:");
for(int i=0; i < hits.length(); i++)
{
System.out.println(hits.doc(i));
System.out.println(hits.doc(i).getField("id"));
}
} catch(ParseException e1){
e1.printStackTrace();
}
}catch (IOException e) {
e.printStackTrace();
}
System.out.println("Search success");
}
/*================================================================
* 名 称:WildcardQueryTest
* 功 能:构造通配符检索查询器,对指定的目录进行查询,找到指定的值,并输出相应结果。
===============================================================*/
public static void WildcardQueryTest(){
try {
IndexSearcher searcher = new IndexSearcher(Dest_Index_Path);
Term term = new Term("content","记*");
WildcardQuery query = new WildcardQuery(term);
System.out.println(query.toString());
Hits hits = searcher.search(query);
System.out.println("Search result:");
for(int i=0; i < hits.length(); i++){
System.out.println(hits.doc(i));
}
}catch (IOException e) {
e.printStackTrace();
}
System.out.println("Search success");
}
/*================================================================
* 名 称:WildcardQueryParserTest
* 功 能:测试QueryParser生成WildcardQuery对象,并利用检索查询器,对指定的目录进行查询,
* 找到指定的值,并输出相应结果。
===============================================================*/
public static void WildcardQueryParserTest(){
try {
IndexSearcher searcher = new IndexSearcher(Dest_Index_Path);
String searchWords = "0*1";
Analyzer analyzer = new SimpleAnalyzer();
QueryParser parser = new QueryParser("id",analyzer);
try{
Query query = parser.parse(searchWords);
System.out.println(query.toString());
System.out.println(query.getClass());
Hits hits = searcher.search(query);
System.out.println("Search result:");
for(int i=0; i < hits.length(); i++)
{
System.out.println(hits.doc(i));
System.out.println(hits.doc(i).getField("id"));
}
} catch(ParseException e1){
e1.printStackTrace();
}
}catch (IOException e) {
e.printStackTrace();
}
System.out.println("Search success");
}
/*================================================================
* 名 称:MultiFeildQueryParserTest
* 功 能:测试QueryParser生成MultiFeildQuery对象,并利用检索查询器,对指定的目录进行查询,
* 找到指定的值,并输出相应结果。
===============================================================*/
public static void MultiFeildQueryParserTest(){
try {
String word_list[] ={"002","记录"};
String feild_list[] ={"id","content"};
try{
IndexSearcher searcher = new IndexSearcher(Dest_Index_Path);
BooleanQuery boolquery = new BooleanQuery();
Analyzer analyzer = new SimpleAnalyzer();
for(int m=0;m<2;m++)
{
QueryParser parser = new QueryParser(feild_list[m],analyzer);
Query query = parser.parse(word_list[m]);
boolquery.add(query, BooleanClause.Occur.SHOULD);
}
//Query query = parser.parse(searchWords);
System.out.println(boolquery.toString());
System.out.println(boolquery.getClass());
Hits hits = searcher.search(boolquery);
System.out.println("Search result:");
for(int i=0; i < hits.length(); i++)
{
System.out.println(hits.doc(i));
System.out.println(hits.doc(i).getField("id"));
}
} catch(ParseException e1){
e1.printStackTrace();
}
}catch (IOException e) {
e.printStackTrace();
}
System.out.println("Search success");
}
/*================================================================
* 名 称:MultiFeildQueryParserTest
* 功 能:测试QueryParser生成MultiFeildQuery对象,并利用检索查询器,对指定的目录进行查询,
* 找到指定的值,并输出相应结果。
===============================================================*/
public static void MultiFeildTermTest(){
try {
String word_list[] ={"002","记录"};
String feild_list[] ={"id","content"};
try{
IndexSearcher searcher = new IndexSearcher(Dest_Index_Path);
BooleanQuery boolquery = new BooleanQuery();
Analyzer analyzer = new SimpleAnalyzer();
for(int m=0;m<2;m++)
{
QueryParser parser = new QueryParser(feild_list[m],analyzer);
Query query = parser.parse(word_list[m]);
boolquery.add(query, BooleanClause.Occur.SHOULD);
}
//Query query = parser.parse(searchWords);
System.out.println(boolquery.toString());
System.out.println(boolquery.getClass());
Hits hits = searcher.search(boolquery);
System.out.println("Search result:");
for(int i=0; i < hits.length(); i++)
{
System.out.println(hits.doc(i));
System.out.println(hits.doc(i).getField("id"));
}
} catch(ParseException e1){
e1.printStackTrace();
}
}catch (IOException e) {
e.printStackTrace();
}
System.out.println("Search success");
}
/*================================================================
* 名 称:MultiSearcherQueryTest
* 功 能:构造检索查询器,对指定的目录进行查询,找到指定的值,并输出相应结果。
===============================================================*/
public static void MultiSearcherQueryTest(){
try {
IndexSearcher searcher1 = new IndexSearcher(Dest_Index_Path);
IndexSearcher searcher2 = new IndexSearcher(Dest_Index_Path2);
IndexSearcher[] searchers = {searcher1,searcher2};
MultiSearcher multisearcher = new MultiSearcher(searchers);
String searchWords = "二";
Term t = new Term("id","002");
Query query = new TermQuery(t);
System.out.println(query.toString());
Hits hits = multisearcher.search(query);
System.out.println("Search result:");
for(int i=0; i < hits.length(); i++)
{
System.out.println(hits.doc(i));
System.out.println(hits.doc(i).getField("id"));
}
}catch (IOException e) {
e.printStackTrace();
}
System.out.println("Search success");
}
/*================================================================
* 名 称:main
* 功 能:测试Lucene索引建立和检索查询功能。
===============================================================*/
public static void main(String[] args) {
//IndexBuilder();
//TermQueryTest();
//TermQueryParserTest();
//DataIndexBuilder();
//RangeQueryTest();
//RangeQueryParserTest();
//IndexBuilder();
//BooleanQueryTest();
//BooleanQueryParserTest();
//IndexBuilder();
//PrefixQueryTest();
//PrefixQueryParserTest();
//IndexBuilder();
//PhraseQueryTest();
//PhraseQueryParserTest();
//IndexBuilder();
//FuzzyQueryTest();
//FuzzyQueryParserTest();
//IndexBuilder();
//WildcardQueryTest();
//WildcardQueryParserTest();
//IndexBuilder();
//MultiFeildQueryParserTest();
IndexBuilder();
IndexBuilder2();
MultiSearcherQueryTest();
System.out.println("Test success");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -