Search in sources :

Example 1 with MockSynonymAnalyzer

use of org.apache.lucene.analysis.MockSynonymAnalyzer in project lucene-solr by apache.

the class TestQueryParser method testDefaultSplitOnWhitespace.

public void testDefaultSplitOnWhitespace() throws Exception {
    QueryParser parser = new QueryParser("field", new Analyzer1());
    // default is false
    assertFalse(parser.getSplitOnWhitespace());
    // A multi-word synonym source will form a synonym query for the same-starting-position tokens
    TermQuery guinea = new TermQuery(new Term("field", "guinea"));
    TermQuery pig = new TermQuery(new Term("field", "pig"));
    TermQuery cavy = new TermQuery(new Term("field", "cavy"));
    // A multi-word synonym source will form a graph query for synonyms that formed the graph token stream
    BooleanQuery.Builder synonym = new BooleanQuery.Builder();
    synonym.add(guinea, BooleanClause.Occur.MUST);
    synonym.add(pig, BooleanClause.Occur.MUST);
    BooleanQuery guineaPig = synonym.build();
    BooleanQuery graphQuery = new BooleanQuery.Builder().add(guineaPig, BooleanClause.Occur.SHOULD).add(cavy, BooleanClause.Occur.SHOULD).build();
    ;
    assertEquals(graphQuery, parser.parse("guinea pig"));
    boolean oldSplitOnWhitespace = splitOnWhitespace;
    splitOnWhitespace = QueryParser.DEFAULT_SPLIT_ON_WHITESPACE;
    assertQueryEquals("guinea pig", new MockSynonymAnalyzer(), "(+guinea +pig) cavy");
    splitOnWhitespace = oldSplitOnWhitespace;
}
Also used : SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) MockSynonymAnalyzer(org.apache.lucene.analysis.MockSynonymAnalyzer) Term(org.apache.lucene.index.Term)

Example 2 with MockSynonymAnalyzer

use of org.apache.lucene.analysis.MockSynonymAnalyzer in project lucene-solr by apache.

the class TestQueryParser method testSynonyms.

/** simple synonyms test */
public void testSynonyms() throws Exception {
    Query expected = new SynonymQuery(new Term(FIELD, "dogs"), new Term(FIELD, "dog"));
    QueryParser qp = new QueryParser(FIELD, new MockSynonymAnalyzer());
    assertEquals(expected, qp.parse("dogs"));
    assertEquals(expected, qp.parse("\"dogs\""));
    qp.setDefaultOperator(Operator.AND);
    assertEquals(expected, qp.parse("dogs"));
    assertEquals(expected, qp.parse("\"dogs\""));
    expected = new BoostQuery(expected, 2f);
    assertEquals(expected, qp.parse("dogs^2"));
    assertEquals(expected, qp.parse("\"dogs\"^2"));
}
Also used : Query(org.apache.lucene.search.Query) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) TermQuery(org.apache.lucene.search.TermQuery) SynonymQuery(org.apache.lucene.search.SynonymQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) SynonymQuery(org.apache.lucene.search.SynonymQuery) MockSynonymAnalyzer(org.apache.lucene.analysis.MockSynonymAnalyzer) Term(org.apache.lucene.index.Term) BoostQuery(org.apache.lucene.search.BoostQuery)

Example 3 with MockSynonymAnalyzer

use of org.apache.lucene.analysis.MockSynonymAnalyzer in project lucene-solr by apache.

the class TestComplexPhraseQuery method testSynonyms.

public void testSynonyms() throws Exception {
    checkMatches("\"dogs\"", "8");
    MockSynonymAnalyzer synonym = new MockSynonymAnalyzer();
    checkMatches("\"dogs\"", "7,8", synonym);
    // synonym is unidirectional 
    checkMatches("\"dog\"", "7", synonym);
    checkMatches("\"dogs cigar*\"", "");
    checkMatches("\"dog cigar*\"", "7");
    checkMatches("\"dogs cigar*\"", "7", synonym);
    checkMatches("\"dog cigar*\"", "7", synonym);
    checkMatches("\"dogs cigar*\"~2", "7,8", synonym);
    // synonym is unidirectional
    checkMatches("\"dog cigar*\"~2", "7", synonym);
}
Also used : MockSynonymAnalyzer(org.apache.lucene.analysis.MockSynonymAnalyzer)

Example 4 with MockSynonymAnalyzer

use of org.apache.lucene.analysis.MockSynonymAnalyzer in project lucene-solr by apache.

the class TestQueryParser method testSynonymsPhrase.

/** forms multiphrase query */
public void testSynonymsPhrase() throws Exception {
    MultiPhraseQuery.Builder expectedQBuilder = new MultiPhraseQuery.Builder();
    expectedQBuilder.add(new Term(FIELD, "old"));
    expectedQBuilder.add(new Term[] { new Term(FIELD, "dogs"), new Term(FIELD, "dog") });
    QueryParser qp = new QueryParser(FIELD, new MockSynonymAnalyzer());
    assertEquals(expectedQBuilder.build(), qp.parse("\"old dogs\""));
    qp.setDefaultOperator(Operator.AND);
    assertEquals(expectedQBuilder.build(), qp.parse("\"old dogs\""));
    BoostQuery expected = new BoostQuery(expectedQBuilder.build(), 2f);
    assertEquals(expected, qp.parse("\"old dogs\"^2"));
    expectedQBuilder.setSlop(3);
    expected = new BoostQuery(expectedQBuilder.build(), 2f);
    assertEquals(expected, qp.parse("\"old dogs\"~3^2"));
}
Also used : MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) MockSynonymAnalyzer(org.apache.lucene.analysis.MockSynonymAnalyzer) Term(org.apache.lucene.index.Term) BoostQuery(org.apache.lucene.search.BoostQuery)

Example 5 with MockSynonymAnalyzer

use of org.apache.lucene.analysis.MockSynonymAnalyzer in project lucene-solr by apache.

the class TestQueryParser method testOperatorsAndMultiWordSynonymsSplitOnWhitespace.

public void testOperatorsAndMultiWordSynonymsSplitOnWhitespace() throws Exception {
    Analyzer a = new MockSynonymAnalyzer();
    boolean oldSplitOnWhitespace = splitOnWhitespace;
    splitOnWhitespace = true;
    assertQueryEquals("+guinea pig", a, "+guinea pig");
    assertQueryEquals("-guinea pig", a, "-guinea pig");
    assertQueryEquals("!guinea pig", a, "-guinea pig");
    assertQueryEquals("guinea* pig", a, "guinea* pig");
    assertQueryEquals("guinea? pig", a, "guinea? pig");
    assertQueryEquals("guinea~2 pig", a, "guinea~2 pig");
    assertQueryEquals("guinea^2 pig", a, "(guinea)^2.0 pig");
    assertQueryEquals("guinea +pig", a, "guinea +pig");
    assertQueryEquals("guinea -pig", a, "guinea -pig");
    assertQueryEquals("guinea !pig", a, "guinea -pig");
    assertQueryEquals("guinea pig*", a, "guinea pig*");
    assertQueryEquals("guinea pig?", a, "guinea pig?");
    assertQueryEquals("guinea pig~2", a, "guinea pig~2");
    assertQueryEquals("guinea pig^2", a, "guinea (pig)^2.0");
    assertQueryEquals("field:guinea pig", a, "guinea pig");
    assertQueryEquals("guinea field:pig", a, "guinea pig");
    assertQueryEquals("NOT guinea pig", a, "-guinea pig");
    assertQueryEquals("guinea NOT pig", a, "guinea -pig");
    assertQueryEquals("guinea pig AND dogs", a, "guinea +pig +Synonym(dog dogs)");
    assertQueryEquals("dogs AND guinea pig", a, "+Synonym(dog dogs) +guinea pig");
    assertQueryEquals("guinea pig && dogs", a, "guinea +pig +Synonym(dog dogs)");
    assertQueryEquals("dogs && guinea pig", a, "+Synonym(dog dogs) +guinea pig");
    assertQueryEquals("guinea pig OR dogs", a, "guinea pig Synonym(dog dogs)");
    assertQueryEquals("dogs OR guinea pig", a, "Synonym(dog dogs) guinea pig");
    assertQueryEquals("guinea pig || dogs", a, "guinea pig Synonym(dog dogs)");
    assertQueryEquals("dogs || guinea pig", a, "Synonym(dog dogs) guinea pig");
    assertQueryEquals("\"guinea\" pig", a, "guinea pig");
    assertQueryEquals("guinea \"pig\"", a, "guinea pig");
    assertQueryEquals("(guinea) pig", a, "guinea pig");
    assertQueryEquals("guinea (pig)", a, "guinea pig");
    assertQueryEquals("/guinea/ pig", a, "/guinea/ pig");
    assertQueryEquals("guinea /pig/", a, "guinea /pig/");
    assertQueryEquals("(guinea pig)", a, "guinea pig");
    assertQueryEquals("+(guinea pig)", a, "+(guinea pig)");
    assertQueryEquals("-(guinea pig)", a, "-(guinea pig)");
    assertQueryEquals("!(guinea pig)", a, "-(guinea pig)");
    assertQueryEquals("NOT (guinea pig)", a, "-(guinea pig)");
    assertQueryEquals("(guinea pig)^2", a, "(guinea pig)^2.0");
    assertQueryEquals("field:(guinea pig)", a, "guinea pig");
    assertQueryEquals("+small guinea pig", a, "+small guinea pig");
    assertQueryEquals("-small guinea pig", a, "-small guinea pig");
    assertQueryEquals("!small guinea pig", a, "-small guinea pig");
    assertQueryEquals("NOT small guinea pig", a, "-small guinea pig");
    assertQueryEquals("small* guinea pig", a, "small* guinea pig");
    assertQueryEquals("small? guinea pig", a, "small? guinea pig");
    assertQueryEquals("\"small\" guinea pig", a, "small guinea pig");
    assertQueryEquals("guinea pig +running", a, "guinea pig +running");
    assertQueryEquals("guinea pig -running", a, "guinea pig -running");
    assertQueryEquals("guinea pig !running", a, "guinea pig -running");
    assertQueryEquals("guinea pig NOT running", a, "guinea pig -running");
    assertQueryEquals("guinea pig running*", a, "guinea pig running*");
    assertQueryEquals("guinea pig running?", a, "guinea pig running?");
    assertQueryEquals("guinea pig \"running\"", a, "guinea pig running");
    assertQueryEquals("\"guinea pig\"~2", a, "spanOr([spanNear([guinea, pig], 0, true), cavy])");
    assertQueryEquals("field:\"guinea pig\"", a, "spanOr([spanNear([guinea, pig], 0, true), cavy])");
    splitOnWhitespace = oldSplitOnWhitespace;
}
Also used : MockSynonymAnalyzer(org.apache.lucene.analysis.MockSynonymAnalyzer) MockBytesAnalyzer(org.apache.lucene.analysis.MockBytesAnalyzer) MockSynonymAnalyzer(org.apache.lucene.analysis.MockSynonymAnalyzer) Analyzer(org.apache.lucene.analysis.Analyzer) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer)

Aggregations

MockSynonymAnalyzer (org.apache.lucene.analysis.MockSynonymAnalyzer)8 Term (org.apache.lucene.index.Term)5 BooleanQuery (org.apache.lucene.search.BooleanQuery)4 TermQuery (org.apache.lucene.search.TermQuery)4 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)4 BoostQuery (org.apache.lucene.search.BoostQuery)3 Query (org.apache.lucene.search.Query)3 SynonymQuery (org.apache.lucene.search.SynonymQuery)3 SpanNearQuery (org.apache.lucene.search.spans.SpanNearQuery)3 SpanOrQuery (org.apache.lucene.search.spans.SpanOrQuery)3 Analyzer (org.apache.lucene.analysis.Analyzer)2 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)2 MockBytesAnalyzer (org.apache.lucene.analysis.MockBytesAnalyzer)2 BooleanClause (org.apache.lucene.search.BooleanClause)2 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)2 MultiPhraseQuery (org.apache.lucene.search.MultiPhraseQuery)2 PhraseQuery (org.apache.lucene.search.PhraseQuery)2 PrefixQuery (org.apache.lucene.search.PrefixQuery)2 SpanQuery (org.apache.lucene.search.spans.SpanQuery)2 HashMap (java.util.HashMap)1