Search in sources :

Example 6 with QueryParser

use of org.apache.lucene.queryParser.QueryParser in project Openfire by igniterealtime.

the class WordMatchRouter method checkForHits.

/**
     * Returns true if the query string matches results in the request map.
     *
     * @param requestMap the map of request meta data. Each map key is a String with a value
     *      of a list of Strings.
     * @param queryString the query to test against the map.
     * @return true if the query string matches the request.
     */
public boolean checkForHits(Map<String, List<String>> requestMap, String queryString) {
    // Enable stemming.
    setStemmingEnabled(true);
    boolean foundMatch = false;
    try {
        // Create an in-memory directory.
        RAMDirectory dir = new RAMDirectory();
        // Index the message.
        IndexWriter writer = new IndexWriter(dir, analyzer, true);
        BooleanQuery booleanQuery = new BooleanQuery();
        Document doc = new Document();
        for (String key : requestMap.keySet()) {
            List<String> keyValue = requestMap.get(key);
            if (keyValue != null) {
                StringBuilder builder = new StringBuilder();
                for (String value : keyValue) {
                    if (ModelUtil.hasLength(value)) {
                        builder.append(value);
                        builder.append(" ");
                    }
                }
                // Add to Search Indexer
                doc.add(new Field(key, builder.toString(), Field.Store.YES, Field.Index.TOKENIZED));
                QueryParser parser = new QueryParser(key, analyzer);
                Query query = parser.parse(queryString);
                booleanQuery.add(query, BooleanClause.Occur.MUST);
            }
        }
        writer.addDocument(doc);
        writer.close();
        // Create a searcher, try to find a match.
        IndexSearcher searcher = new IndexSearcher(dir);
        Hits hits = searcher.search(booleanQuery);
        // Check to see if a match was found.
        if (hits.length() > 0) {
            foundMatch = true;
        }
        searcher.close();
    } catch (Exception e) {
        Log.error(e.getMessage(), e);
    }
    return foundMatch;
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) BooleanQuery(org.apache.lucene.search.BooleanQuery) Hits(org.apache.lucene.search.Hits) Query(org.apache.lucene.search.Query) BooleanQuery(org.apache.lucene.search.BooleanQuery) Document(org.apache.lucene.document.Document) RAMDirectory(org.apache.lucene.store.RAMDirectory) Field(org.apache.lucene.document.Field) QueryParser(org.apache.lucene.queryParser.QueryParser) IndexWriter(org.apache.lucene.index.IndexWriter)

Example 7 with QueryParser

use of org.apache.lucene.queryParser.QueryParser in project Openfire by igniterealtime.

the class ArchiveSearcher method luceneSearch.

/**
     * Searches the Lucene index for all archived conversations using the specified search.
     *
     * @param search the search.
     * @return the collection of conversations that match the search.
     */
private Collection<Conversation> luceneSearch(ArchiveSearch search) {
    try {
        IndexSearcher searcher = archiveIndexer.getSearcher();
        final StandardAnalyzer analyzer = new StandardAnalyzer();
        // Create the query based on the search terms.
        Query query = new QueryParser("text", analyzer).parse(search.getQueryString());
        // See if the user wants to sort on something other than relevance. If so, we need
        // to tell Lucene to do sorting. Default to a null sort so that it has no
        // effect if sorting hasn't been selected.
        Sort sort = null;
        if (search.getSortField() != ArchiveSearch.SortField.relevance) {
            if (search.getSortField() == ArchiveSearch.SortField.date) {
                sort = new Sort("date", search.getSortOrder() == ArchiveSearch.SortOrder.descending);
            }
        }
        // See if we need to filter on date. Default to a null filter so that it has
        // no effect if date filtering hasn't been selected.
        Filter filter = null;
        if (search.getDateRangeMin() != null || search.getDateRangeMax() != null) {
            String min = null;
            if (search.getDateRangeMin() != null) {
                min = DateTools.dateToString(search.getDateRangeMin(), DateTools.Resolution.DAY);
            }
            String max = null;
            if (search.getDateRangeMax() != null) {
                max = DateTools.dateToString(search.getDateRangeMax(), DateTools.Resolution.DAY);
            }
            // ENT-271: don't include upper or lower bound if these elements are null
            filter = new RangeFilter("date", min, max, min != null, max != null);
        }
        // See if we need to match external conversations. This will only be true
        // when less than two conversation participants are specified and external
        // wildcard matching is enabled.
        Collection<JID> participants = search.getParticipants();
        if (search.getParticipants().size() < 2 && search.isExternalWildcardMode()) {
            TermQuery externalQuery = new TermQuery(new Term("external", "true"));
            // Add this query to the existing query.
            BooleanQuery booleanQuery = new BooleanQuery();
            booleanQuery.add(query, BooleanClause.Occur.MUST);
            booleanQuery.add(externalQuery, BooleanClause.Occur.MUST);
            query = booleanQuery;
        }
        // See if we need to restrict the search to certain users.
        if (!participants.isEmpty()) {
            if (participants.size() == 1) {
                String jid = participants.iterator().next().toBareJID();
                Query participantQuery = new QueryParser("jid", analyzer).parse(jid);
                // Add this query to the existing query.
                BooleanQuery booleanQuery = new BooleanQuery();
                booleanQuery.add(query, BooleanClause.Occur.MUST);
                booleanQuery.add(participantQuery, BooleanClause.Occur.MUST);
                query = booleanQuery;
            } else // Otherwise there are two participants.
            {
                Iterator<JID> iter = participants.iterator();
                String participant1 = iter.next().toBareJID();
                String participant2 = iter.next().toBareJID();
                BooleanQuery participantQuery = new BooleanQuery();
                participantQuery.add(new QueryParser("jid", analyzer).parse(participant1), BooleanClause.Occur.MUST);
                participantQuery.add(new QueryParser("jid", analyzer).parse(participant2), BooleanClause.Occur.MUST);
                // Add this query to the existing query.
                BooleanQuery booleanQuery = new BooleanQuery();
                booleanQuery.add(query, BooleanClause.Occur.MUST);
                booleanQuery.add(participantQuery, BooleanClause.Occur.MUST);
                query = booleanQuery;
            }
        }
        Hits hits = searcher.search(query, filter, sort);
        int startIndex = search.getStartIndex();
        int endIndex = startIndex + search.getNumResults() - 1;
        // The end index can't be after the end of the results.
        if (endIndex > hits.length() - 1) {
        // endIndex = hits.length() - 1;
        // TODO: We need to determine if this is necessary.
        }
        // If the start index is positioned after the end, return an empty list.
        if (((endIndex - startIndex) + 1) <= 0) {
            return Collections.emptyList();
        } else // Otherwise return the results.
        {
            return new LuceneQueryResults(hits, startIndex, endIndex);
        }
    } catch (ParseException pe) {
        Log.error(pe.getMessage(), pe);
        return Collections.emptySet();
    } catch (IOException ioe) {
        Log.error(ioe.getMessage(), ioe);
        return Collections.emptySet();
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) Hits(org.apache.lucene.search.Hits) Query(org.apache.lucene.search.Query) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) JID(org.xmpp.packet.JID) Term(org.apache.lucene.index.Term) IOException(java.io.IOException) RangeFilter(org.apache.lucene.search.RangeFilter) QueryParser(org.apache.lucene.queryParser.QueryParser) RangeFilter(org.apache.lucene.search.RangeFilter) Filter(org.apache.lucene.search.Filter) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) Sort(org.apache.lucene.search.Sort) ParseException(org.apache.lucene.queryParser.ParseException)

Example 8 with QueryParser

use of org.apache.lucene.queryParser.QueryParser in project graphdb by neo4j-attic.

the class IndexType method query.

Query query(String keyOrNull, Object value, QueryContext contextOrNull) {
    if (value instanceof Query) {
        return (Query) value;
    }
    QueryParser parser = new QueryParser(Version.LUCENE_30, keyOrNull, analyzer);
    parser.setAllowLeadingWildcard(true);
    parser.setLowercaseExpandedTerms(toLowerCase);
    if (contextOrNull != null && contextOrNull.getDefaultOperator() != null) {
        parser.setDefaultOperator(contextOrNull.getDefaultOperator());
    }
    try {
        return parser.parse(value.toString());
    } catch (ParseException e) {
        throw new RuntimeException(e);
    }
}
Also used : QueryParser(org.apache.lucene.queryParser.QueryParser) Query(org.apache.lucene.search.Query) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) ParseException(org.apache.lucene.queryParser.ParseException)

Example 9 with QueryParser

use of org.apache.lucene.queryParser.QueryParser in project ansj_seg by NLPchina.

the class NearTest method testSearch.

//	public static void createIndex() throws Exception {
//
//		IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_32, new AnsjAnalysis());
//		Directory directory = FSDirectory.open(new File("index"));
//		IndexWriter writer = new IndexWriter(directory, conf);
//
//		String str = "文化人;文化人谈文化";
//		String[] values = str.split(";");
//		List<Document> docs = new ArrayList<Document>();
//		for (String value : values) {
//			Document doc = new Document();
//			Field field = new Field("test", value, Store.YES, Index.ANALYZED_NO_NORMS, TermVector.WITH_POSITIONS_OFFSETS);
//			// field.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);
//			doc.add(field);
//			docs.add(doc);
//		}
//		writer.addDocuments(docs);
//		writer.commit();
//		writer.close();
//	}
public static void testSearch() throws Exception {
    String rules = "\"文化人谈文化\"~10";
    IndexReader reader = IndexReader.open(FSDirectory.open(new File("index")));
    IndexSearcher searcher = new IndexSearcher(reader);
    QueryParser parser = new QueryParser(Version.LUCENE_32, "test", new AnsjAnalysis());
    Query query = parser.parse(rules);
    System.out.println(query.toString());
    TopDocs topDocs = searcher.search(query, 10);
    ScoreDoc[] docs = topDocs.scoreDocs;
    System.out.println(docs.length);
    for (ScoreDoc scoreDoc : docs) {
        Document doc = searcher.doc(scoreDoc.doc);
        Explanation explain = searcher.explain(query, scoreDoc.doc);
        System.out.println(explain);
        System.out.println(doc);
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TopDocs(org.apache.lucene.search.TopDocs) QueryParser(org.apache.lucene.queryParser.QueryParser) Query(org.apache.lucene.search.Query) Explanation(org.apache.lucene.search.Explanation) IndexReader(org.apache.lucene.index.IndexReader) Document(org.apache.lucene.document.Document) File(java.io.File) ScoreDoc(org.apache.lucene.search.ScoreDoc)

Example 10 with QueryParser

use of org.apache.lucene.queryParser.QueryParser in project zm-mailbox by Zimbra.

the class AbstractIndexStoreTest method booleanQuery.

@Test
public void booleanQuery() throws Exception {
    ZimbraLog.test.debug("--->TEST booleanQuery");
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
    Contact contact = createContact(mbox, "First", "Last", "f.last@zimbra.com", "Software Development Engineer");
    createContact(mbox, "Given", "Surname", "GiV.SurN@zimbra.com");
    // Make sure all indexing has been done
    mbox.index.indexDeferredItems();
    IndexStore index = mbox.index.getIndexStore();
    ZimbraIndexSearcher searcher = index.openSearcher();
    // This seems to be the supported way of enabling leading wildcard queries
    QueryParser queryParser = new QueryParser(LuceneIndex.VERSION, LuceneFields.L_CONTACT_DATA, new StandardAnalyzer(LuceneIndex.VERSION));
    queryParser.setAllowLeadingWildcard(true);
    Query wquery = queryParser.parse("*irst");
    Query tquery = new TermQuery(new Term(LuceneFields.L_CONTACT_DATA, "absent"));
    Query tquery2 = new TermQuery(new Term(LuceneFields.L_CONTACT_DATA, "Last"));
    BooleanQuery bquery = new BooleanQuery();
    bquery.add(wquery, Occur.MUST);
    bquery.add(tquery, Occur.MUST_NOT);
    bquery.add(tquery2, Occur.SHOULD);
    ZimbraTopDocs result = searcher.search(bquery, 100);
    Assert.assertNotNull("searcher.search result object", result);
    ZimbraLog.test.debug("Result for search [hits=%d]:%s", result.getTotalHits(), result.toString());
    Assert.assertEquals("Number of hits", 1, result.getTotalHits());
    String expected1Id = String.valueOf(contact.getId());
    String match1Id = searcher.doc(result.getScoreDoc(0).getDocumentID()).get(LuceneFields.L_MAILBOX_BLOB_ID);
    Assert.assertEquals("Mailbox Blob ID of match", expected1Id, match1Id);
}
Also used : TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) QueryParser(org.apache.lucene.queryParser.QueryParser) Mailbox(com.zimbra.cs.mailbox.Mailbox) Query(org.apache.lucene.search.Query) PhraseQuery(org.apache.lucene.search.PhraseQuery) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) Term(org.apache.lucene.index.Term) Contact(com.zimbra.cs.mailbox.Contact) ParsedContact(com.zimbra.cs.mime.ParsedContact) Test(org.junit.Test)

Aggregations

QueryParser (org.apache.lucene.queryParser.QueryParser)14 Query (org.apache.lucene.search.Query)11 ParseException (org.apache.lucene.queryParser.ParseException)8 BooleanQuery (org.apache.lucene.search.BooleanQuery)6 TermQuery (org.apache.lucene.search.TermQuery)6 IOException (java.io.IOException)4 StandardAnalyzer (org.apache.lucene.analysis.standard.StandardAnalyzer)4 IndexSearcher (org.apache.lucene.search.IndexSearcher)4 Name (org.apache.jackrabbit.spi.Name)3 Hits (org.apache.lucene.search.Hits)3 Contact (com.zimbra.cs.mailbox.Contact)2 Mailbox (com.zimbra.cs.mailbox.Mailbox)2 ParsedContact (com.zimbra.cs.mime.ParsedContact)2 RepositoryException (javax.jcr.RepositoryException)2 NodeLocalName (javax.jcr.query.qom.NodeLocalName)2 NodeName (javax.jcr.query.qom.NodeName)2 StaticOperand (javax.jcr.query.qom.StaticOperand)2 Document (org.apache.lucene.document.Document)2 Term (org.apache.lucene.index.Term)2 MultiPhraseQuery (org.apache.lucene.search.MultiPhraseQuery)2