Skip to content

Instantly share code, notes, and snippets.

@brupm
Created June 29, 2016 15:50
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 brupm/ad43e871478c10a845478fb20da4c735 to your computer and use it in GitHub Desktop.
Save brupm/ad43e871478c10a845478fb20da4c735 to your computer and use it in GitHub Desktop.
Elasticsearch Sample Mappings, Indexing, Searching - Useful for debugging & troubleshooting
### 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", "bruno@gmail.com"]
}'
curl -XPUT localhost:9200/users/user/2 -d '{
"name": "dan",
"emails" : ["dnill@doximity", "dan@gmail.com"]
}'
### Searching against the index
curl -XPOST 'localhost:9200/users/_search' -d '{
"query": {
"bool": {
"must": [
{
"terms": {
"emails": [
"bruno@gmail.com"
]
}
}
]
}
}
}'
curl -XPOST 'localhost:9200/users/_search' -d '{
"query": {
"bool": {
"must": [
{
"multi_match": {
"query": "dan",
"fields": ["name"]
}
},
{
"terms": {
"emails": [
"dan@gmail.com"
]
}
}
]
}
}
}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment