Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8183039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:02:23+00:00 2026-06-07T01:02:23+00:00

I have the following program: public class RegexQueryExample { public static String[] terms =

  • 0

I have the following program:

public class RegexQueryExample {

    public static String[] terms = {
        "US $65M dollars",
        "USA",
        "$35",
        "355",
        "US $33",
        "U.S.A",
        "John Keates",
        "Tom Dick Harry",
        "Southeast' Asia"
    };
    private static Directory directory;

    public static void main(String[] args) throws CorruptIndexException, IOException {
        String searchString = ".*\\$.*";
        createIndex();
        searchRegexIndex(searchString);
    }

    /**
     * Creates an index for the files in the data directory.
     */
    private static void createIndex() throws CorruptIndexException, LockObtainFailedException, IOException {

        Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);
        directory = new RAMDirectory();
        IndexWriter indexWriter = new IndexWriter(directory, analyzer, IndexWriter.MaxFieldLength.UNLIMITED);


        for (String term : terms) {
            Document document = new Document();
            if (term.indexOf('$') >= 0) {
                document.add(new Field("type", "currency", Field.Store.YES, Field.Index.NOT_ANALYZED));
            } else {
                document.add(new Field("type", "simple_field", Field.Store.YES, Field.Index.NOT_ANALYZED));
            }
            document.add(new Field("term", term, Field.Store.YES, Field.Index.NOT_ANALYZED));
            indexWriter.addDocument(document);
        }

        indexWriter.close();
    }

    /**
     * searches for a regular expression satisfied by a file path.
     *
     * @param searchString the string to be searched.
     */
    private static void searchRegexIndex(String regexString) throws CorruptIndexException, IOException {
        regexString = regexString;
        IndexSearcher searcher = new IndexSearcher(directory);

        RegexQuery rquery = new RegexQuery(new Term("term", regexString));
        BooleanQuery queryin = new BooleanQuery();
        BooleanQuery query = new BooleanQuery();
        query.add(new TermQuery(new Term("type", "simple_field")), BooleanClause.Occur.MUST);
        query.add(rquery, BooleanClause.Occur.MUST);

        TopDocs hits = searcher.search(query, terms.length);
        ScoreDoc[] alldocs = hits.scoreDocs;
        for (int i = 0; i < alldocs.length; i++) {
            Document d = searcher.doc(alldocs[i].doc);
            System.out.println((i + 1) + ". " + d.get("term"));
        }
    }
}

The createIndex() function creates the Lucene index while searchRegexIndex() performs a regex query. In the main() function I search for .*\\$.* expecting it to return the terms containing the $ sign. But, it did not work. How do I make it work? Is this some problem with the Analyzer?

Edit:

My Lucene index snapshot from Luke:

Lucene Index

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T01:02:24+00:00Added an answer on June 7, 2026 at 1:02 am

    You are using StandardAnalyzer, which removes the dollar signs from the tokens. E.g. “US $65M dollars” becomes three tokens: “us”, “65m”, “dollars”. You need to use another analyzer that does not remove the dollar signs. Luke provides an excellent analyzer tool in which you can try out different analyzers and check their outputs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: public class Foo {} static class Program { [XmlElement(foo)]
I have the following program import java.util.*; public class Test { public static void
In the following program. public class SerialExample { public static void main(String args[]) throws
I have the following code: class Program { static void Main(string[] args) { string
If you have the following: Public Class Person { public int Id; Public String
I have the following example relationship: namespace Yesod { public class Program { //
I have the following class in my program: public class RZoom extends Activity {
I have the following code: public class Events extends ListActivity { //... private static
I have the following code: using System; using System.Linq; using System.Linq.Expressions; public class Program
I have the following in a program (written in VB.NET): Imports Microsoft.Office.Interop.Excel Public Class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.