|
Lucene example source code file (TestComplexPhraseQuery.java)
The Lucene TestComplexPhraseQuery.java source codepackage org.apache.lucene.queryParser.complexPhrase; /** * 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 java.util.HashSet; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.MockAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.queryParser.QueryParser; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query; import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.search.TopDocs; import org.apache.lucene.store.Directory; import org.apache.lucene.util.LuceneTestCase; public class TestComplexPhraseQuery extends LuceneTestCase { Directory rd; Analyzer analyzer = new MockAnalyzer(random); DocData docsContent[] = { new DocData("john smith", "1"), new DocData("johathon smith", "2"), new DocData("john percival smith", "3"), new DocData("jackson waits tom", "4") }; private IndexSearcher searcher; String defaultFieldName = "name"; public void testComplexPhrases() throws Exception { checkMatches("\"john smith\"", "1"); // Simple multi-term still works checkMatches("\"j* smyth~\"", "1,2"); // wildcards and fuzzies are OK in // phrases checkMatches("\"(jo* -john) smith\"", "2"); // boolean logic works checkMatches("\"jo* smith\"~2", "1,2,3"); // position logic works. checkMatches("\"jo* [sma TO smZ]\" ", "1,2"); // range queries supported checkMatches("\"john\"", "1,3"); // Simple single-term still works checkMatches("\"(john OR johathon) smith\"", "1,2"); // boolean logic with // brackets works. checkMatches("\"(jo* -john) smyth~\"", "2"); // boolean logic with // brackets works. // checkMatches("\"john -percival\"", "1"); // not logic doesn't work // currently :(. checkMatches("\"john nosuchword*\"", ""); // phrases with clauses producing // empty sets checkBadQuery("\"jo* id:1 smith\""); // mixing fields in a phrase is bad checkBadQuery("\"jo* \"smith\" \""); // phrases inside phrases is bad } private void checkBadQuery(String qString) { QueryParser qp = new ComplexPhraseQueryParser(TEST_VERSION_CURRENT, defaultFieldName, analyzer); Throwable expected = null; try { qp.parse(qString); } catch (Throwable e) { expected = e; } assertNotNull("Expected parse error in " + qString, expected); } private void checkMatches(String qString, String expectedVals) throws Exception { QueryParser qp = new ComplexPhraseQueryParser(TEST_VERSION_CURRENT, defaultFieldName, analyzer); qp.setFuzzyPrefixLength(1); // usually a good idea Query q = qp.parse(qString); HashSet<String> expecteds = new HashSet Other Lucene examples (source code examples)Here is a short list of links related to this Lucene TestComplexPhraseQuery.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.