recollq: added option -p to be used with -A for showing page-numbered snippets instead of abstract

This commit is contained in:
Jean-Francois Dockes 2020-08-29 09:43:21 +02:00
parent 92f636be39
commit 20e845709e
3 changed files with 93 additions and 69 deletions

View File

@ -25,6 +25,9 @@ recollq \- command line / standard output Recoll query command.
.B \-A
]
[
.B \-p
]
[
.B \-e
]
[
@ -106,7 +109,9 @@ is set, the whole metadata array will be dumped for each document.
.PP
If
.B \-A
is set, the document abstracts will be printed.
is set, the document abstracts will be printed. With an additional
.B \-p
option, snippets with page numbers (when available) will be shown instead.
.PP
.B \-S
<fieldname>

View File

@ -24,6 +24,7 @@
#include <limits.h>
#include <iostream>
#include <sstream>
#include <list>
#include <string>
@ -57,8 +58,27 @@ bool dump_contents(RclConfig *rclconfig, Rcl::Doc& idoc)
return true;
}
string make_abstract(Rcl::Doc& doc, Rcl::Query& query, bool asSnippets)
{
string abstract;
if (asSnippets) {
std::vector<Rcl::Snippet> snippets;
std::ostringstream str;
if (query.makeDocAbstract(doc, snippets, -1, -1, true)) {
for (const auto snippet : snippets) {
str << snippet.page << " : " << snippet.snippet << endl;
}
}
abstract = str.str();
} else {
query.makeDocAbstract(doc, abstract);
abstract += "\n";
}
return abstract;
}
void output_fields(vector<string> fields, Rcl::Doc& doc,
Rcl::Query& query, Rcl::Db&, bool printnames)
Rcl::Query& query, Rcl::Db&, bool printnames, bool asSnippets)
{
if (fields.empty()) {
map<string,string>::const_iterator it;
@ -70,9 +90,7 @@ void output_fields(vector<string> fields, Rcl::Doc& doc,
it != fields.end(); it++) {
string out;
if (!it->compare("abstract")) {
string abstract;
query.makeDocAbstract(doc, abstract);
base64_encode(abstract, out);
base64_encode(make_abstract(doc, query, asSnippets), out);
} else if (!it->compare("xdocid")) {
char cdocid[30];
sprintf(cdocid, "%lu", (unsigned long)doc.xdocid);
@ -94,45 +112,46 @@ void output_fields(vector<string> fields, Rcl::Doc& doc,
static char *thisprog;
static char usage [] =
" -P: Show the date span for all the documents present in the index.\n"
" [-o|-a|-f] [-q] <query string>\n"
" Runs a recoll query and displays result lines. \n"
" Default: will interpret the argument(s) as a xesam query string.\n"
" Query elements: \n"
" * Implicit AND, exclusion, field spec: t1 -t2 title:t3\n"
" * OR has priority: t1 OR t2 t3 OR t4 means (t1 OR t2) AND (t3 OR t4)\n"
" * Phrase: \"t1 t2\" (needs additional quoting on cmd line)\n"
" -o Emulate the GUI simple search in ANY TERM mode.\n"
" -a Emulate the GUI simple search in ALL TERMS mode.\n"
" -f Emulate the GUI simple search in filename mode.\n"
" -q is just ignored (compatibility with the recoll GUI command line).\n"
"Common options:\n"
" -c <configdir> : specify config directory, overriding $RECOLL_CONFDIR.\n"
" -C : collapse duplicates\n"
" -d also dump file contents.\n"
" -n [first-]<cnt> define the result slice. The default value for [first]\n"
" is 0. Without the option, the default max count is 2000.\n"
" Use n=0 for no limit.\n"
" -b : basic. Just output urls, no mime types or titles.\n"
" -Q : no result lines, just the processed query and result count.\n"
" -m : dump the whole document meta[] array for each result.\n"
" -A : output the document abstracts.\n"
" -S fld : sort by field <fld>.\n"
" -D : sort descending.\n"
" -s stemlang : set stemming language to use (must exist in index...).\n"
" Use -s \"\" to turn off stem expansion.\n"
" -T <synonyms file>: use the parameter (Thesaurus) for word expansion.\n"
" -i <dbdir> : additional index, several can be given.\n"
" -e use url encoding (%xx) for urls.\n"
" -E use exact result count instead of lower bound estimate.\n"
" -F <field name list> : output exactly these fields for each result.\n"
" The field values are encoded in base64, output in one line and \n"
" separated by one space character. This is the recommended format \n"
" for use by other programs. Use a normal query with option -m to \n"
" see the field names. Use -F '' to output all fields, but you probably\n"
" also want option -N in this case.\n"
" -N : with -F, print the (plain text) field names before the field values.\n"
;
" -P: Show the date span for all the documents present in the index.\n"
" [-o|-a|-f] [-q] <query string>\n"
" Runs a recoll query and displays result lines. \n"
" Default: will interpret the argument(s) as a xesam query string.\n"
" Query elements: \n"
" * Implicit AND, exclusion, field spec: t1 -t2 title:t3\n"
" * OR has priority: t1 OR t2 t3 OR t4 means (t1 OR t2) AND (t3 OR t4)\n"
" * Phrase: \"t1 t2\" (needs additional quoting on cmd line)\n"
" -o Emulate the GUI simple search in ANY TERM mode.\n"
" -a Emulate the GUI simple search in ALL TERMS mode.\n"
" -f Emulate the GUI simple search in filename mode.\n"
" -q is just ignored (compatibility with the recoll GUI command line).\n"
"Common options:\n"
" -c <configdir> : specify config directory, overriding $RECOLL_CONFDIR.\n"
" -C : collapse duplicates\n"
" -d also dump file contents.\n"
" -n [first-]<cnt> define the result slice. The default value for [first]\n"
" is 0. Without the option, the default max count is 2000.\n"
" Use n=0 for no limit.\n"
" -b : basic. Just output urls, no mime types or titles.\n"
" -Q : no result lines, just the processed query and result count.\n"
" -m : dump the whole document meta[] array for each result.\n"
" -A : output the document abstracts.\n"
" -p : show snippets, with page numbers instead of abstract.\n"
" -S fld : sort by field <fld>.\n"
" -D : sort descending.\n"
" -s stemlang : set stemming language to use (must exist in index...).\n"
" Use -s \"\" to turn off stem expansion.\n"
" -T <synonyms file>: use the parameter (Thesaurus) for word expansion.\n"
" -i <dbdir> : additional index, several can be given.\n"
" -e use url encoding (%xx) for urls.\n"
" -E use exact result count instead of lower bound estimate.\n"
" -F <field name list> : output exactly these fields for each result.\n"
" The field values are encoded in base64, output in one line and \n"
" separated by one space character. This is the recommended format \n"
" for use by other programs. Use a normal query with option -m to \n"
" see the field names. Use -F '' to output all fields, but you probably\n"
" also want option -N in this case.\n"
" -N : with -F, print the (plain text) field names before the field values.\n"
;
static void
Usage(void)
@ -167,18 +186,19 @@ static int op_flags;
#define OPT_n 0x4000
// GUI: -o same
#define OPT_o 0x8000
#define OPT_P 0x10000
#define OPT_Q 0x20000
#define OPT_p 0x10000
#define OPT_P 0x20000
#define OPT_Q 0x40000
// GUI: -q same
#define OPT_q 0x40000
#define OPT_S 0x80000
#define OPT_s 0x100000
#define OPT_T 0x200000
#define OPT_q 0x80000
#define OPT_S 0x100000
#define OPT_s 0x2000000
#define OPT_T 0x4000000
// GUI: -t use command line, us: ignored
#define OPT_t 0x400000
#define OPT_t 0x800000
// GUI uses -v : show version. Us: usage
// GUI uses -w : open minimized
#define OPT_E 0x800000
#define OPT_E 0x1000000
int recollq(RclConfig **cfp, int argc, char **argv)
{
@ -245,6 +265,7 @@ int recollq(RclConfig **cfp, int argc, char **argv)
argc--; goto b1;
case 'o': op_flags |= OPT_o; break;
case 'P': op_flags |= OPT_P; break;
case 'p': op_flags |= OPT_p; break;
case 'q': op_flags |= OPT_q; break;
case 'Q': op_flags |= OPT_Q; break;
case 'S': op_flags |= OPT_S; if (argc < 2) Usage();
@ -402,7 +423,8 @@ endopts:
break;
if (op_flags & OPT_F) {
output_fields(fields, doc, query, rcldb, op_flags & OPT_N);
output_fields(fields, doc, query, rcldb,
op_flags & OPT_N, op_flags & OPT_p);
continue;
}
@ -435,11 +457,13 @@ endopts:
}
}
if (op_flags & OPT_A) {
string abstract;
if (query.makeDocAbstract(doc, abstract)) {
cout << "ABSTRACT" << endl;
cout << abstract << endl;
cout << "/ABSTRACT" << endl;
bool asSnippets = (op_flags & OPT_p) != 0;
string abstract = make_abstract(doc, query, asSnippets);
string marker = asSnippets ? "SNIPPETS" : "ABSTRACT";
if (!abstract.empty()) {
cout << marker << endl;
cout << abstract;
cout << string("/") + marker << endl;
}
}
}
@ -450,4 +474,3 @@ endopts:
return 0;
}

View File

@ -40,11 +40,8 @@ enum abstract_result {
class Snippet {
public:
Snippet(int page, const std::string& snip)
: page(page), snippet(snip)
{
}
Snippet& setTerm(const std::string& trm)
{
: page(page), snippet(snip) {}
Snippet& setTerm(const std::string& trm) {
term = trm;
return *this;
}
@ -65,6 +62,9 @@ public:
Query(Db *db);
~Query();
Query(const Query &) = delete;
Query& operator=(const Query &) = delete;
/** Get explanation about last error */
std::string getReason() const {
return m_reason;
@ -114,7 +114,7 @@ public:
bool makeDocAbstract(const Doc &doc, std::vector<std::string>& abstract);
// Returned as a vector of pair<page,snippet> page is 0 if unknown
int makeDocAbstract(const Doc &doc, std::vector<Snippet>& abst,
int maxoccs= -1, int ctxwords = -1, bool sortbypage=false);
int maxoccs= -1, int ctxwords= -1,bool sortbypage=false);
/** Retrieve page number for first match for "significant" query term
* @param term returns the chosen term */
int getFirstMatchPage(const Doc &doc, std::string& term);
@ -146,10 +146,6 @@ private:
int m_resCnt;
std::shared_ptr<SearchData> m_sd;
int m_snipMaxPosWalk;
/* Copyconst and assignement private and forbidden */
Query(const Query &) {}
Query & operator=(const Query &) {return *this;};
};
#ifndef NO_NAMESPACES