Arguments: <keyword1> [<keyword2> [<keyword3> [...]]]

    List the documents containing the keywords.

    Syntax is the same than with the search field in Paperwork-gui.
    Search "" (empty string) to get all the documents.

    Example: 'label:contrat AND paperwork'

    Possible JSON replies:
        --
        {
            "status": "error", "exception": "yyy",
            "reason": "xxxx", "args": "(xxxx, )"
        }
        --
        {
            "status": "ok",
            "results" [
                {"docid": "xxxx", "nb_pages": 22, "labels": ["xxx", "yyy"]}
                {"docid": "yyyy", "nb_pages": 22, "labels": ["xxx", "yyy"]}
                {"docid": "zzzz", "nb_pages": 22, "labels": ["xxx", "yyy"]}
            ],
        }

def cmd_search(*args):
    """
    Arguments: <keyword1> [<keyword2> [<keyword3> [...]]]

    List the documents containing the keywords.

    Syntax is the same than with the search field in Paperwork-gui.
    Search "" (empty string) to get all the documents.

    Example: 'label:contrat AND paperwork'

    Possible JSON replies:
        --
        {
            "status": "error", "exception": "yyy",
            "reason": "xxxx", "args": "(xxxx, )"
        }
        --
        {
            "status": "ok",
            "results" [
                {"docid": "xxxx", "nb_pages": 22, "labels": ["xxx", "yyy"]}
                {"docid": "yyyy", "nb_pages": 22, "labels": ["xxx", "yyy"]}
                {"docid": "zzzz", "nb_pages": 22, "labels": ["xxx", "yyy"]}
            ],
        }
    """
    dsearch = get_docsearch()

    verbose("Search: {}".format(" ".join(args)))

    r = {'results': []}

    docs = dsearch.find_documents(" ".join(args))
    docs.sort(key=lambda doc: doc.docid)
    for doc in docs:
        r['results'].append({
            'docid': doc.docid,
            'nb_pages': doc.nb_pages,
            'labels': [l.name for l in doc.labels],
        })
    reply(r)