2018-05-04 13:53:29 +02:00
|
|
|
#!/usr/bin/env python3
|
2014-08-11 12:03:27 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# This file is part of AIL framework - Analysis Information Leak framework
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Copyright (c) 2014 Alexandre Dulaunoy - a@foo.be
|
|
|
|
|
2018-04-16 14:50:04 +02:00
|
|
|
import configparser
|
2014-08-11 12:03:27 +02:00
|
|
|
import argparse
|
2014-08-12 13:26:56 +02:00
|
|
|
import gzip
|
2014-08-20 15:14:57 +02:00
|
|
|
import os
|
2014-08-12 13:26:56 +02:00
|
|
|
|
2014-08-14 17:55:18 +02:00
|
|
|
|
2014-08-12 13:26:56 +02:00
|
|
|
def readdoc(path=None):
|
|
|
|
if path is None:
|
|
|
|
return False
|
2014-08-14 17:55:18 +02:00
|
|
|
f = gzip.open(path, 'r')
|
2014-08-12 13:26:56 +02:00
|
|
|
return f.read()
|
2014-08-11 12:03:27 +02:00
|
|
|
|
2014-08-22 17:35:40 +02:00
|
|
|
configfile = os.path.join(os.environ['AIL_BIN'], 'packages/config.cfg')
|
2018-04-16 14:50:04 +02:00
|
|
|
cfg = configparser.ConfigParser()
|
2014-08-11 12:03:27 +02:00
|
|
|
cfg.read(configfile)
|
|
|
|
|
|
|
|
# Indexer configuration - index dir and schema setup
|
2014-08-22 17:35:40 +02:00
|
|
|
indexpath = os.path.join(os.environ['AIL_HOME'], cfg.get("Indexer", "path"))
|
2014-08-11 12:03:27 +02:00
|
|
|
indexertype = cfg.get("Indexer", "type")
|
|
|
|
|
|
|
|
argParser = argparse.ArgumentParser(description='Fulltext search for AIL')
|
|
|
|
argParser.add_argument('-q', action='append', help='query to lookup (one or more)')
|
2014-08-11 14:56:15 +02:00
|
|
|
argParser.add_argument('-n', action='store_true', default=False, help='return numbers of indexed documents')
|
2014-08-11 15:07:12 +02:00
|
|
|
argParser.add_argument('-t', action='store_true', default=False, help='dump top 500 terms')
|
2014-08-11 14:56:15 +02:00
|
|
|
argParser.add_argument('-l', action='store_true', default=False, help='dump all terms encountered in indexed documents')
|
2014-08-12 13:26:56 +02:00
|
|
|
argParser.add_argument('-f', action='store_true', default=False, help='dump each matching document')
|
2015-12-22 22:37:05 +01:00
|
|
|
argParser.add_argument('-v', action='store_true', default=False, help='Include filepath')
|
2014-08-12 13:42:26 +02:00
|
|
|
argParser.add_argument('-s', action='append', help='search similar documents')
|
2014-08-12 13:26:56 +02:00
|
|
|
|
2014-08-11 12:03:27 +02:00
|
|
|
args = argParser.parse_args()
|
|
|
|
|
|
|
|
from whoosh import index
|
2014-08-14 17:55:18 +02:00
|
|
|
from whoosh.fields import Schema, TEXT, ID
|
|
|
|
|
2014-08-11 12:03:27 +02:00
|
|
|
schema = Schema(title=TEXT(stored=True), path=ID(stored=True), content=TEXT)
|
|
|
|
|
|
|
|
ix = index.open_dir(indexpath)
|
|
|
|
|
|
|
|
from whoosh.qparser import QueryParser
|
2014-08-11 14:50:35 +02:00
|
|
|
|
|
|
|
if args.n:
|
2018-04-16 14:50:04 +02:00
|
|
|
print(ix.doc_count_all())
|
2014-08-11 14:50:35 +02:00
|
|
|
exit(0)
|
|
|
|
|
2014-08-11 14:56:15 +02:00
|
|
|
if args.l:
|
|
|
|
xr = ix.searcher().reader()
|
|
|
|
for x in xr.lexicon("content"):
|
|
|
|
print (x)
|
|
|
|
exit(0)
|
|
|
|
|
2014-08-11 15:07:12 +02:00
|
|
|
if args.t:
|
|
|
|
xr = ix.searcher().reader()
|
|
|
|
for x in xr.most_frequent_terms("content", number=500, prefix=''):
|
|
|
|
print (x)
|
|
|
|
exit(0)
|
|
|
|
|
2014-08-12 13:42:26 +02:00
|
|
|
if args.s:
|
|
|
|
# By default, the index is not storing the vector of the document (Whoosh
|
|
|
|
# document schema). It won't work if you don't change the schema of the
|
|
|
|
# index for the content. It depends of your storage strategy.
|
|
|
|
docnum = ix.searcher().document_number(path=args.s)
|
|
|
|
r = ix.searcher().more_like(docnum, "content")
|
|
|
|
for hit in r:
|
|
|
|
print(hit["path"])
|
|
|
|
exit(0)
|
|
|
|
|
2014-08-11 12:03:27 +02:00
|
|
|
if args.q is None:
|
|
|
|
argParser.print_help()
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
with ix.searcher() as searcher:
|
|
|
|
query = QueryParser("content", ix.schema).parse(" ".join(args.q))
|
|
|
|
results = searcher.search(query, limit=None)
|
|
|
|
for x in results:
|
2014-08-12 13:26:56 +02:00
|
|
|
if args.f:
|
2015-12-22 22:37:05 +01:00
|
|
|
if args.v:
|
|
|
|
print (x.items()[0][1])
|
2014-08-12 13:26:56 +02:00
|
|
|
print (readdoc(path=x.items()[0][1]))
|
|
|
|
else:
|
|
|
|
print (x.items()[0][1])
|
|
|
|
print
|