Created
March 8, 2011 22:45
-
-
Save ncode/861275 to your computer and use it in GitHub Desktop.
teste do elastic search
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
from pyes import * | |
conn = ES('127.0.0.1:9500') | |
try: | |
conn.delete_index('accept-index') | |
conn.delete_index('reject-index') | |
except: | |
pass | |
conn.create_index('accept-index') | |
conn.create_index('reject-index') | |
accept_mapping = { u'subject': {'boost': 1.0, 'index': 'analyzed', 'store': 'yes', 'type': u'string', 'term_vector' : 'with_positions_offsets'}, | |
u'from': {'boost': 1.0, 'index': 'analyzed', 'store': 'yes', 'type': u'string', 'term_vector' : 'with_positions_offsets'}, | |
u'to': {'boost': 1.0, 'index': 'analyzed', 'store': 'yes', 'type': u'string', 'term_vector' : 'with_positions_offsets'}, | |
u'logfile': {'boost': 1.0, 'index': 'not_analyzed', 'store': 'yes', 'type': u'string'}, | |
u'msgid': {'boost': 1.0, 'index': 'not_analyzed', 'store':'yes', 'type': u'string'}} | |
reject_mapping = { u'address': {'boost': 1.0, 'index': 'analyzed', 'store': 'yes', 'type': u'string', 'term_vector' : 'with_positions_offsets'}, | |
u'reason': {'boost': 1.0, 'index': 'analyzed', 'store': 'yes', 'type': u'string', 'term_vector' : 'with_positions_offsets'}, | |
u'logfile': {'boost': 1.0, 'index': 'analyzed', 'store': 'yes', 'type': u'string'}} | |
conn.put_mapping("accept", {'properties':accept_mapping}, ["accept-index"]) | |
conn.put_mapping("reject", {'properties':reject_mapping}, ["reject-index"]) | |
conn.index({"from":"[email protected]", "to":"[email protected]", "subject":"teste", "msgid":"QASWDF1234DF78TD", "logfile":"/var/log/mail.log.2"}, "accept-index", "accept") | |
conn.index({"address":"127.0.0.1", "reason":"Relay access denied", "logfile":"/var/log/mail.log.1"}, "reject-index", "reject") | |
conn.refresh(["accept-index"]) | |
conn.refresh(["reject-index"]) | |
q = TermQuery("from", "[email protected]") | |
result = conn.search(query = q) | |
for hit in result['hits']: | |
print hit, result['hits'][hit] | |
q = TermQuery("address", "127.0.0.1") | |
result = conn.search(query = q) | |
for hit in result['hits']: | |
print hit, result['hits'][hit] | |
q = TermQuery("reason", "denied") | |
result = conn.search(query = q) | |
for hit in result['hits']: | |
print hit, result['hits'][hit] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment