Example usage for org.apache.lucene.search Query Query

List of usage examples for org.apache.lucene.search Query Query

Introduction

In this page you can find the example usage for org.apache.lucene.search Query Query.

Prototype

Query

Source Link

Usage

From source file:com.ibm.watson.developer_cloud.professor_languo.pipeline.primary_search.RetrieveAndRankSearcherTest.java

License:Open Source License

/**
 * Create a query from a string and set it to the query field
 * /*  w  w  w . j a v a  2  s  .c om*/
 * @param query - A string for the query
 */
private void create_query(final String query) {
    this.query = new Query() {
        @Override
        public String toString(String arg0) {
            return query;
        }
    };
}

From source file:com.meltmedia.cadmium.search.SearchServiceTest.java

License:Apache License

@Before
public void setupIndexReader() throws Exception {
    searcherProvider = mock(IndexSearcherProvider.class);
    index = mock(IndexSearcher.class);
    parser = mock(QueryParser.class);

    when(parser.parse("good_query")).thenReturn(new Query() {

        @Override//from   w w w.  ja v a2s  . c o m
        public String toString(String arg0) {
            return "good";
        }
    });
    when(parser.parse("bad_query")).thenReturn(new Query() {

        @Override
        public String toString(String arg0) {
            return "bad";
        }
    });

    TopDocs hasResults = new TopDocs(5, new ScoreDoc[] { new ScoreDoc(1, 1.1f), new ScoreDoc(2, 1.1f),
            new ScoreDoc(3, 1.1f), new ScoreDoc(4, 1.1f), new ScoreDoc(5, 1.1f) }, 5.5f);

    TopDocs noResults = new TopDocs(0, new ScoreDoc[] {}, 0.0f);

    when(index.search(parser.parse("good_query"), null, 100000)).thenReturn(hasResults);
    when(index.search(parser.parse("bad_query"), null, 100000)).thenReturn(noResults);
    Document one = new Document();
    one.add(new TextField("path", "1", Field.Store.YES));
    Document two = new Document();
    two.add(new TextField("path", "2", Field.Store.YES));
    Document three = new Document();
    three.add(new TextField("path", "3", Field.Store.YES));
    Document four = new Document();
    four.add(new TextField("path", "4", Field.Store.YES));
    Document five = new Document();
    five.add(new TextField("path", "5", Field.Store.YES));

    docs = new Document[] { one, two, three, four, five };

    when(index.doc(1)).thenReturn(one);
    when(index.doc(2)).thenReturn(two);
    when(index.doc(3)).thenReturn(three);
    when(index.doc(4)).thenReturn(four);
    when(index.doc(5)).thenReturn(five);

    when(searcherProvider.startSearch()).thenReturn(index);

    service = new SearchService() {
        @Override
        QueryParser createParser(Analyzer analyzer) {
            return parser;
        }
    };
    service.setIndexSearchProvider(searcherProvider);
}

From source file:it.intext.pattern.gindex.SpanSimpleTerm.java

License:Apache License

public Query makeLuceneQueryFieldNoBoost(final String fieldName, final BasicQueryFactory qf) {
    return new Query() {

        private static final long serialVersionUID = -5797231896662175334L;

        public String toString(String fn) {
            return getClass().toString() + " " + fieldName + " (" + fn + "?)";
        }//from w  ww  . j a v a  2 s  . co  m

        public Query rewrite(IndexReader reader) throws IOException {
            final List<Query> luceneSubQueries = new ArrayList<Query>();
            visitMatchingTerms(reader, fieldName, new MatchingTermVisitor() {
                public void visitMatchingTerm(Term term) throws IOException {
                    luceneSubQueries.add(qf.newSpanTermQuery(term));
                }
            });
            return (luceneSubQueries.size() == 0) ? SrndQuery.theEmptyLcnQuery
                    : (luceneSubQueries.size() == 1) ? (Query) luceneSubQueries.get(0)
                            : makeBooleanQuery(
                                    /* luceneSubQueries all have default weight */
                                    luceneSubQueries, BooleanClause.Occur.SHOULD); /* OR the subquery terms */
        }
    };
}

From source file:uk.co.flax.luwak.matchers.TestHighlightingMatcher.java

License:Apache License

@Test
public void testQueryErrors() throws IOException {

    monitor = new Monitor(new MonitorQueryParser() {
        @Override/*  ww w  .  ja  v  a  2 s.  co  m*/
        public Query parse(String queryString, Map<String, String> metadata) throws Exception {
            if (queryString.equals("error!")) {
                return new Query() {
                    @Override
                    public String toString(String field) {
                        return "";
                    }

                    @Override
                    public Query rewrite(IndexReader reader) throws IOException {
                        throw new RuntimeException("Oops!");
                    }
                };
            }
            return new LuceneQueryParser(textfield).parse(queryString, metadata);
        }
    }, new MatchAllPresearcher());

    monitor.update(new MonitorQuery("1", "test"), new MonitorQuery("2", "error!"),
            new MonitorQuery("3", "document"), new MonitorQuery("4", "foo"));

    assertThat(monitor.match(buildDoc("doc1", "this is a test document"), HighlightingMatcher.FACTORY))
            .hasQueriesRunCount(4).hasMatchCount("doc1", 2).hasErrorCount(1);
}