Lucene 的四大索引查询
清单1:使用布尔操作符
Java代码
- //Test boolean operator
- blic void testOperator(String indexDirectory) throws Exception{
- Directory dir = FSDirectory.getDirectory(indexDirectory,false);
- IndexSearcher indexSearcher = new IndexSearcher(dir);
- String[] searchWords = {"Java AND Lucene", "Java NOT Lucene", "Java OR Lucene",
- "+Java +Lucene", "+Java -Lucene"};
- Analyzer language = new StandardAnalyzer();
- Query query;
- for(int i = 0; i < searchWords.length; i++){
- query = QueryParser.parse(searchWords[i], "title", language);
- Hits results = indexSearcher.search(query);
- System.out.println(results.length() + "search results for query " + searchWords[i]);
- }
域搜索(Field Search)
Lucene 支持域搜索,你可以指定一次查询是在哪些域(Field)上进行。例如,如果索引的文档包含两个域,Title 和 Content,你就可以使用查询 “Title: Lucene AND Content: Java” 来返回所有在 Title 域上包含 Lucene 并且在 Content 域上包含 Java 的文档。清单 2 显示了如何利用 Lucene 的 API 来实现域搜索。
清单2:实现域搜索
Java代码
- //Test field search
- public void testFieldSearch(String indexDirectory) throws Exception{
- Directory dir = FSDirectory.getDirectory(indexDirectory,false);
- IndexSearcher indexSearcher = new IndexSearcher(dir);
- String searchWords = "title:Lucene AND content:Java";
- Analyzer language = new StandardAnalyzer();
- Query query = QueryParser.parse(searchWords, "title", language);
- Hits results = indexSearcher.search(query);
- System.out.println(results.length() + "search results for query " + searchWords);
- }
-
通配符搜索(Wildcard Search)
Lucene 支持两种通配符:问号(?)和星号(*)。你可以使用问号(?)来进行单字符的通配符查询,或者利用星号(*)进行多字符的通配符查询。例如,如果你想搜索 tiny 或者 tony,你就可以使用查询语句 “t?ny”;如果你想查询 Teach, Teacher 和 Teaching,你就可以使用查询语句 “Teach*”。清单3 显示了通配符查询的过程。
清单3:进行通配符查询
Java代码
- //Test wildcard search
- public void testWildcardSearch(String indexDirectory)throws Exception{
- Directory dir = FSDirectory.getDirectory(indexDirectory,false);
- IndexSearcher indexSearcher = new IndexSearcher(dir);
- String[] searchWords = {"tex*", "tex?", "?ex*"};
- Query query;
- for(int i = 0; i < searchWords.length; i++){
- query = new WildcardQuery(new Term("title",searchWords[i]));
- Hits results = indexSearcher.search(query);
- System.out.println(results.length() + "search results for query " + searchWords[i]);
- }
- }
-
模糊查询
Lucene 提供的模糊查询基于编辑距离算法(Edit distance algorithm)。你可以在搜索词的尾部加上字符 ~ 来进行模糊查询。例如,查询语句 “think~” 返回所有包含和 think 类似的关键词的文档。清单 4 显示了如果利用 Lucene 的 API 进行模糊查询的代码。
清单4:实现模糊查询
Java代码
- //Test fuzzy search
- public void testFuzzySearch(String indexDirectory)throws Exception{
- Directory dir = FSDirectory.getDirectory(indexDirectory,false);
- IndexSearcher indexSearcher = new IndexSearcher(dir);
- String[] searchWords = {"text", "funny"};
- Query query;
- for(int i = 0; i < searchWords.length; i++){
- query = new FuzzyQuery(new Term("title",searchWords[i]));
- Hits results = indexSearcher.search(query);
- System.out.println(results.length() + "search results for query " + searchWords[i]);
- }
- }
-
范围搜索(Range Search)
范围搜索匹配某个域上的值在一定范围的文档。例如,查询 “age:[18 TO 35]” 返回所有 age 域上的值在 18 到 35 之间的文档。清单5显示了利用 Lucene 的 API 进行返回搜索的过程。
清单5:测试范围搜索
Java代码
- //Test range search
- public void testRangeSearch(String indexDirectory)throws Exception{
- Directory dir = FSDirectory.getDirectory(indexDirectory,false);
- IndexSearcher indexSearcher = new IndexSearcher(dir);
- Term begin = new Term("birthDay","20000101");
- Term end = new Term("birthDay","20060606");
- Query query = new RangeQuery(begin,end,true);
- Hits results = indexSearcher.search(query);
- System.out.println(results.length() + "search results is returned");
- }
本文转自张昺华-sky博客园博客,原文链接:http://www.cnblogs.com/bonelee/p/6601943.html ,如需转载请自行联系原作者