Author: rooneg
Date: Sun Mar  6 08:04:12 2005
New Revision: 156323

URL: http://svn.apache.org/viewcvs?view=rev&rev=156323
Log:
Add a 'search' command to the command line tool, which runs a query over
an index and prints a specified field from each hit.

At the moment this is actually implemented using the scorer interface, but
that'll change once we have a searcher interface implemented.

* src/cmdline/main.c
  (dispatch_table): add lcn_search_cmd.
  (lcn_search_cmd): new command.

* src/query_parser/parser.c
  (lcn_query_parser_parse): use -1 as the "i don't have a previous index"
   marker instead of zero, so we can deal with queries that consist of a
   single term.

Modified:
    incubator/lucene4c/trunk/src/cmdline/main.c
    incubator/lucene4c/trunk/src/query_parser/parser.c

Modified: incubator/lucene4c/trunk/src/cmdline/main.c
URL: 
http://svn.apache.org/viewcvs/incubator/lucene4c/trunk/src/cmdline/main.c?view=diff&r1=156322&r2=156323
==============================================================================
--- incubator/lucene4c/trunk/src/cmdline/main.c (original)
+++ incubator/lucene4c/trunk/src/cmdline/main.c Sun Mar  6 08:04:12 2005
@@ -23,12 +23,13 @@
 #include "lcn_index.h"
 #include "lcn_types.h"
 #include "lcn_segments.h"
+#include "lcn_query_parser.h"
 
 typedef lcn_error_t *(lcn_subcommand_t) (int argc,
                                          char *argv[],
                                          apr_pool_t *pool);
 
-lcn_subcommand_t lcn_segments_cmd, lcn_termdocs_cmd;
+lcn_subcommand_t lcn_segments_cmd, lcn_termdocs_cmd, lcn_search_cmd;
 
 typedef struct {
   const char *name;
@@ -38,6 +39,7 @@
 subcommand_desc_t dispatch_table[] = {
   { "segments", lcn_segments_cmd },
   { "termdocs", lcn_termdocs_cmd },
+  { "search", lcn_search_cmd },
   { NULL }
 };
 
@@ -155,6 +157,62 @@
         }
 
       LCN_ERR (lcn_doc_iter_close (itr));
+
+      lcn_pool_destroy (subpool);
+
+      return LCN_NO_ERROR;
+    }
+}
+
+lcn_error_t *
+lcn_search_cmd (int argc, char *argv[], apr_pool_t *pool)
+{
+  if (argc != 3)
+    {
+      printf ("usage: lcn search <index> <query> <pfield>\n");
+      return LCN_NO_ERROR;
+    }
+  else
+    {
+      lcn_char_t *pfield = lcn_str_from_cstring (argv[2], pool);
+      apr_pool_t *subpool = lcn_pool_create (pool);
+      lcn_query_parser_t *parser;
+      lcn_scorer_t *scorer;
+      lcn_boolean_t next;
+      lcn_query_t *query;
+      lcn_index_t *idx;
+      lcn_error_t *err;
+
+      LCN_ERR (lcn_index_open (&idx, argv[0], pool));
+
+      LCN_ERR (lcn_query_parser_create (&parser,
+                                        lcn_str_from_cstring ("contents",
+                                                              pool),
+                                        LCN_MUST,
+                                        pool));
+
+      LCN_ERR (lcn_query_parser_parse (&query,
+                                       parser,
+                                       lcn_str_from_cstring (argv[1], pool),
+                                       pool));
+
+      /* XXX eventually we want to use a searcher, not a scorer. */
+      LCN_ERR (lcn_query_scorer (&scorer, query, idx, pool));
+
+      LCN_ERR (print_doc_field (idx,
+                                lcn_scorer_doc (scorer),
+                                pfield,
+                                subpool));
+
+      while ((err = lcn_scorer_next (&next, scorer)) == LCN_NO_ERROR && next)
+        {
+          apr_pool_clear (subpool);
+
+          LCN_ERR (print_doc_field (idx,
+                                    lcn_scorer_doc (scorer),
+                                    pfield,
+                                    subpool));
+        }
 
       lcn_pool_destroy (subpool);
 

Modified: incubator/lucene4c/trunk/src/query_parser/parser.c
URL: 
http://svn.apache.org/viewcvs/incubator/lucene4c/trunk/src/query_parser/parser.c?view=diff&r1=156322&r2=156323
==============================================================================
--- incubator/lucene4c/trunk/src/query_parser/parser.c (original)
+++ incubator/lucene4c/trunk/src/query_parser/parser.c Sun Mar  6 08:04:12 2005
@@ -50,7 +50,7 @@
   apr_array_header_t *terms = apr_array_make (subpool,
                                               5,
                                               sizeof (lcn_char_t *));
-  int idx, prev = 0;
+  int idx, prev = -1;
 
   /* XXX this will need to be reworked to take into account non-default
    * fields, grouped terms via parentheses, and probably a ton of other
@@ -60,13 +60,10 @@
     {
       if (input[idx] == ' ')
         {
-          if (prev)
+          if (prev != -1)
             {
               lcn_char_t *tmp;
 
-              if (prev == 1)
-                prev = 0;
-
               tmp = apr_pcalloc (subpool,
                                  (idx - prev + 1) * sizeof (lcn_char_t));
 
@@ -74,21 +71,23 @@
 
               APR_ARRAY_PUSH (terms, lcn_char_t *) = tmp;
 
-              prev = 0;
+              prev = -1;
             }
         }
       else
         {
-          if (prev == 0)
+          if (prev == -1)
             prev = idx;
         }
     }
 
-  if (prev)
+  if (prev != -1)
     {
-      lcn_char_t *tmp
-        = apr_pcalloc (subpool,
-                       (lcn_strlen (input) - prev + 1) * sizeof (lcn_char_t));
+      lcn_char_t *tmp;
+
+      tmp = apr_pcalloc (subpool,
+                         (lcn_strlen (input) - prev + 1)
+                         * sizeof (lcn_char_t));
 
       memcpy (tmp,
               input + prev,
@@ -125,7 +124,7 @@
         for (idx = 0; idx < terms->nelts; ++idx)
           {
             lcn_query_t *tq;
-            
+ 
             LCN_ERR (lcn_term_query_create
                       (&tq,
                        lcn_term_create (APR_ARRAY_IDX (terms,


Reply via email to