[KinoSearch] Best way to set default_boolop to 'AND'

Marvin Humphrey marvin at rectangular.com
Thu Mar 15 08:50:47 PST 2007


On Mar 14, 2007, at 10:04 AM, Dmitri Tikhonov wrote:

> This is a large departure from the original code:
>
>   my $hits = $searcher->search(query => $query);
>
> which does not care if it knows the names of all the fields [1].

Searcher->search has a simple interface because it makes many, many  
assumptions about what the default behavior should be.  It is an  
"easy thing easy, hard things possible" API design.

It would not be possible to concentrate all conceivable options in a  
single interface; adding even a couple (e.g. default_boolop, fields)  
would clutter things up while still covering only a fraction of the  
potential configurations.

Therefore, any time you need to override the defaults of Searcher- 
 >search, it is necessarily going to involve a significant  
departure.  Nevertheless, I agree that your particular use case could  
be improved.

> Question: is this the best way to do this or am I missing somthing
> obvious?

There are slightly less cumbersome ways of generating the list of  
fields in 0.15, but they all rely on internal APIs.  IndexReader and  
Searcher actually have get_fields() methods, which Searcher uses for  
precisely this purpose.  Take a look at the code in Searcher- 
 >_prepare_simple_search().

In the development branch of KS (which I don't know if you are  
familiar with; the most recent CPAN release is 0.20_02), the  
interface has changed.

http://www.rectangular.com/kinosearch/docs/devel/KinoSearch/ 
QueryParser/QueryParser.html

If you are not adding fields dynamically, then your code could be  
rewritten as

   my $hits = $searcher->search(query =>
     KinoSearch::QueryParser::QueryParser->new(
       schema         => MySchema->new,
       default_boolop => 'AND',
     )->parse($query)
   );

If you *are* adding fields dynamically, you need to make sure that  
the Schema instance you pass to QueryParser knows about them.   
Calling MySchema->open() has the side effect of loading up the Schema  
object with all fields which were added dynamically on all prior  
indexing passes:

   my $schema   = MySchema->new;
   my $invindex = $schema->open('/path/to/invindex'); # adds dynamic  
fields
   my $searcher = KinoSearch::Searcher->new( invindex => $invindex );

   my $hits = $searcher->search(query =>
     KinoSearch::QueryParser::QueryParser->new(
       schema         => $schema,
       default_boolop => 'AND',
     )->parse($query)
   );

> 1. By the way, why not?

[ this quote refer to footnote 1, rather than a list item ]

The default is to search all indexed fields.  If that's not  
appropriate, you have the option of overriding.

Marvin Humphrey
Rectangular Research
http://www.rectangular.com/





More information about the KinoSearch mailing list