I believe I have aged a few years trying to figure out the best way to do this.

For various reasons, I'm reduced to writing my own site search engine.

I've already made a stab at an indexer, and I'm happy with the results.

The thing that's bugging me is how to create a search query parser that allows for parenthesis, "and", "or", and "not" matching. After reading the Cookbook and this article on perl.com, I have some ideas but I'm losing it on the implementation.

Here's what I want to do:

  1. Step through the search query by word, from left to right. I've got code that does this nicely.
  2. Look the word up in the search index, retrieve the doc ids it's in into an array.
  3. If a word begins with "(" send everything up to the next ")" to a second level match, that returns an array to be added (in the case of an "or") or subtracted (in the case of an "and") to the match array
  4. Repeat until done.

The thing that's mostly eluding me is step 3- how to send everything from "(" to ")" somewhere.

The code I'm using to walk through the string word-by-word is:

foreach($query =~ /\b([A-Za-z'\d.@\/:-]+)\b/g){ #do stuff here }

And this works pretty well. Any help is appreciated, especially if you know some CPAN modules that would simplify this. I've played with Text::Balanced and it's partially what I need.

-Any sufficiently advanced technology is
indistinguishable from doubletalk.


In reply to Search engine query parsing by Hero Zzyzzx

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.