Last active
February 27, 2021 19:57
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import requests | |
url_base = 'http://localhost:9200' | |
# https://dzone.com/articles/23-useful-elasticsearch-example-queries | |
fill_data = True | |
# fill_data = False | |
def get(path): | |
print(f'GET {path}') | |
r = requests.get(f'{url_base}{path}') | |
assert r.ok | |
return r.text | |
def delete(path, do_asert=True): | |
print(f'DELETE {path}') | |
r = requests.delete(f'{url_base}{path}') | |
if do_asert: | |
assert r.ok | |
return r.text | |
def put(path, data): | |
print(f'PUT {path}') | |
r = requests.put(f'{url_base}{path}', data=data, headers={'content-type': 'application/json'}) | |
print(r.text) | |
assert r.ok | |
return r.text | |
def post(path, data): | |
print(f'POST {path}') | |
r = requests.post(f'{url_base}{path}', data=data, headers={'content-type': 'application/json'}) | |
print(r.text) | |
assert r.ok | |
return r.text | |
if fill_data: | |
delete('/named_index', do_asert=False) | |
put('/named_index', ''' | |
{ | |
"settings": { "number_of_shards": 1 } | |
} | |
''') | |
post('/named_index/named/1', '''{"code": "green1","names": [{"name": "Green One", "name_norm": "greenone"}, {"name": "Cyan One", "name_norm": "cyanone"}]}''') | |
post('/named_index/named/2', '''{"code": "red1","names": [{"name": "Red One", "name_norm": "redone"}, {"name": "Purple One", "name_norm": "purpleone"}]}''') | |
time.sleep(1.0) | |
# post('/named_index/named/_search?pretty=true', ''' | |
# { | |
# "query": { | |
# "bool": { | |
# "must": { | |
# "bool" : { | |
# "should": [ | |
# { "match": { "names.name_norm": "purpleone" }} | |
# ] | |
# } | |
# } | |
# } | |
# } | |
# } | |
# ''') | |
post('/named_index/named/_search?pretty=true&explain=true', ''' | |
{ | |
"query": { | |
"function_score": { | |
"query": { | |
"regexp": { | |
"names.name_norm": {"value": "purpleone"} | |
} | |
}, | |
"script_score": { | |
"script": { | |
"source": "100.0" | |
} | |
} | |
} | |
} | |
} | |
''') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment