lucene查询索引之Query子类查询——(七)
0.文档名字:(根据名字索引查询文档)
1. 提取获取InsexSearch 与 处理结果的公共代码
//IndexReader IndexSearcher public IndexSearcher getIndexSearcher() throws Exception{ // 第一步:创建一个Directory对象,也就是索引库存放的位置。 Directory directory = FSDirectory.open(new File("D:\\temp\\index"));// 磁盘 // 第二步:创建一个indexReader对象,需要指定Directory对象。 IndexReader indexReader = DirectoryReader.open(directory); // 第三步:创建一个indexsearcher对象,需要指定IndexReader对象 return new IndexSearcher(indexReader); } //执行查询的结果 public void printResult(IndexSearcher indexSearcher,Query query)throws Exception{ // 第五步:执行查询。 TopDocs topDocs = indexSearcher.search(query, 10); // 第六步:返回查询结果。遍历查询结果并输出。 ScoreDoc[] scoreDocs = topDocs.scoreDocs; for (ScoreDoc scoreDoc : scoreDocs) { int doc = scoreDoc.doc; Document document = indexSearcher.doc(doc); // 文件名称 String fileName = document.get("fileName"); System.out.println(fileName); // 文件内容 String fileContent = document.get("fileContent"); System.out.println(fileContent); // 文件大小 String fileSize = document.get("fileSize"); System.out.println(fileSize); // 文件路径 String filePath = document.get("filePath"); System.out.println(filePath); System.out.println("------------"); } }
2.精准查询:(入门程序的查询索引)
查询名字索引中含有Java的文件(解析语法: fileName:java)
fileName:java
// 精准查询 @Test public void testTermQuery() throws Exception { IndexSearcher indexSearcher = getIndexSearcher(); // 第四步:创建一个TermQuery对象,指定查询的域和查询的关键词。 Query query = new TermQuery(new Term("fileName", "java")); printResult(indexSearcher, query); // 关闭资源 indexSearcher.getIndexReader().close(); }
结果:
java struts - .txt think smiling is as important as sunshine. Smiling is like sunshine because it can make people happy and have a good day. If you aren��t happy, you can smile, and then you will feel happy. Someone may say, ��But I don��t feel happy.�� Then I would say, ��Please smile as you do when you are happy or play wit 309 E:\lucene&solr\searchfiles\java struts - .txt ------------ java struts.txt think smiling is as important as sunshine. Smiling is like sunshine because it can make people happy and have a good day. If you aren��t happy, you can smile, and then you will feel happy. Someone may say, ��But I don��t feel happy.�� Then I would say, ��Please smile as you do when you are happy or play wit 309 E:\lucene&solr\searchfiles\java struts.txt ------------ java 基础.txt think smiling is as important as sunshine. Smiling is like sunshine because it can make people happy and have a good day. If you aren��t happy, you can smile, and then you will feel happy. Someone may say, ��But I don��t feel happy.�� Then I would say, ��Please smile as you do when you are happy or play wit 309 E:\lucene&solr\searchfiles\java 基础.txt ------------ java高级.txt ����java���̳̣��������������� 32 E:\lucene&solr\searchfiles\java高级.txt ------------ java struts springmvc.txt think smiling is as important as sunshine. Smiling is like sunshine because it can make people happy and have a good day. If you aren��t happy, you can smile, and then you will feel happy. Someone may say, ��But I don��t feel happy.�� Then I would say, ��Please smile as you do when you are happy or play wit 309 E:\lucene&solr\searchfiles\java struts springmvc.txt ------------ java struts spring.txt think smiling is as important as sunshine. Smiling is like sunshine because it can make people happy and have a good day. If you aren��t happy, you can smile, and then you will feel happy. Someone may say, ��But I don��t feel happy.�� Then I would say, ��Please smile as you do when you are happy or play wit 309 E:\lucene&solr\searchfiles\java struts spring.txt ------------
3.查询所有
解析语法 *:*
// 查询所有 @Test public void testMatchAllDocsQuery() throws Exception { IndexSearcher indexSearcher = getIndexSearcher(); Query query = new MatchAllDocsQuery(); System.out.println(query); printResult(indexSearcher, query); // 关闭资源 indexSearcher.getIndexReader().close(); }
结果:
*:* 1javaweb .txt this is javaweb dsbadfsabjkfsdf njdfndsj njaj spring 53 E:\lucene&solr\searchfiles\1javaweb .txt ------------ 2.javeSpring .txt my family, because I have a happy family. My father is an English teacher. His name is Jacky. He is thirty-eight. He likes playing basketball. What��s my mother job? Is she a teacher? Yes, you��re right! My mother is very kind and nice, she is thirty-seven. My mother is always laborious work. I love my parents! On Saturday and Sunday, I often go to the library and play the piano, My father go to play basketball. Sometimes, we watch TV and listen to music at home. I love my family. Because I��m very happy to live 518 E:\lucene&solr\searchfiles\2.javeSpring .txt ------------ 2.springMVC.txt is is my room. Near the window there is a desk. I often do my homework at it. You can see some books, some flowers in a vase, a ruler and a pen. On the wall near the desk there is a picture of a cat. There is a clock above the end of my bed. I usually put my shoe under my bed. Of course 287 E:\lucene&solr\searchfiles\2.springMVC.txt ------------ computer.txt ��Computers are changing our life. You can do a lot of things with a computer. Such as, you can use a computer to write articles, watch video CDs, play games and do office work. But the most important use of a computer is to join the Internet.We don��t need to leave home to borrow books from a library or to do shopping in a supermarke 336 E:\lucene&solr\searchfiles\computer.txt ------------ java struts springmvc.txt think smiling is as important as sunshine. Smiling is like sunshine because it can make people happy and have a good day. If you aren��t happy, you can smile, and then you will feel happy. Someone may say, ��But I don��t feel happy.�� Then I would say, ��Please smile as you do when you are happy or play wit 309 E:\lucene&solr\searchfiles\java struts springmvc.txt ------------ java struts - .txt think smiling is as important as sunshine. Smiling is like sunshine because it can make people happy and have a good day. If you aren��t happy, you can smile, and then you will feel happy. Someone may say, ��But I don��t feel happy.�� Then I would say, ��Please smile as you do when you are happy or play wit 309 E:\lucene&solr\searchfiles\java struts - .txt ------------ java struts spring.txt think smiling is as important as sunshine. Smiling is like sunshine because it can make people happy and have a good day. If you aren��t happy, you can smile, and then you will feel happy. Someone may say, ��But I don��t feel happy.�� Then I would say, ��Please smile as you do when you are happy or play wit 309 E:\lucene&solr\searchfiles\java struts spring.txt ------------ java struts.txt think smiling is as important as sunshine. Smiling is like sunshine because it can make people happy and have a good day. If you aren��t happy, you can smile, and then you will feel happy. Someone may say, ��But I don��t feel happy.�� Then I would say, ��Please smile as you do when you are happy or play wit 309 E:\lucene&solr\searchfiles\java struts.txt ------------ java 基础.txt think smiling is as important as sunshine. Smiling is like sunshine because it can make people happy and have a good day. If you aren��t happy, you can smile, and then you will feel happy. Someone may say, ��But I don��t feel happy.�� Then I would say, ��Please smile as you do when you are happy or play wit 309 E:\lucene&solr\searchfiles\java 基础.txt ------------ java高级.txt ����java���̳̣��������������� 32 E:\lucene&solr\searchfiles\java高级.txt ------------
4.根据范围值查询
解析语法: fileSize:{47 TO 200]
NumericRangeQuery.newLongRange("fileSize", 47L, 200L, false, true);
根据范围查询,第一个是域名字,第二个是最小值,第三个参数是最大值,第四个参数是是否包含最小值,第五个参数是是否包含最大值。
Long型后面要加L,不加L默认是int,int转为long是安全的,所以会自动转,能编译通过
浮点数不加F默认是double类型,double转float可能损失精度,因为不会自动转,编译是通不过的
// 根据数值范围查询 @Test public void testNumericRangeQuery() throws Exception { IndexSearcher indexSearcher = getIndexSearcher(); Query query = NumericRangeQuery.newLongRange("fileSize", 47L, 200L, false, true); System.out.println(query); printResult(indexSearcher, query); // 关闭资源 indexSearcher.getIndexReader().close(); }
结果:
fileSize:{47 TO 200] 1javaweb .txt this is javaweb dsbadfsabjkfsdf njdfndsj njaj spring 53 E:\lucene&solr\searchfiles\1javaweb .txt ------------
5.组合条件查询(重点)
(1)查找名字中java必有,struts可有可无的
解析语法: +fileName:java fileName:struts
// 可以组合查询条件 @Test public void testBooleanQuery() throws Exception { IndexSearcher indexSearcher = getIndexSearcher(); BooleanQuery booleanQuery = new BooleanQuery(); Query query1 = new TermQuery(new Term("fileName", "java")); Query query2 = new TermQuery(new Term("fileName", "struts")); // select * from user where id =1 or name = 'safdsa' booleanQuery.add(query1, Occur.MUST); booleanQuery.add(query2, Occur.SHOULD); System.out.println(booleanQuery); printResult(indexSearcher, booleanQuery); // 关闭资源 indexSearcher.getIndexReader().close(); }
结果:
+fileName:java fileName:struts java struts - .txt think smiling is as important as sunshine. Smiling is like sunshine because it can make people happy and have a good day. If you aren��t happy, you can smile, and then you will feel happy. Someone may say, ��But I don��t feel happy.�� Then I would say, ��Please smile as you do when you are happy or play wit 309 E:\lucene&solr\searchfiles\java struts - .txt ------------ java struts.txt think smiling is as important as sunshine. Smiling is like sunshine because it can make people happy and have a good day. If you aren��t happy, you can smile, and then you will feel happy. Someone may say, ��But I don��t feel happy.�� Then I would say, ��Please smile as you do when you are happy or play wit 309 E:\lucene&solr\searchfiles\java struts.txt ------------ java struts springmvc.txt think smiling is as important as sunshine. Smiling is like sunshine because it can make people happy and have a good day. If you aren��t happy, you can smile, and then you will feel happy. Someone may say, ��But I don��t feel happy.�� Then I would say, ��Please smile as you do when you are happy or play wit 309 E:\lucene&solr\searchfiles\java struts springmvc.txt ------------ java struts spring.txt think smiling is as important as sunshine. Smiling is like sunshine because it can make people happy and have a good day. If you aren��t happy, you can smile, and then you will feel happy. Someone may say, ��But I don��t feel happy.�� Then I would say, ��Please smile as you do when you are happy or play wit 309 E:\lucene&solr\searchfiles\java struts spring.txt ------------ java 基础.txt think smiling is as important as sunshine. Smiling is like sunshine because it can make people happy and have a good day. If you aren��t happy, you can smile, and then you will feel happy. Someone may say, ��But I don��t feel happy.�� Then I would say, ��Please smile as you do when you are happy or play wit 309 E:\lucene&solr\searchfiles\java 基础.txt ------------ java高级.txt ����java���̳̣��������������� 32 E:\lucene&solr\searchfiles\java高级.txt ------------
(2) 查找java与struts都必须有的
解析语法 +fileName:java +fileName:struts
@Test public void testBooleanQuery() throws Exception { IndexSearcher indexSearcher = getIndexSearcher(); BooleanQuery booleanQuery = new BooleanQuery(); Query query1 = new TermQuery(new Term("fileName", "java")); Query query2 = new TermQuery(new Term("fileName", "struts")); // select * from user where id =1 or name = 'safdsa' booleanQuery.add(query1, Occur.MUST); booleanQuery.add(query2, Occur.MUST); System.out.println(booleanQuery); printResult(indexSearcher, booleanQuery); // 关闭资源 indexSearcher.getIndexReader().close(); }
结果;(下面的+表示查询条件必须有要求的分词,可有可无不带+也不带-)
+fileName:java +fileName:struts java struts - .txt think smiling is as important as sunshine. Smiling is like sunshine because it can make people happy and have a good day. If you aren��t happy, you can smile, and then you will feel happy. Someone may say, ��But I don��t feel happy.�� Then I would say, ��Please smile as you do when you are happy or play wit 309 E:\lucene&solr\searchfiles\java struts - .txt ------------ java struts.txt think smiling is as important as sunshine. Smiling is like sunshine because it can make people happy and have a good day. If you aren��t happy, you can smile, and then you will feel happy. Someone may say, ��But I don��t feel happy.�� Then I would say, ��Please smile as you do when you are happy or play wit 309 E:\lucene&solr\searchfiles\java struts.txt ------------ java struts springmvc.txt think smiling is as important as sunshine. Smiling is like sunshine because it can make people happy and have a good day. If you aren��t happy, you can smile, and then you will feel happy. Someone may say, ��But I don��t feel happy.�� Then I would say, ��Please smile as you do when you are happy or play wit 309 E:\lucene&solr\searchfiles\java struts springmvc.txt ------------ java struts spring.txt think smiling is as important as sunshine. Smiling is like sunshine because it can make people happy and have a good day. If you aren��t happy, you can smile, and then you will feel happy. Someone may say, ��But I don��t feel happy.�� Then I would say, ��Please smile as you do when you are happy or play wit 309 E:\lucene&solr\searchfiles\java struts spring.txt ------------
(3)查询必有java,必须没有struts的文档
解析语法 +fileName:java -fileName:struts
@Test public void testBooleanQuery() throws Exception { IndexSearcher indexSearcher = getIndexSearcher(); BooleanQuery booleanQuery = new BooleanQuery(); Query query1 = new TermQuery(new Term("fileName", "java")); Query query2 = new TermQuery(new Term("fileName", "struts")); // select * from user where id =1 or name = 'safdsa' booleanQuery.add(query1, Occur.MUST); booleanQuery.add(query2, Occur.MUST_NOT); System.out.println(booleanQuery); printResult(indexSearcher, booleanQuery); // 关闭资源 indexSearcher.getIndexReader().close(); }
结果:(-表示查询条件中必须没有包含的分词)
+fileName:java -fileName:struts java 基础.txt think smiling is as important as sunshine. Smiling is like sunshine because it can make people happy and have a good day. If you aren��t happy, you can smile, and then you will feel happy. Someone may say, ��But I don��t feel happy.�� Then I would say, ��Please smile as you do when you are happy or play wit 309 E:\lucene&solr\searchfiles\java 基础.txt ------------ java高级.txt ����java���̳̣��������������� 32 E:\lucene&solr\searchfiles\java高级.txt ------------
(4)查询必有java,必须没有struts的文档且大小必须在4400字节的文档
解析语法 +fileName:java -fileName:struts +fileSize:{47 TO 400]
@Test public void testBooleanQuery() throws Exception { IndexSearcher indexSearcher = getIndexSearcher(); BooleanQuery booleanQuery = new BooleanQuery(); Query query1 = new TermQuery(new Term("fileName", "java")); Query query2 = new TermQuery(new Term("fileName", "struts")); Query query3 = NumericRangeQuery.newLongRange("fileSize", 47L, 400L, false, true); // select * from user where id =1 or name = 'safdsa' booleanQuery.add(query1, Occur.MUST); booleanQuery.add(query2, Occur.MUST_NOT); booleanQuery.add(query3, Occur.MUST); System.out.println(booleanQuery); printResult(indexSearcher, booleanQuery); // 关闭资源 indexSearcher.getIndexReader().close(); }
结果:
+fileName:java -fileName:struts +fileSize:{47 TO 400] java 基础.txt think smiling is as important as sunshine. Smiling is like sunshine because it can make people happy and have a good day. If you aren��t happy, you can smile, and then you will feel happy. Someone may say, ��But I don��t feel happy.�� Then I would say, ��Please smile as you do when you are happy or play wit 309 E:\lucene&solr\searchfiles\java 基础.txt ------------