📄 testbooleanminshouldmatch.java
字号:
package org.apache.lucene.search;/** * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */import junit.framework.TestCase;import org.apache.lucene.util.LuceneTestCase;import org.apache.lucene.analysis.WhitespaceAnalyzer;import org.apache.lucene.document.Document;import org.apache.lucene.document.Field;import org.apache.lucene.index.IndexReader;import org.apache.lucene.index.IndexWriter;import org.apache.lucene.index.Term;import org.apache.lucene.store.Directory;import org.apache.lucene.store.RAMDirectory;import java.text.DecimalFormat;import java.util.Random;/** Test that BooleanQuery.setMinimumNumberShouldMatch works. */public class TestBooleanMinShouldMatch extends LuceneTestCase { public Directory index; public IndexReader r; public IndexSearcher s; public void setUp() throws Exception { super.setUp(); String[] data = new String [] { "A 1 2 3 4 5 6", "Z 4 5 6", null, "B 2 4 5 6", "Y 3 5 6", null, "C 3 6", "X 4 5 6" }; index = new RAMDirectory(); IndexWriter writer = new IndexWriter(index, new WhitespaceAnalyzer(), true); for (int i = 0; i < data.length; i++) { Document doc = new Document(); doc.add(new Field("id", String.valueOf(i), Field.Store.YES, Field.Index.UN_TOKENIZED));//Field.Keyword("id",String.valueOf(i))); doc.add(new Field("all", "all", Field.Store.YES, Field.Index.UN_TOKENIZED));//Field.Keyword("all","all")); if (null != data[i]) { doc.add(new Field("data", data[i], Field.Store.YES, Field.Index.TOKENIZED));//Field.Text("data",data[i])); } writer.addDocument(doc); } writer.optimize(); writer.close(); r = IndexReader.open(index); s = new IndexSearcher(r);//System.out.println("Set up " + getName()); } public void verifyNrHits(Query q, int expected) throws Exception { Hits h = s.search(q); if (expected != h.length()) { printHits(getName(), h); } assertEquals("result count", expected, h.length()); QueryUtils.check(q,s); } public void testAllOptional() throws Exception { BooleanQuery q = new BooleanQuery(); for (int i = 1; i <=4; i++) { q.add(new TermQuery(new Term("data",""+i)), BooleanClause.Occur.SHOULD);//false, false); } q.setMinimumNumberShouldMatch(2); // match at least two of 4 verifyNrHits(q, 2); } public void testOneReqAndSomeOptional() throws Exception { /* one required, some optional */ BooleanQuery q = new BooleanQuery(); q.add(new TermQuery(new Term("all", "all" )), BooleanClause.Occur.MUST);//true, false); q.add(new TermQuery(new Term("data", "5" )), BooleanClause.Occur.SHOULD);//false, false); q.add(new TermQuery(new Term("data", "4" )), BooleanClause.Occur.SHOULD);//false, false); q.add(new TermQuery(new Term("data", "3" )), BooleanClause.Occur.SHOULD);//false, false); q.setMinimumNumberShouldMatch(2); // 2 of 3 optional verifyNrHits(q, 5); } public void testSomeReqAndSomeOptional() throws Exception { /* two required, some optional */ BooleanQuery q = new BooleanQuery(); q.add(new TermQuery(new Term("all", "all" )), BooleanClause.Occur.MUST);//true, false); q.add(new TermQuery(new Term("data", "6" )), BooleanClause.Occur.MUST);//true, false); q.add(new TermQuery(new Term("data", "5" )), BooleanClause.Occur.SHOULD);//false, false); q.add(new TermQuery(new Term("data", "4" )), BooleanClause.Occur.SHOULD);//false, false); q.add(new TermQuery(new Term("data", "3" )), BooleanClause.Occur.SHOULD);//false, false); q.setMinimumNumberShouldMatch(2); // 2 of 3 optional verifyNrHits(q, 5); } public void testOneProhibAndSomeOptional() throws Exception { /* one prohibited, some optional */ BooleanQuery q = new BooleanQuery(); q.add(new TermQuery(new Term("data", "1" )), BooleanClause.Occur.SHOULD);//false, false); q.add(new TermQuery(new Term("data", "2" )), BooleanClause.Occur.SHOULD);//false, false); q.add(new TermQuery(new Term("data", "3" )), BooleanClause.Occur.MUST_NOT);//false, true ); q.add(new TermQuery(new Term("data", "4" )), BooleanClause.Occur.SHOULD);//false, false); q.setMinimumNumberShouldMatch(2); // 2 of 3 optional verifyNrHits(q, 1); } public void testSomeProhibAndSomeOptional() throws Exception { /* two prohibited, some optional */ BooleanQuery q = new BooleanQuery(); q.add(new TermQuery(new Term("data", "1" )), BooleanClause.Occur.SHOULD);//false, false); q.add(new TermQuery(new Term("data", "2" )), BooleanClause.Occur.SHOULD);//false, false); q.add(new TermQuery(new Term("data", "3" )), BooleanClause.Occur.MUST_NOT);//false, true ); q.add(new TermQuery(new Term("data", "4" )), BooleanClause.Occur.SHOULD);//false, false); q.add(new TermQuery(new Term("data", "C" )), BooleanClause.Occur.MUST_NOT);//false, true ); q.setMinimumNumberShouldMatch(2); // 2 of 3 optional verifyNrHits(q, 1); } public void testOneReqOneProhibAndSomeOptional() throws Exception { /* one required, one prohibited, some optional */ BooleanQuery q = new BooleanQuery(); q.add(new TermQuery(new Term("data", "6" )), BooleanClause.Occur.MUST);// true, false); q.add(new TermQuery(new Term("data", "5" )), BooleanClause.Occur.SHOULD);//false, false); q.add(new TermQuery(new Term("data", "4" )), BooleanClause.Occur.SHOULD);//false, false); q.add(new TermQuery(new Term("data", "3" )), BooleanClause.Occur.MUST_NOT);//false, true ); q.add(new TermQuery(new Term("data", "2" )), BooleanClause.Occur.SHOULD);//false, false); q.add(new TermQuery(new Term("data", "1" )), BooleanClause.Occur.SHOULD);//false, false); q.setMinimumNumberShouldMatch(3); // 3 of 4 optional verifyNrHits(q, 1); } public void testSomeReqOneProhibAndSomeOptional() throws Exception { /* two required, one prohibited, some optional */ BooleanQuery q = new BooleanQuery(); q.add(new TermQuery(new Term("all", "all")), BooleanClause.Occur.MUST);//true, false); q.add(new TermQuery(new Term("data", "6" )), BooleanClause.Occur.MUST);//true, false); q.add(new TermQuery(new Term("data", "5" )), BooleanClause.Occur.SHOULD);//false, false); q.add(new TermQuery(new Term("data", "4" )), BooleanClause.Occur.SHOULD);//false, false); q.add(new TermQuery(new Term("data", "3" )), BooleanClause.Occur.MUST_NOT);//false, true ); q.add(new TermQuery(new Term("data", "2" )), BooleanClause.Occur.SHOULD);//false, false); q.add(new TermQuery(new Term("data", "1" )), BooleanClause.Occur.SHOULD);//false, false); q.setMinimumNumberShouldMatch(3); // 3 of 4 optional
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -