首页 > 代码库 > Lucene中内置常用Query对象

Lucene中内置常用Query对象

       以下这几种lucene内置查询对象,不过在实际的项目开发中,这种查询方式应用的不多。般用QueryParser去获取查询对象。因为QueryParser内部基本实现了所有的内置查询对象。

      这是最常见的QueryParse的写法

	/**
	 * 通过QueryParser去获取查询对象
	 * 
	 * @throws ParseException
	 */
	@Test
	public void searchByQueryParse() throws ParseException {
		// 1创建QueryParser对象,默认域为content,可以改变
		QueryParser parser = new QueryParser(Version.LUCENE_35, "content",
				new StandardAnalyzer(Version.LUCENE_35));

		// 改变空格的默认操作符,以下可以改为AND
		parser.setDefaultOperator(Operator.AND);

		// 开启第一个字符的通配查询,默认是关闭的,太消耗性能
		parser.setAllowLeadingWildcard(true);

		// 搜索包含content中包含like的
		Query query = parser.parse("like");

		// 查询content中,basketball 和 basketball 的文档
		query = parser.parse("basketball football");

		// 改变搜索域
		query = parser.parse("name:make");

		// 用*或者? 通配符匹配
		query = parser.parse("name:j*");

		// 通配符默认不能放在首位
		query = parser.parse("email:*@itat.org");

		// 查询 名字中没mike,content中like的。 +和- 必须要放在前面
		query = parser.parse("- name:mike + like");

		// 匹配一个区间,TO必须大写
		query = parser.parse("id:[1 TO 6]");

		// 开区间
		query = parser.parse("id:{1 TO 3}");

		/**
		 * 查询字符串中空格的,加上“” 完全匹配
		 */
		query = parser.parse("\"I like football\"");

		// 查询I和football中一个字符距离的
		query = parser.parse("\"I football\"~1");

		// 没办法匹配数字范围,必须要自定义
		query = parser.parse("attach:[2 TO 10]");
		// 模糊匹配
		query = parser.parse("name:make~");

		su.searchByQueryParse(query, 10);
	}

 这是对term(最小分词单元)的查询

	/**
	 * 备注:这里的精确匹配查询,是匹配索引文件中的词。
	 * 如果 是搜索英文,忽视大小写
	 * @param field
	 * @param name
	 * @param num
	 */
	public void searchByTerm(String field,String name,int num) {
		try {
			IndexSearcher searcher = getSearcher();
			Query query = new TermQuery(new Term(field,name));
			TopDocs tds = searcher.search(query, num);
			String[] path = FieldCache.DEFAULT.getStrings(reader, "name");
			System.out.println(path.length);
			for(String p:path){
				System.out.println(p);
			}
			System.out.println("查询总数:"+tds.totalHits);
			for(ScoreDoc sd:tds.scoreDocs) {
				//System.out.println(sd.score);-查询得分信息
				Document doc = searcher.doc(sd.doc);
				System.out.println(doc.get("id")+"---->"+
						doc.get("name")+"["+doc.get("email")+"]-->"+doc.get("id")+","+
						doc.get("attach")+","+doc.get("date"));
			}
			searcher.close();
		} catch (CorruptIndexException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
多值查询(BooleanQuery)可以用来连接多个子查询:

      /**
	 * 多值查询
	 * @param num
	 */
	public void searchByBoolean(int num) {
		try {
			IndexSearcher searcher = getSearcher();
			BooleanQuery query = new BooleanQuery();
			/*
			 * BooleanQuery连接多值查询
			 * Occur.MUST表示必须出现
			 * Occur.SHOULD表示可以出现
			 * Occur.MUSE_NOT表示必须不能出现
			 */
			query.add(new TermQuery(new Term("name","zhangsan")), Occur.MUST_NOT);
			query.add(new TermQuery(new Term("content","game")),Occur.SHOULD);
			TopDocs tds = searcher.search(query, num);
			System.out.println("命中数:"+tds.totalHits);
			for(ScoreDoc sd:tds.scoreDocs) {
				Document doc = searcher.doc(sd.doc);
				System.out.println(doc.get("id")+"---->"+
						doc.get("name")+"["+doc.get("email")+"]-->"+doc.get("id")+","+
						doc.get("attach")+","+doc.get("date"));
			}
			searcher.close();
		} catch (CorruptIndexException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

 范围搜索(TermRangeQuery):

      /**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String dir = "D:\\user";

		try {
			Directory directory = FSDirectory.open(new File(dir));
			@SuppressWarnings("deprecation")
			IndexReader reader = IndexReader.open(directory);
			IndexSearcher indexSearcher = new IndexSearcher(reader);
			/**
			 * 范围搜索
			 */
			Term begin = new Term("datetime", "191111");
			Term end = new Term("datetime", "198108");
			/**
			 * 参数Boolean值说明 false表示不包含 191111 true 表示包含198108 这两个Boolean表示查询结果
			 * 是否包含lower,upper两个临界值
			 */
			TermRangeQuery termRangeQuery = new TermRangeQuery("datetime",
					begin.bytes(), end.bytes(), false, true);
			TopDocs topDocs = indexSearcher.search(termRangeQuery, 10);
			ScoreDoc scoreDocs[] = topDocs.scoreDocs;
			for (int i = 0; i < scoreDocs.length; i++) {
				Document document = indexSearcher.doc(scoreDocs[i].doc);
				System.out.println(document.get("id"));
				System.out.println(document.get("name"));
				System.out.println(document.get("text"));
				System.out.println(document.get("datetime"));
			}
			directory.close();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}


数字范围查询:

	public void searchByNumricRange(String field,int start,int end,int num) {
		try {
			IndexSearcher searcher = getSearcher();
			Query query = NumericRangeQuery.newIntRange(field,start, end,true,true);
			TopDocs tds = searcher.search(query, num);
			System.out.println("命中数量:"+tds.totalHits);
			for(ScoreDoc sd:tds.scoreDocs) {
				Document doc = searcher.doc(sd.doc);
				System.out.println(doc.get("id")+"---->"+
						doc.get("name")+"["+doc.get("email")+"]-->"+doc.get("id")+","+
						doc.get("attach")+","+doc.get("date"));
			}
			searcher.close();
		} catch (CorruptIndexException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

前缀搜索(PrefixQuery),注意也是对词的前缀搜索:

	/**
	 * 前缀搜索
	 * @param field
	 * @param value
	 * @param num
	 */
	public void searchByPrefix(String field,String value,int num) {
		try {
			IndexSearcher searcher = getSearcher();
			Query query = new PrefixQuery(new Term(field,value));
			TopDocs tds = searcher.search(query, num);
			System.out.println("命中数:"+tds.totalHits);
			for(ScoreDoc sd:tds.scoreDocs) {
				Document doc = searcher.doc(sd.doc);
				System.out.println(doc.get("id")+"---->"+
						doc.get("name")+"["+doc.get("email")+"]-->"+doc.get("id")+","+
						doc.get("attach")+","+doc.get("date"));
			}
			searcher.close();
		} catch (CorruptIndexException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

通配符搜索(WildcardQuery)

	/**
	 * 通配符查询 ?和* 
	 * @param field
	 * @param value
	 * @param num
	 */
	public void searchByWildcard(String field,String value,int num) {
		try {
			IndexSearcher searcher = getSearcher();
			//在传入的value中可以使用通配符?和*,问号表示匹配一个字符,*表示匹配任意多个字符
			Query query = new WildcardQuery(new Term(field,value));
			TopDocs tds = searcher.search(query, num);
			System.out.println("一共命中数:"+tds.totalHits);
			for(ScoreDoc sd:tds.scoreDocs) {
				Document doc = searcher.doc(sd.doc);
				System.out.println(doc.get("id")+"---->"+
						doc.get("name")+"["+doc.get("email")+"]-->"+doc.get("id")+","+
						doc.get("attach")+","+doc.get("date"));
			}
			searcher.close();
		} catch (CorruptIndexException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

短语查询(PhraseQuery),根据零碎的短语组合成新的词组进行搜索,一般应用于英文搜索中

 	public void searchByPhrase(int num) {
		try {
			IndexSearcher searcher = getSearcher();
			PhraseQuery query = new PhraseQuery();
			//设置跳数
			query.setSlop(3);
			query.add(new Term("content","pingpeng"));
			//第一个Term
			query.add(new Term("content","i"));
			//产生距离之后的第一个Term
//			query.add(new Term("content","football"));
			TopDocs tds = searcher.search(query, num);
			System.out.println("命中数:"+tds.totalHits);
			for(ScoreDoc sd:tds.scoreDocs) {
				Document doc = searcher.doc(sd.doc);
				System.out.println(doc.get("id")+"---->"+
						doc.get("name")+"["+doc.get("email")+"]-->"+doc.get("id")+","+
						doc.get("attach")+","+doc.get("date"));
			}
			searcher.close();
		} catch (CorruptIndexException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

模糊匹配(searchByFuzzy),允许有个别错误的查询 

      	public void searchByFuzzy(int num) {
		try {
			IndexSearcher searcher = getSearcher();
                  //调整相似读
			FuzzyQuery query = new FuzzyQuery(new Term("name","mase"),0.4f,0);
			System.out.println(query.getPrefixLength());
			System.out.println(query.getMinSimilarity());
			TopDocs tds = searcher.search(query, num);
			for(ScoreDoc sd:tds.scoreDocs) {
				Document doc = searcher.doc(sd.doc);
				System.out.println(doc.get("id")+"---->"+
						doc.get("name")+"["+doc.get("email")+"]-->"+doc.get("id")+","+
						doc.get("attach")+","+doc.get("date"));
			}
			searcher.close();
		} catch (CorruptIndexException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}


Lucene中内置常用Query对象