Example usage for org.apache.lucene.analysis MockAnalyzer MockAnalyzer

List of usage examples for org.apache.lucene.analysis MockAnalyzer MockAnalyzer

Introduction

In this page you can find the example usage for org.apache.lucene.analysis MockAnalyzer MockAnalyzer.

Prototype

public MockAnalyzer(Random random, CharacterRunAutomaton runAutomaton, boolean lowerCase,
        CharacterRunAutomaton filter) 

Source Link

Document

Creates a new MockAnalyzer.

Usage

From source file:org.apache.solr.search.TestSolrCoreParser.java

License:Apache License

private CoreParser solrCoreParser() {
    if (solrCoreParser == null) {
        final String defaultField = "contents";
        final Analyzer analyzer = new MockAnalyzer(random(), MockTokenizer.WHITESPACE, true,
                MockTokenFilter.ENGLISH_STOPSET);
        final SolrQueryRequest req = null;
        solrCoreParser = new SolrCoreParser(defaultField, analyzer, req);
        {/*from  w  ww . jav  a  2 s .c om*/
            final NamedList<String> args = new NamedList<>();
            args.add("HelloQuery", HelloQueryBuilder.class.getCanonicalName());
            args.add("GoodbyeQuery", GoodbyeQueryBuilder.class.getCanonicalName());
            args.add("HandyQuery", HandyQueryBuilder.class.getCanonicalName());
            args.add("ApacheLuceneSolr", ApacheLuceneSolrNearQueryBuilder.class.getCanonicalName());
            args.add("ChooseOneWord", ChooseOneWordQueryBuilder.class.getCanonicalName());
            solrCoreParser.init(args);
        }
    }
    return solrCoreParser;
}

From source file:org.easynet.resource.queryparser.QueryParserTestBase.java

License:Apache License

public void testBoost() throws Exception {
    CharacterRunAutomaton stopWords = new CharacterRunAutomaton(Automata.makeString("on"));
    Analyzer oneStopAnalyzer = new MockAnalyzer(random(), MockTokenizer.SIMPLE, true, stopWords);
    QueryParser qp = getParserConfig(oneStopAnalyzer);
    Query q = getQuery("on^1.0", qp);
    assertNotNull(q);/*from   www  . j a  v a2s .  co m*/
    q = getQuery("\"hello\"^2.0", qp);
    assertNotNull(q);
    assertEquals(q.getBoost(), (float) 2.0, (float) 0.5);
    q = getQuery("hello^2.0", qp);
    assertNotNull(q);
    assertEquals(q.getBoost(), (float) 2.0, (float) 0.5);
    q = getQuery("\"on\"^1.0", qp);
    assertNotNull(q);

    Analyzer a2 = new MockAnalyzer(random(), MockTokenizer.SIMPLE, true, MockTokenFilter.ENGLISH_STOPSET);
    QueryParser qp2 = getParserConfig(a2);
    q = getQuery("the^3", qp2);
    // "the" is a stop word so the result is an empty query:
    assertNotNull(q);
    assertEquals("", q.toString());
    assertEquals(1.0f, q.getBoost(), 0.01f);
}

From source file:org.easynet.resource.queryparser.QueryParserTestBase.java

License:Apache License

public void testStopwords() throws Exception {
    CharacterRunAutomaton stopSet = new CharacterRunAutomaton(new RegExp("the|foo").toAutomaton());
    QueryParser qp = getParserConfig(new MockAnalyzer(random(), MockTokenizer.SIMPLE, true, stopSet));
    Query result = getQuery("field:the OR field:foo", qp);
    assertNotNull("result is null and it shouldn't be", result);
    assertTrue("result is not a BooleanQuery", result instanceof BooleanQuery);
    assertTrue(((BooleanQuery) result).clauses().size() + " does not equal: " + 0,
            ((BooleanQuery) result).clauses().size() == 0);
    result = getQuery("field:woo OR field:the", qp);
    assertNotNull("result is null and it shouldn't be", result);
    assertTrue("result is not a TermQuery", result instanceof TermQuery);
    result = getQuery("(fieldX:xxxxx OR fieldy:xxxxxxxx)^2 AND (fieldx:the OR fieldy:foo)", qp);
    assertNotNull("result is null and it shouldn't be", result);
    assertTrue("result is not a BooleanQuery", result instanceof BooleanQuery);
    if (VERBOSE)//from  ww  w.ja  v a 2 s.c om
        System.out.println("Result: " + result);
    assertTrue(((BooleanQuery) result).clauses().size() + " does not equal: " + 2,
            ((BooleanQuery) result).clauses().size() == 2);
}

From source file:org.easynet.resource.queryparser.QueryParserTestBase.java

License:Apache License

public void testPositionIncrement() throws Exception {
    QueryParser qp = getParserConfig(//w ww . j a v a 2s  .co m
            new MockAnalyzer(random(), MockTokenizer.SIMPLE, true, MockTokenFilter.ENGLISH_STOPSET));
    qp.setEnablePositionIncrements(true);
    String qtxt = "\"the words in poisitions pos02578 are stopped in this phrasequery\"";
    // 0 2 5 7 8
    int expectedPositions[] = { 1, 3, 4, 6, 9 };
    PhraseQuery pq = (PhraseQuery) getQuery(qtxt, qp);
    // System.out.println("Query text: "+qtxt);
    // System.out.println("Result: "+pq);
    Term t[] = pq.getTerms();
    int pos[] = pq.getPositions();
    for (int i = 0; i < t.length; i++) {
        // System.out.println(i+". "+t[i]+"  pos: "+pos[i]);
        assertEquals("term " + i + " = " + t[i] + " has wrong term-position!", expectedPositions[i], pos[i]);
    }
}

From source file:org.easynet.resource.queryparser.QueryParserTestBase.java

License:Apache License

public void testPhraseQueryToString() throws Exception {
    Analyzer analyzer = new MockAnalyzer(random(), MockTokenizer.SIMPLE, true, MockTokenFilter.ENGLISH_STOPSET);
    QueryParser qp = getParserConfig(analyzer);
    qp.setEnablePositionIncrements(true);
    PhraseQuery q = (PhraseQuery) getQuery("\"this hi this is a test is\"", qp);
    assertEquals("field:\"? hi ? ? ? test\"", q.toString());
}

From source file:org.easynet.resource.queryparser.QueryParserTestBase.java

License:Apache License

public void testPhraseQueryPositionIncrements() throws Exception {
    CharacterRunAutomaton stopStopList = new CharacterRunAutomaton(
            new RegExp("[sS][tT][oO][pP]").toAutomaton());

    QueryParser qp = getParserConfig(new MockAnalyzer(random(), MockTokenizer.WHITESPACE, false, stopStopList));

    qp = getParserConfig(new MockAnalyzer(random(), MockTokenizer.WHITESPACE, false, stopStopList));
    qp.setEnablePositionIncrements(true);

    PhraseQuery phraseQuery = new PhraseQuery();
    phraseQuery.add(new Term("field", "1"));
    phraseQuery.add(new Term("field", "2"), 2);
    assertEquals(phraseQuery, getQuery("\"1 stop 2\"", qp));
}

From source file:org.tallison.lucene.queryparser.spans.TestOverallSpanQueryParser.java

License:Apache License

public void testStops() throws Exception {
    Analyzer stopsAnalyzer = new MockAnalyzer(random(), MockTokenizer.WHITESPACE, true,
            MockTokenFilter.ENGLISH_STOPSET);
    Directory dir = newDirectory();//from ww  w  . ja  v  a2 s .c om
    RandomIndexWriter w = new RandomIndexWriter(random(), dir, newIndexWriterConfig(stopsAnalyzer)
            .setMaxBufferedDocs(TestUtil.nextInt(random(), 100, 1000)).setMergePolicy(newLogMergePolicy()));
    String[] docs = new String[] { "ab the the cd the the the ef the gh", "ab cd", "ab the ef" };

    for (int i = 0; i < docs.length; i++) {
        Document doc = new Document();
        doc.add(newTextField(FIELD1, docs[i], Field.Store.YES));
        w.addDocument(doc);
    }
    IndexReader r = w.getReader();
    IndexSearcher s = newSearcher(r);
    w.close();
    SpanQueryParser p = new SpanQueryParser(FIELD1, stopsAnalyzer, MULTITERM_ANALYZER);
    assertHits("-ab +the +cd", p, s, 0);
    assertHits("+ab +the +cd", p, s, 2);
    assertHits("+the", p, s, 0);
    assertHits("ab AND CD", p, s, 2);
    assertHits("ab AND the", p, s, 3);
    assertHits("ab OR the", p, s, 3);
    assertHits("(ab the cd)~2", p, s, 2);
    assertHits("(ab the cd)~3", p, s, 0);
    assertHits("ab AND (the OR cd)", p, s, 2);
    assertHits("ab AND (the AND cd)", p, s, 2);
    assertHits("cd OR (the OR ef)", p, s, 3);
    assertHits("cd AND (the AND ef)", p, s, 1);
    //do we want this behavior?
    assertHits("-the", p, s, 0);

    assertHits("\"ab cd\"", p, s, 1);
    assertHits("\"ab a a cd\"", p, s, 2);
    assertHits("\"ab a cd\"~1", p, s, 2);
    assertHits("\"ab a cd\"~>1", p, s, 2);
    assertHits("\"cd a a ab\"", p, s, 0);
    assertHits("\"cd a ab\"~1", p, s, 2);

    r.close();
    dir.close();
}

From source file:org.tallison.lucene.queryparser.spans.TestQPTestBaseSpanQuery.java

License:Apache License

@Override
public void testPositionIncrement() throws Exception {
    //For SQP, this only tests whether stop words have been dropped.
    //PositionIncrements are not available in SpanQueries yet.
    CommonQueryParserConfiguration qp = getParserConfig(
            new MockAnalyzer(random(), MockTokenizer.SIMPLE, true, MockTokenFilter.ENGLISH_STOPSET));
    //qp.setEnablePositionIncrements(true);
    String qtxt = "\"the words in poisitions pos02578 are stopped in this phrasequery\"";
    //               0         2                      5           7  8
    SpanNearQuery pq = (SpanNearQuery) getQuery(qtxt, qp);
    SpanQuery[] clauses = pq.getClauses();
    assertEquals(clauses.length, 5);//from w  w w  . ja va 2 s  . co  m
    Set<Term> expected = new HashSet<Term>();
    expected.add(new Term("field", "words"));
    expected.add(new Term("field", "poisitions"));
    expected.add(new Term("field", "pos"));
    expected.add(new Term("field", "stopped"));
    expected.add(new Term("field", "phrasequery"));
}