Skip to content

Instantly share code, notes, and snippets.

@clintongormley
Created February 25, 2014 20:37
Show Gist options
  • Select an option

  • Save clintongormley/9217164 to your computer and use it in GitHub Desktop.

Select an option

Save clintongormley/9217164 to your computer and use it in GitHub Desktop.
Multi-match
DELETE /_all
PUT /myindex
{
"settings": {
"number_of_shards": 1
}
}
POST /myindex/mytype/1
{
"title": "Quick brown rabbits",
"content": "Brown rabbits are commonly seen"
}
POST /myindex/mytype/2
{
"title": "Keeping pets healthy",
"content": "My quick brown fox eats rabbits on a regular basis"
}
# term filter - looks up term in inverted index
GET /_search
{
"query": {
"filtered": {
"filter": {
"term": {
"title": "brown"
}
}
}
}
}
GET /_validate/query?explain
{
"query": {
"filtered": {
"filter": {
"term": {
"title": "brown"
}
}
}
}
}
# term query - same as filter plus scoring
GET /_search
{
"query": {
"term": {
"title": "brown"
}
}
}
GET /_validate/query?explain
{
"query": {
"term": {
"title": "brown"
}
}
}
GET /_search
{
"explain": true,
"query": {
"term": {
"title": "brown"
}
}
}
# one word match query - analysis plus term query
GET /_search
{
"query": {
"match": {
"title": "QUICK!"
}
}
}
GET /_validate/query?explain
{
"query": {
"match": {
"title": "QUICK!"
}
}
}
# multi-word match query
GET /_search
{
"query": {
"match": {
"content": "quick brown fox"
}
}
}
GET /_validate/query?explain
{
"query": {
"match": {
"content": "quick brown fox"
}
}
}
GET /_search
{
"query": {
"bool": {
"should": [
{"term": { "content": "quick" }},
{"term": { "content": "brown" }},
{"term": { "content": "fox" }}
]
}
}
}
# operator: and
GET /_search
{
"query": {
"match": {
"content": {
"query": "quick brown fox",
"operator": "and"
}
}
}
}
GET /_validate/query?explain
{
"query": {
"match": {
"content": {
"query": "quick brown fox",
"operator": "and"
}
}
}
}
GET /_search
{
"query": {
"bool": {
"must": [
{"term": { "content": "quick" }},
{"term": { "content": "brown" }},
{"term": { "content": "fox" }}
]
}
}
}
# minimum_should_match
GET /_search
{
"query": {
"match": {
"content": {
"query": "quick brown fox",
"minimum_should_match": "70%"
}
}
}
}
GET /_validate/query?explain
{
"query": {
"match": {
"content": {
"query": "quick brown fox",
"minimum_should_match": "70%"
}
}
}
}
GET /_search
{
"query": {
"bool": {
"should": [
{"term": { "content": "quick" }},
{"term": { "content": "brown" }},
{"term": { "content": "fox" }}
],
"minimum_should_match": "70%"
}
}
}
# multi-field query
GET /_search
{
"query": {
"bool": {
"should": [
{
"match": {
"title": "healthy pets"
}
},
{
"match": {
"content": "brown fox"
}
}
]
}
}
}
GET /_validate/query?explain
{
"query": {
"bool": {
"should": [
{
"match": {
"title": "healthy pets"
}
},
{
"match": {
"content": "brown fox"
}
}
]
}
}
}
GET /_search
# single query string
GET /_search
{
"query": {
"bool": {
"should": [
{
"match": {
"title": "quick brown fox"
}
},
{
"match": {
"content": "quick brown fox"
}
}
]
}
}
}
# dis_max query
GET /_search
{
"query": {
"dis_max": {
"queries": [
{
"match": {
"title": "quick brown fox"
}
},
{
"match": {
"content": "quick brown fox"
}
}
]
}
}
}
GET /_validate/query?explain
{
"query": {
"dis_max": {
"queries": [
{
"match": {
"title": "quick brown fox"
}
},
{
"match": {
"content": "quick brown fox"
}
}
]
}
}
}
# tie_breaker
GET /_search
{
"query": {
"dis_max": {
"queries": [
{
"match": {
"title": "quick pets"
}
},
{
"match": {
"content": "quick pets"
}
}
],
"tie_breaker": 0.3
}
}
}
# multi_match
GET /_search
{
"query": {
"multi_match": {
"query": "quick pets",
"fields": [
"title",
"content"
],
"tie_breaker": 0.3
}
}
}
DELETE /_all
PUT /myindex
{
"settings": {
"number_of_shards": 1,
"analysis": {
"filter": {
"autocomplete": {
"type": "edge_ngram",
"min_gram": 1,
"max_gram": 20
},
"shingles": {
"type": "shingle",
"min_shingle_size": 2,
"max_shingle_size": 2,
"output_unigrams": false
}
},
"analyzer": {
"autocomplete": {
"type": "custom",
"tokenizer": "standard",
"filter": [
"lowercase",
"autocomplete"
]
},
"shingles": {
"type": "custom",
"tokenizer": "standard",
"filter": [
"lowercase",
"shingles"
]
}
}
}
},
"mappings": {
"mytype": {
"properties": {
"content": {
"type": "string",
"analyzer": "standard",
"fields": {
"stemmed": {
"type": "string",
"analyzer": "english"
},
"autocomplete": {
"type": "string",
"analyzer": "autocomplete"
},
"shingles": {
"type": "string",
"analyzer": "shingles"
}
}
}
}
}
}
}
POST /myindex/_analyze?field=content.autocomplete
{"":"The Quick Brown Foxes"}
POST /myindex/mytype/1
{
"title": "Quick brown rabbits",
"content": "Brown rabbits are commonly seen"
}
POST /myindex/mytype/2
{
"title": "Keeping pets healthy",
"content": "My quick brown fox eats rabbits on a regular basis"
}
# use_dis_max
GET /_search
{
"query": {
"multi_match": {
"query": "brown foxes",
"fields": [
"content"
],
"use_dis_max": false
}
}
}
GET /_validate/query?explain
{
"query": {
"multi_match": {
"query": "quick brown foxes",
"fields": [
"content.*"
],
"use_dis_max": false
}
}
}
DELETE /_all
PUT /myindex
{
"settings": {
"number_of_shards": 1
}
}
POST /myindex/mytype/_bulk
{"index":{"_id":1}}
{"firstname":"John","lastname":"Smith"}
{"index":{"_id":2}}
{"firstname":"Alice","lastname":"Smith"}
{"index":{"_id":3}}
{"firstname":"Alice","lastname":"Jones"}
{"index":{"_id":4}}
{"firstname":"Mary","lastname":"Smith"}
{"index":{"_id":5}}
{"firstname":"Smith","lastname":"Johns"}
# multi_match and operator/min_should_match
GET /_search
{
"query": {
"multi_match": {
"query": "alice smith",
"fields": [
"firstname",
"lastname"
],
"operator": "and"
}
}
}
GET /_validate/query?explain
{
"query": {
"multi_match": {
"query": "alice smith",
"fields": [
"firstname",
"lastname"
],
"operator": "and"
}
}
}
# bad frequencies
GET /_search
{
"query": {
"multi_match": {
"query": "Alice Smith",
"fields": [
"firstname",
"lastname"
]
}
}
}
DELETE /_all
PUT /myindex
{
"settings": {
"number_of_shards": 1
},
"mappings": {
"mytype": {
"properties": {
"firstname": {
"type": "string",
"copy_to": "fullname"
},
"lastname": {
"type": "string",
"copy_to": "fullname"
},
"fullname": {
"type": "string"
}
}
}
}
}
POST /myindex/mytype/_bulk
{"index":{"_id":1}}
{"firstname":"John","lastname":"Smith"}
{"index":{"_id":2}}
{"firstname":"Alice","lastname":"Smith"}
{"index":{"_id":3}}
{"firstname":"Alice","lastname":"Jones"}
{"index":{"_id":4}}
{"firstname":"Mary","lastname":"Smith"}
{"index":{"_id":5}}
{"firstname":"Smith","lastname":"Johns"}
# Search single field
GET /_search
{
"query": {
"multi_match": {
"query": "alice smith",
"fields": [
"fullname"
]
}
}
}
GET /_search
{
"query": {
"multi_match": {
"query": "alice smith",
"fields": [
"fullname"
],
"operator": "and"
}
}
}
# Cross fields
GET /_search
{
"query": {
"multi_match": {
"query": "alice smith",
"fields": [
"firstname",
"lastname"
],
"operator": "and",
"type": "cross_fields"
}
}
}
GET /_validate/query?explain
{
"query": {
"multi_match": {
"query": "alice smith",
"fields": [
"firstname",
"lastname"
],
"type": "cross_fields",
"operator": "and"
}
}
}
# best fields
GET /_search
{
"query": {
"multi_match": {
"query": "quick pets",
"type": "best_fields",
"fields": [
"title",
"content"
],
"tie_breaker": 0.3
}
}
}
# most fields
GET /_search
{
"query": {
"multi_match": {
"query": "brown foxes",
"type": "most_fields",
"fields": [
"content.*"
]
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment