全文检索lucene学习笔记(四)

关于查询

1.TermQuery:

package com.lucene.search;

import java.io.File;
import java.io.IOException;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

public class TermQuerySearcher {
 
 public static void main(String[] args) throws IOException, ParseException {

  File indexDir = new File("C://test//index");
  Directory fsDir  = FSDirectory.getDirectory(indexDir);
  IndexSearcher searcher = new IndexSearcher(fsDir);
  
  String q = "ERROR";
  Term t = new Term("contents", q.toLowerCase());
  Query query = new TermQuery(t);
//  QueryParser parser = new QueryParser("contents", new StandardAnalyzer());
//  Query query = parser.parse(q);
  Hits hits = searcher.search(query);
  
  System.out.println("共有" + searcher.maxDoc() + "条索引,命中" + hits.length() + "条");
  for(int i=0; i<hits.length(); i++) {
   int DocId = hits.id(i);
   String DocPath = hits.doc(i).get("filename");
   System.out.println(DocId + ":" + DocPath);
  }
 }
}

2.RangeQuery:

package com.lucene.search;

import java.io.File;
import java.io.IOException;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.RangeQuery;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

public class RangeQuerySearcher {

 public static void main(String[] args) throws IOException, ParseException {

  File indexDir = new File("C://test//index");
  Directory fsDir  = FSDirectory.getDirectory(indexDir);
  IndexSearcher searcher = new IndexSearcher(fsDir);
  String b = "2007";
  String e = "2008";
  Term begin = new Term("contents", b);
  Term end = new Term("contents", e);
  RangeQuery query = new RangeQuery(begin, end, true);
//  QueryParser parser = new QueryParser("contents", new StandardAnalyzer());
//  Query query = parser.parse("[2007 TO 2008]");
  Hits hits = searcher.search(query);
  
  System.out.println("共有" + searcher.maxDoc() + "条索引,命中" + hits.length() + "条");
  for(int i=0; i<hits.length(); i++) {
   int DocId = hits.id(i);
   String DocPath = hits.doc(i).get("filename");
   System.out.println(DocId + ":" + DocPath);
  }
 }
}

3.BooleanQuery:

package com.lucene.search;

import java.io.File;
import java.io.IOException;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.RangeQuery;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

public class BooleanQuerySearcher {

 public static void main(String[] args) throws IOException, ParseException {

  File indexDir = new File("C://test//index");
  Directory fsDir  = FSDirectory.getDirectory(indexDir);
  IndexSearcher searcher = new IndexSearcher(fsDir);
  
  Query tq = new TermQuery(new Term("contents", "ERROR".toLowerCase()));
  RangeQuery rq = new RangeQuery(new Term("contents", "2007"), new Term("contents", "2008"), true);
  BooleanQuery query = new BooleanQuery();
  query.add(tq, BooleanClause.Occur.MUST);
  query.add(rq, BooleanClause.Occur.MUST);
//  QueryParser parser = new QueryParser("contents", new StandardAnalyzer());
//  Query query = parser.parse("error AND [2007 TO 2008]");
  Hits hits = searcher.search(query);
  
  System.out.println("共有" + searcher.maxDoc() + "条索引,命中" + hits.length() + "条");
  for(int i=0; i<hits.length(); i++) {
   int DocId = hits.id(i);
   String DocPath = hits.doc(i).get("filename");
   System.out.println(DocId + ":" + DocPath);
  }
 }
}

4.PrefixQuery

package com.lucene.search;

import java.io.File;
import java.io.IOException;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.PrefixQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

public class PrefixQuerySearcher {

 public static void main(String[] args) throws IOException, ParseException {

  File indexDir = new File("C://test//index");
  Directory fsDir  = FSDirectory.getDirectory(indexDir);
  IndexSearcher searcher = new IndexSearcher(fsDir);
  
  String q = "20";
  Term t = new Term("contents", q.toLowerCase());
  PrefixQuery query = new PrefixQuery(t);
//  QueryParser parser = new QueryParser("contents", new StandardAnalyzer());
//  Query query = parser.parse(q + "*");
  Hits hits = searcher.search(query);
  
  System.out.println("共有" + searcher.maxDoc() + "条索引,命中" + hits.length() + "条");
  for(int i=0; i<hits.length(); i++) {
   int DocId = hits.id(i);
   String DocPath = hits.doc(i).get("filename");
   System.out.println(DocId + ":" + DocPath);
  }
 }
}

5.PhraseQuery

package com.lucene.search;

import java.io.File;
import java.io.IOException;

import org.apache.lucene.index.Term;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.PhraseQuery;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

public class PhraseQuerySearcher {

 public static void main(String[] args) throws IOException {
  
  File indexDir = new File("C://test//index");
  Directory fsDir  = FSDirectory.getDirectory(indexDir);
  IndexSearcher searcher = new IndexSearcher(fsDir);
  
  String[] phrase = {"ERROR".toLowerCase(), "TEST".toLowerCase()};
  PhraseQuery query = new PhraseQuery();
  query.setSlop(1);
  for (int i = 0; i < phrase.length; i++) {
   query.add(new Term("contents", phrase[i]));
  }
  Hits hits = searcher.search(query);
  
  System.out.println("共有" + searcher.maxDoc() + "条索引,命中" + hits.length() + "条");
  for(int i=0; i<hits.length(); i++) {
   int DocId = hits.id(i);
   String DocPath = hits.doc(i).get("filename");
   System.out.println(DocId + ":" + DocPath);
  }
 }
}

6.WildcardQuery

package com.lucene.search;

import java.io.File;
import java.io.IOException;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.WildcardQuery;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

public class WildcardQuerySearcher {

 public static void main(String[] args) throws IOException, ParseException {
  
  File indexDir = new File("C://test//index");
  Directory fsDir  = FSDirectory.getDirectory(indexDir);
  IndexSearcher searcher = new IndexSearcher(fsDir);
  
  Query query = new WildcardQuery(new Term("contents", "?ER*".toLowerCase()));
//====================================================================================
//  QueryParser parser = new QueryParser("contents", new StandardAnalyzer());
//  Query query = parser.parse("?ER*".toLowerCase());
//====================================================================================
  Hits hits = searcher.search(query);
  
  System.out.println("共有" + searcher.maxDoc() + "条索引,命中" + hits.length() + "条");
  for(int i=0; i<hits.length(); i++) {
   int DocId = hits.id(i);
   String DocPath = hits.doc(i).get("filename");
   System.out.println(DocId + ":" + DocPath);
  }
 }
}

7.FuzzyQuery

package com.lucene.search;

import java.io.File;
import java.io.IOException;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.FuzzyQuery;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

public class FuzzyQuerySearcher {

 public static void main(String[] args) throws IOException, ParseException {
  
  File indexDir = new File("C://test//index");
  Directory fsDir  = FSDirectory.getDirectory(indexDir);
  IndexSearcher searcher = new IndexSearcher(fsDir);
  
  Query query = new FuzzyQuery(new Term("contents", "XERRORX".toLowerCase()));
  //QueryParser parser = new QueryParser("contents", new StandardAnalyzer());
  //Query query = parser.parse("ERROR~".toLowerCase());
  Hits hits = searcher.search(query);
  
  System.out.println("共有" + searcher.maxDoc() + "条索引,命中" + hits.length() + "条");
  for(int i=0; i<hits.length(); i++) {
   int DocId = hits.id(i);
   String DocPath = hits.doc(i).get("filename");
   System.out.println(DocId + ":" + DocPath);
  }
 }
}

参考资料:http://www.dlog.cn/nicholascoder/diary/8881

你可能感兴趣的:(全文检索lucene学习笔记(四))