Ejemplo n.º 1
0
def remove_from_index(dataobj_id):
    """Removes object of given id"""
    es = get_elastic_client()
    if not es:
        return
    es.delete(index=current_app.config["SEARCH_CONF"]["index_name"],
              id=dataobj_id)
Ejemplo n.º 2
0
def query_index(query):
    """Returns search results for your given query"""
    es = get_elastic_client()
    if not es:
        return []
    search = es.search(index=current_app.config["SEARCH_CONF"]["index_name"],
                       body={
                           "query": {
                               "multi_match": {
                                   "query": query,
                                   "fields": ["*"],
                                   "analyzer": "rebuilt_standard"
                               }
                           },
                           "highlight": {
                               "fragment_size": 0,
                               "fields": {
                                   "content": {
                                       "pre_tags": "==",
                                       "post_tags": "==",
                                   }
                               }
                           }
                       })

    hits = []
    for hit in search["hits"]["hits"]:
        formatted_hit = {"id": hit["_id"], "title": hit["_source"]["title"]}
        if "highlight" in hit:
            formatted_hit["highlight"] = hit["highlight"]["content"]
        hits.append(formatted_hit)

    return hits
Ejemplo n.º 3
0
def query_index(index, query):
    """Returns search results for your given query"""
    es = get_elastic_client()
    if not es:
        return []
    search = es.search(index=index,
                       body={
                           "query": {
                               "multi_match": {
                                   "query": query,
                                   "fields": ["*"],
                                   "analyzer": "rebuilt_standard"
                               }
                           },
                           "highlight": {
                               "fields": {
                                   "content": {
                                       "pre_tags": "<span class='matches'>",
                                       "post_tags": "</span>",
                                       "boundary_max_scan": 200,
                                       "fragment_size": 0
                                   }
                               }
                           }
                       })

    text = ""
    for hit in search["hits"]["hits"]:
        text += f"<li>[{hit['_source']['title']}](/dataobj/{hit['_id']})\n\n"
        if "highlight" in hit:
            for highlight in hit["highlight"]["content"]:
                text += f"{highlight}"
        text += "</li>"

    return convert_text(text, "html", format="md")
Ejemplo n.º 4
0
def add_to_index(index, model):
    """
    Adds dataobj to given index. If object of given id already exists, it will be updated.

    Params:

    - **index** - String of the ES Index. Archivy uses `dataobj` by default.
    - **model** - Instance of `archivy.models.Dataobj`, the object you want to index.
    """
    es = get_elastic_client()
    if not es:
        return
    payload = {}
    for field in model.__searchable__:
        payload[field] = getattr(model, field)
    es.index(index=index, id=model.id, body=payload)
Ejemplo n.º 5
0
def query_es_index(query, strict=False):
    """
    Returns search results for your given query

    Specify strict=True if you want only exact result (in case you're using ES.
    """
    es = get_elastic_client()
    if not es:
        return []
    search = es.search(
        index=current_app.config["SEARCH_CONF"]["index_name"],
        body={
            "query": {
                "multi_match": {
                    "query": query,
                    "fields": ["*"],
                    "analyzer": "rebuilt_standard",
                }
            },
            "highlight": {
                "fragment_size": 0,
                "fields": {
                    "content": {
                        "pre_tags": "==",
                        "post_tags": "==",
                    }
                },
            },
        },
    )

    hits = []
    for hit in search["hits"]["hits"]:
        formatted_hit = {"id": hit["_id"], "title": hit["_source"]["title"]}
        if "highlight" in hit:
            formatted_hit["highlight"] = hit["highlight"]["content"]
            reformatted_match = " ".join(formatted_hit["highlight"]).replace(
                "==", "")
            if strict and not (query in reformatted_match):
                continue
        hits.append(formatted_hit)
    return hits
Ejemplo n.º 6
0
app = Flask(__name__)
app.logger.setLevel(logging.INFO)
config = Config()
try:
    # if it exists, load user config
    config.override(load_config(config.INTERNAL_DIR))
except FileNotFoundError:
    pass

app.config.from_object(config)

(Path(app.config["USER_DIR"]) / "data").mkdir(parents=True, exist_ok=True)

if app.config["SEARCH_CONF"]["enabled"]:
    with app.app_context():
        es = helpers.get_elastic_client()
        try:
            es.indices.create(index=app.config["SEARCH_CONF"]["index_name"],
                              body=app.config["SEARCH_CONF"]["search_conf"])
        except elasticsearch.exceptions.RequestError:
            app.logger.info("Elasticsearch index already created")

# login routes / setup
login_manager = LoginManager()
login_manager.login_view = "login"
login_manager.init_app(app)
app.register_blueprint(api_bp, url_prefix='/api')

# compress files
Compress(app)
Ejemplo n.º 7
0
def remove_from_index(index, dataobj_id):
    """Removes object of given id"""
    es = get_elastic_client()
    if not es:
        return
    es.delete(index=index, id=dataobj_id)
Ejemplo n.º 8
0
            try:
                es.cluster.health()
                app.config["SEARCH_CONF"]["engine"] = "elasticsearch"
            except elasticsearch.exceptions.ConnectionError:
                if which("rg"):
                    app.config["SEARCH_CONF"]["engine"] = "ripgrep"
            engine = app.config["SEARCH_CONF"]["engine"]
            if engine == "none":
                app.logger.warning(
                    "No working search engine found. Disabling search.")
                app.config["SEARCH_CONF"]["enabled"] = 0
            else:
                app.logger.info(f"Running {engine} installation found.")

        if app.config["SEARCH_CONF"]["engine"] == "elasticsearch":
            es = es or get_elastic_client()
            try:
                es.indices.create(
                    index=app.config["SEARCH_CONF"]["index_name"],
                    body=app.config["SEARCH_CONF"]["search_conf"],
                )
            except elasticsearch.exceptions.RequestError:
                app.logger.info("Elasticsearch index already created")

# login routes / setup
login_manager = LoginManager()
login_manager.login_view = "login"
login_manager.init_app(app)
app.register_blueprint(api_bp, url_prefix="/api")

# compress files