Created
June 29, 2016 15:50
-
-
Save brupm/ad43e871478c10a845478fb20da4c735 to your computer and use it in GitHub Desktop.
Elasticsearch Sample Mappings, Indexing, Searching - Useful for debugging & troubleshooting
This file contains hidden or 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
### Create the Index | |
curl -XDELETE http://localhost:9200/users | |
curl -XPOST localhost:9200/users -d '{ | |
"mappings" : { | |
"user" : { | |
"properties" : { | |
"name" : { | |
"type" : "string", | |
"index" : "not_analyzed" | |
} | |
} | |
} | |
} | |
}' | |
### If you need to create a custom analyzer | |
curl -XPOST 'localhost:9200/users/_close' | |
curl -XPUT 'localhost:9200/users/_settings' -d '{ | |
"analysis" : { | |
"analyzer":{ | |
"for_emails":{ | |
"type":"custom", | |
"tokenizer":"uax_url_email" | |
} | |
} | |
} | |
}' | |
curl -XPOST 'localhost:9200/users/_open' | |
### Update the mappings to use the custom analyzer | |
curl -XPUT localhost:9200/users/_mapping/user -d '{ | |
"user": { | |
"properties": { | |
"emails": { | |
"type": "string", | |
"analyzer": "for_emails" | |
} | |
} | |
} | |
}' | |
### Adding documents to the index | |
curl -XPUT localhost:9200/users/user/1 -d '{ | |
"name": "bruno", | |
"emails" : ["bmiranda@doximity", "[email protected]"] | |
}' | |
curl -XPUT localhost:9200/users/user/2 -d '{ | |
"name": "dan", | |
"emails" : ["dnill@doximity", "[email protected]"] | |
}' | |
### Searching against the index | |
curl -XPOST 'localhost:9200/users/_search' -d '{ | |
"query": { | |
"bool": { | |
"must": [ | |
{ | |
"terms": { | |
"emails": [ | |
"[email protected]" | |
] | |
} | |
} | |
] | |
} | |
} | |
}' | |
curl -XPOST 'localhost:9200/users/_search' -d '{ | |
"query": { | |
"bool": { | |
"must": [ | |
{ | |
"multi_match": { | |
"query": "dan", | |
"fields": ["name"] | |
} | |
}, | |
{ | |
"terms": { | |
"emails": [ | |
"[email protected]" | |
] | |
} | |
} | |
] | |
} | |
} | |
}' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment