Created
August 25, 2018 12:53
-
-
Save alexgarel/92440caec4232c5ad51c569c2d0f465b to your computer and use it in GitHub Desktop.
Test of ipv6 in ES6 QueryString
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
# in the shell | |
# running ES6:: | |
# | |
# $ docker pull docker.elastic.co/elasticsearch/elasticsearch:6.4.0 | |
# $ docker run -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:6.4.0 | |
# in a new widow, install elasticsearch_dsl in a new virtualenv:: | |
# | |
# $ cd /tmp/ | |
# $ python3 -m venv tmp | |
# $ . tmp/bin/activate | |
# $ pip install elasticsearch_dsl | |
# $ pip install ipython | |
# $ ipython | |
# in ipython console | |
from elasticsearch_dsl import connections | |
connections.create_connection(hosts=['localhost'], timeout=20) | |
# we create an index with objects having an ip field | |
from elasticsearch_dsl import Document, Ip, Keyword | |
class Machine(Document): | |
name = Keyword() | |
address = Ip() | |
class Index: | |
name = 'test_machines' | |
Machine.init() | |
# add two objects | |
Machine(name="four", address="128.0.0.1").save() | |
Machine(name="six", address="1::1").save() | |
from elasticsearch_dsl import Search | |
from elasticsearch_dsl import query as q | |
# verify search works | |
list(Search(index='test_machines').execute()) | |
# [<Hit(test_machines/doc/eSPqcGUB7q8HKg2ix8EQ): {'name': 'four', 'address': '128.0.0.1'}>, | |
# <Hit(test_machines/doc/eiPrcGUB7q8HKg2iA8Ff): {'name': 'six', 'address': '1::1'}>] | |
list(Search(index='test_machines').filter(q.QueryString(query="name:four")).execute()) | |
# [<Hit(test_machines/doc/eSPqcGUB7q8HKg2ix8EQ): {'name': 'four', 'address': '128.0.0.1'}>] | |
# ipv4 is ok without quoting because characters are compliant with Lucene expressions | |
list(Search(index='test_machines').filter(q.QueryString(query='address:128.0.0.1')).execute | |
# [<Hit(test_machines/doc/eSPqcGUB7q8HKg2ix8EQ): {'name': 'four', 'address': '128.0.0.1'}>] | |
# ipv6 is not ok without quoting because characters are compliant with Lucene expressions | |
try: | |
list(Search(index='test_machines').filter(q.QueryString(query='address:1::1')).execute()) | |
except Exception as e: | |
print(e) | |
# GET http://localhost:9200/test_machines/_search [status:400 request:0.011s] | |
# RequestError(400, 'search_phase_execution_exception', 'Failed to parse query [address:1::1]') | |
# you can either quote ipv6 | |
list(Search(index='test_machines').filter(q.QueryString(query='address:"1::1"')).execute()) | |
# [<Hit(test_machines/doc/eiPrcGUB7q8HKg2iA8Ff): {'name': 'six', 'address': '1::1'}>] | |
# or (the hard way), escape the columns in ip address | |
list(Search(index='test_machines').filter(q.QueryString(query=r'address:1\:\:1')).execute()) | |
# [<Hit(test_machines/doc/eiPrcGUB7q8HKg2iA8Ff): {'name': 'six', 'address': '1::1'}>] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment