Skip to content

Instantly share code, notes, and snippets.

@corny
Created July 9, 2012 19:28
Show Gist options
  • Save corny/3078369 to your computer and use it in GitHub Desktop.
Save corny/3078369 to your computer and use it in GitHub Desktop.
Tests for a elastic search plugin

Recipes (number) with ingredients (letter):

    1. A B
    1. B C D
    1. D

All must match:

  • Query for A matches 1
  • Query for B matches 1,2
  • Query for C matches 2
  • Query for D matches 3
  • Query for E matches none
  • Query for A,B matches 1
  • Query for B,C matches none
  • Query for C,D matches 3
  • Query for A,B,C matches 1
  • Query for A,C,D matches 3
  • Query for A,B,D matches 3
  • Query for B,C,D matches 2,3

Results with one missing allowed:

  • Query for A matches 1,3
  • Query for B matches 1,3
  • Query for C matches 3
  • Query for D matches 3
  • Query for E matches 3
  • Query for A,B matches 1,3
  • Query for B,C matches 1,2,3
  • Query for C,D matches 2,3
  • Query for A,B,C matches 1,2,3
  • Query for A,C,D matches 1,2,3
  • Query for A,B,D matches 1,2,3
  • Query for B,C,D matches 1,2,3

Results with two missing allowed:

  • Query for A matches 1,3
  • Query for B matches 1,2,3
  • Query for C matches 1,2,3
  • Query for D matches 1,2,3
  • Query for E matches 1,3
  • Query for A,E matches 1,3
###########################################################
# Create mapping
###########################################################
curl -XPUT 'http://127.0.0.1:9200/test/?pretty=1' -d '
{
"mappings" : {
"test" : {
"properties" : {
"tags" : {
"index" : "not_analyzed",
"type" : "string"
}
}
}
}
}
'
###########################################################
# Add some data
###########################################################
curl -XPUT 'http://127.0.0.1:9200/test/test/1?pretty=1' -d '
{
"tags" : [
"A",
"B",
"C"
]
}
'
curl -XPUT 'http://127.0.0.1:9200/test/test/2?pretty=1' -d '
{
"tags" : [
"B",
"C",
"D"
]
}
'
curl -XPUT 'http://127.0.0.1:9200/test/test/3?pretty=1' -d '
{
"tags" : [
"D"
]
}
'
###########################################################
# Example queries
###########################################################
# Should return all documents
curl -XGET 'http://127.0.0.1:9200/test/test/_search?pretty=1' -d '
{
"query" : {
"match_all" : { }
}
}'
# Tag A or B must be included (ALREADY supported in elasticsearch)
# Should only match document 1 and 2
curl -XGET 'http://127.0.0.1:9200/test/test/_search?pretty=1' -d '
{
"query" : {
"filtered" : {
"query" : {
"match_all" : { }
},
"filter" : {
"terms" : {
"tags" : [
"A",
"B"
]
}
}
}
}
}
'
# Only tags A and B are allowed, but no other tags.
# should only match document 1
curl -XGET 'http://127.0.0.1:9200/test/test/_search?pretty=1' -d '
{
"query" : {
"filtered" : {
"query" : {
"match_all" : { }
},
"filter" : {
"terms_set" : {
"terms" : {
"tags" : [
"A",
"B"
]
}
}
}
}
}
}
'
# Only tags B, C and D are allowed
# should only match document 2 and 3
curl -XGET 'http://127.0.0.1:9200/test/test/_search?pretty=1' -d '
{
"query" : {
"filtered" : {
"query" : {
"match_all" : { }
},
"filter" : {
"terms_set" : {
"terms" : {
"tags" : [
"A",
"B"
]
}
}
}
}
}
}
'
# Tag B and one missing is allowed
# should only match document 1 and 3
curl -XGET 'http://127.0.0.1:9200/test/test/_search?pretty=1' -d '
{
"query" : {
"filtered" : {
"query" : {
"match_all" : { }
},
"filter" : {
"terms_set" : {
"terms" : {
"tags" : [
"B"
]
},
"maximum_not_match" : 1
}
}
}
}
}
'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment