Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
|
opensearch [2025/03/25 01:02] |
opensearch [2025/03/27 11:49] (aktuell) |
||
|---|---|---|---|
| Zeile 1: | Zeile 1: | ||
| + | Siehe auch [[ElasticSearch]] (z.B Scripte, API, etc. sind identisch). [[Wazuh]] verwendet [[OpenSearch]]. | ||
| + | =====Installation===== | ||
| + | |||
| + | Run a local cluster | ||
| + | |||
| + | < | ||
| + | docker run --rm -p 9200:9200 -p 9600:9600 -e " | ||
| + | </ | ||
| + | |||
| + | Create a python script | ||
| + | |||
| + | <code python> | ||
| + | from opensearchpy import OpenSearch | ||
| + | |||
| + | client = OpenSearch( | ||
| + | hosts = [{" | ||
| + | http_auth = (" | ||
| + | use_ssl = True, | ||
| + | verify_certs = False, | ||
| + | ssl_assert_hostname = False, | ||
| + | ssl_show_warn = False, | ||
| + | ) | ||
| + | client.info() | ||
| + | </ | ||
| + | |||
| + | Get some random data for e.g [[https:// | ||
| + | |||
| + | <code python> | ||
| + | import pandas as pd | ||
| + | |||
| + | df = ( | ||
| + | pd.read_csv(" | ||
| + | .dropna() | ||
| + | .sample(5000, | ||
| + | .reset_index(drop=True) | ||
| + | ) | ||
| + | </ | ||
| + | |||
| + | Create an index | ||
| + | |||
| + | <code python> | ||
| + | body = { | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | response = client.indices.create(" | ||
| + | </ | ||
| + | |||
| + | Push the data into the index | ||
| + | |||
| + | <code python> | ||
| + | for i, row in df.iterrows(): | ||
| + | body = { | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | client.index(index=" | ||
| + | </ | ||
| + | |||
| + | More data in a bulk | ||
| + | |||
| + | <code python> | ||
| + | from opensearchpy.helpers import bulk | ||
| + | |||
| + | bulk_data = [] | ||
| + | for i,row in df.iterrows(): | ||
| + | bulk_data.append( | ||
| + | { | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | } | ||
| + | ) | ||
| + | bulk(client, | ||
| + | </ | ||
| + | |||
| + | Count the inserted data | ||
| + | |||
| + | <code python> | ||
| + | client.indices.refresh(index=" | ||
| + | client.cat.count(index=" | ||
| + | </ | ||
| + | |||
| + | Search the data | ||
| + | |||
| + | <code python> | ||
| + | resp = client.search( | ||
| + | index=" | ||
| + | body={ | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | }, | ||
| + | " | ||
| + | }, | ||
| + | }, | ||
| + | } | ||
| + | ) | ||
| + | resp | ||
| + | </ | ||
| + | |||
| + | Remove documents | ||
| + | <code python> | ||
| + | client.delete(index=" | ||
| + | </ | ||
| + | |||
| + | Delete the index | ||
| + | <code python> | ||
| + | client.indices.delete(index=' | ||
| + | </ | ||