Skip to content

Instantly share code, notes, and snippets.

@zencd
Last active February 27, 2021 19:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zencd/f17943c2af89b9b6de024a0e30b22c37 to your computer and use it in GitHub Desktop.
Save zencd/f17943c2af89b9b6de024a0e30b22c37 to your computer and use it in GitHub Desktop.
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