⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testqueryparser.java

📁 Lucene a java open-source SearchEngine Framework
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
package org.apache.lucene.queryParser;/** * 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 org.apache.lucene.util.LuceneTestCase;import org.apache.lucene.analysis.*;import org.apache.lucene.analysis.Token;import org.apache.lucene.analysis.standard.StandardAnalyzer;import org.apache.lucene.document.DateField;import org.apache.lucene.document.DateTools;import org.apache.lucene.document.Document;import org.apache.lucene.document.Field;import org.apache.lucene.index.IndexWriter;import org.apache.lucene.index.Term;import org.apache.lucene.search.*;import org.apache.lucene.store.RAMDirectory;import java.io.IOException;import java.io.Reader;import java.text.DateFormat;import java.util.Calendar;import java.util.Date;import java.util.Locale;/** * Tests QueryParser. */public class TestQueryParser extends LuceneTestCase {  public static Analyzer qpAnalyzer = new QPTestAnalyzer();  public static class QPTestFilter extends TokenFilter {    /**     * Filter which discards the token 'stop' and which expands the     * token 'phrase' into 'phrase1 phrase2'     */    public QPTestFilter(TokenStream in) {      super(in);    }    boolean inPhrase = false;    int savedStart = 0, savedEnd = 0;    public Token next() throws IOException {      if (inPhrase) {        inPhrase = false;        return new Token("phrase2", savedStart, savedEnd);      } else        for (Token token = input.next(); token != null; token = input.next()) {          if (token.termText().equals("phrase")) {            inPhrase = true;            savedStart = token.startOffset();            savedEnd = token.endOffset();            return new Token("phrase1", savedStart, savedEnd);          } else if (!token.termText().equals("stop"))            return token;        }      return null;    }  }  public static class QPTestAnalyzer extends Analyzer {    /** Filters LowerCaseTokenizer with StopFilter. */    public final TokenStream tokenStream(String fieldName, Reader reader) {      return new QPTestFilter(new LowerCaseTokenizer(reader));    }  }  public static class QPTestParser extends QueryParser {    public QPTestParser(String f, Analyzer a) {      super(f, a);    }    protected Query getFuzzyQuery(String field, String termStr, float minSimilarity) throws ParseException {      throw new ParseException("Fuzzy queries not allowed");    }    protected Query getWildcardQuery(String field, String termStr) throws ParseException {      throw new ParseException("Wildcard queries not allowed");    }  }  private int originalMaxClauses;  public void setUp() throws Exception {    super.setUp();    originalMaxClauses = BooleanQuery.getMaxClauseCount();  }  public QueryParser getParser(Analyzer a) throws Exception {    if (a == null)      a = new SimpleAnalyzer();    QueryParser qp = new QueryParser("field", a);    qp.setDefaultOperator(QueryParser.OR_OPERATOR);    return qp;  }  public Query getQuery(String query, Analyzer a) throws Exception {    return getParser(a).parse(query);  }  public void assertQueryEquals(String query, Analyzer a, String result)    throws Exception {    Query q = getQuery(query, a);    String s = q.toString("field");    if (!s.equals(result)) {      fail("Query /" + query + "/ yielded /" + s           + "/, expecting /" + result + "/");    }  }  public void assertQueryEquals(QueryParser qp, String field, String query, String result)     throws Exception {    Query q = qp.parse(query);    String s = q.toString(field);    if (!s.equals(result)) {      fail("Query /" + query + "/ yielded /" + s           + "/, expecting /" + result + "/");    }  }    public void assertEscapedQueryEquals(String query, Analyzer a, String result)    throws Exception {    String escapedQuery = QueryParser.escape(query);    if (!escapedQuery.equals(result)) {      fail("Query /" + query + "/ yielded /" + escapedQuery          + "/, expecting /" + result + "/");    }  }  public void assertWildcardQueryEquals(String query, boolean lowercase, String result, boolean allowLeadingWildcard)    throws Exception {    QueryParser qp = getParser(null);    qp.setLowercaseExpandedTerms(lowercase);    qp.setAllowLeadingWildcard(allowLeadingWildcard);    Query q = qp.parse(query);    String s = q.toString("field");    if (!s.equals(result)) {      fail("WildcardQuery /" + query + "/ yielded /" + s           + "/, expecting /" + result + "/");    }  }  public void assertWildcardQueryEquals(String query, boolean lowercase, String result)    throws Exception {    assertWildcardQueryEquals(query, lowercase, result, false);  }  public void assertWildcardQueryEquals(String query, String result) throws Exception {    QueryParser qp = getParser(null);    Query q = qp.parse(query);    String s = q.toString("field");    if (!s.equals(result)) {      fail("WildcardQuery /" + query + "/ yielded /" + s + "/, expecting /"          + result + "/");    }  }  public Query getQueryDOA(String query, Analyzer a)    throws Exception {    if (a == null)      a = new SimpleAnalyzer();    QueryParser qp = new QueryParser("field", a);    qp.setDefaultOperator(QueryParser.AND_OPERATOR);    return qp.parse(query);  }  public void assertQueryEqualsDOA(String query, Analyzer a, String result)    throws Exception {    Query q = getQueryDOA(query, a);    String s = q.toString("field");    if (!s.equals(result)) {      fail("Query /" + query + "/ yielded /" + s           + "/, expecting /" + result + "/");    }  }  public void testSimple() throws Exception {    assertQueryEquals("term term term", null, "term term term");    assertQueryEquals("türm term term", new WhitespaceAnalyzer(), "türm term term");    assertQueryEquals("ümlaut", new WhitespaceAnalyzer(), "ümlaut");    assertQueryEquals("\"\"", new KeywordAnalyzer(), "");    assertQueryEquals("foo:\"\"", new KeywordAnalyzer(), "foo:");    assertQueryEquals("a AND b", null, "+a +b");    assertQueryEquals("(a AND b)", null, "+a +b");    assertQueryEquals("c OR (a AND b)", null, "c (+a +b)");    assertQueryEquals("a AND NOT b", null, "+a -b");    assertQueryEquals("a AND -b", null, "+a -b");    assertQueryEquals("a AND !b", null, "+a -b");    assertQueryEquals("a && b", null, "+a +b");    assertQueryEquals("a && ! b", null, "+a -b");    assertQueryEquals("a OR b", null, "a b");    assertQueryEquals("a || b", null, "a b");    assertQueryEquals("a OR !b", null, "a -b");    assertQueryEquals("a OR ! b", null, "a -b");    assertQueryEquals("a OR -b", null, "a -b");    assertQueryEquals("+term -term term", null, "+term -term term");    assertQueryEquals("foo:term AND field:anotherTerm", null,                      "+foo:term +anotherterm");    assertQueryEquals("term AND \"phrase phrase\"", null,                      "+term +\"phrase phrase\"");    assertQueryEquals("\"hello there\"", null, "\"hello there\"");    assertTrue(getQuery("a AND b", null) instanceof BooleanQuery);    assertTrue(getQuery("hello", null) instanceof TermQuery);    assertTrue(getQuery("\"hello there\"", null) instanceof PhraseQuery);    assertQueryEquals("germ term^2.0", null, "germ term^2.0");    assertQueryEquals("(term)^2.0", null, "term^2.0");    assertQueryEquals("(germ term)^2.0", null, "(germ term)^2.0");    assertQueryEquals("term^2.0", null, "term^2.0");    assertQueryEquals("term^2", null, "term^2.0");    assertQueryEquals("\"germ term\"^2.0", null, "\"germ term\"^2.0");    assertQueryEquals("\"term germ\"^2", null, "\"term germ\"^2.0");    assertQueryEquals("(foo OR bar) AND (baz OR boo)", null,                      "+(foo bar) +(baz boo)");    assertQueryEquals("((a OR b) AND NOT c) OR d", null,                      "(+(a b) -c) d");    assertQueryEquals("+(apple \"steve jobs\") -(foo bar baz)", null,                      "+(apple \"steve jobs\") -(foo bar baz)");    assertQueryEquals("+title:(dog OR cat) -author:\"bob dole\"", null,                      "+(title:dog title:cat) -author:\"bob dole\"");        QueryParser qp = new QueryParser("field", new StandardAnalyzer());    // make sure OR is the default:    assertEquals(QueryParser.OR_OPERATOR, qp.getDefaultOperator());    qp.setDefaultOperator(QueryParser.AND_OPERATOR);    assertEquals(QueryParser.AND_OPERATOR, qp.getDefaultOperator());    qp.setDefaultOperator(QueryParser.OR_OPERATOR);    assertEquals(QueryParser.OR_OPERATOR, qp.getDefaultOperator());  }  public void testPunct() throws Exception {    Analyzer a = new WhitespaceAnalyzer();    assertQueryEquals("a&b", a, "a&b");    assertQueryEquals("a&&b", a, "a&&b");    assertQueryEquals(".NET", a, ".NET");  }  public void testSlop() throws Exception {    assertQueryEquals("\"term germ\"~2", null, "\"term germ\"~2");    assertQueryEquals("\"term germ\"~2 flork", null, "\"term germ\"~2 flork");    assertQueryEquals("\"term\"~2", null, "term");    assertQueryEquals("\" \"~2 germ", null, "germ");    assertQueryEquals("\"term germ\"~2^2", null, "\"term germ\"~2^2.0");  }  public void testNumber() throws Exception {// The numbers go away because SimpleAnalzyer ignores them    assertQueryEquals("3", null, "");    assertQueryEquals("term 1.0 1 2", null, "term");    assertQueryEquals("term term1 term2", null, "term term term");    Analyzer a = new StandardAnalyzer();    assertQueryEquals("3", a, "3");    assertQueryEquals("term 1.0 1 2", a, "term 1.0 1 2");    assertQueryEquals("term term1 term2", a, "term term1 term2");  }  public void testWildcard() throws Exception {    assertQueryEquals("term*", null, "term*");    assertQueryEquals("term*^2", null, "term*^2.0");    assertQueryEquals("term~", null, "term~0.5");    assertQueryEquals("term~0.7", null, "term~0.7");    assertQueryEquals("term~^2", null, "term~0.5^2.0");    assertQueryEquals("term^2~", null, "term~0.5^2.0");    assertQueryEquals("term*germ", null, "term*germ");    assertQueryEquals("term*germ^3", null, "term*germ^3.0");    assertTrue(getQuery("term*", null) instanceof PrefixQuery);    assertTrue(getQuery("term*^2", null) instanceof PrefixQuery);    assertTrue(getQuery("term~", null) instanceof FuzzyQuery);    assertTrue(getQuery("term~0.7", null) instanceof FuzzyQuery);    FuzzyQuery fq = (FuzzyQuery)getQuery("term~0.7", null);    assertEquals(0.7f, fq.getMinSimilarity(), 0.1f);    assertEquals(FuzzyQuery.defaultPrefixLength, fq.getPrefixLength());    fq = (FuzzyQuery)getQuery("term~", null);    assertEquals(0.5f, fq.getMinSimilarity(), 0.1f);    assertEquals(FuzzyQuery.defaultPrefixLength, fq.getPrefixLength());        assertParseException("term~1.1"); // value > 1, throws exception    assertTrue(getQuery("term*germ", null) instanceof WildcardQuery);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -