Skip to content

Instantly share code, notes, and snippets.

@jprante
Created September 10, 2013 12:14
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 jprante/6508544 to your computer and use it in GitHub Desktop.
Save jprante/6508544 to your computer and use it in GitHub Desktop.
Demo of Elasticsearch/Lucene precedence of AND/OR/NOT
# Demo of Elasticsearch/Lucene precedence of AND/OR/NOT
curl -XDELETE 'localhost:9200/test'
curl -XPUT 'localhost:9200/test/data/1' -d '
{
"name" : "ABC",
"text" : "Hey there"
}
'
curl -XPUT 'localhost:9200/test/data/2' -d '
{
"name" : "DEF",
"text" : "Hello there"
}
'
curl -XPUT 'localhost:9200/test/data/3' -d '
{
"name" : "GHI",
"text" : "NULL"
}
'
curl -XGET 'localhost:9200/test/_refresh'
echo
echo "Query 1"
# 1 hit: ABC
curl -XGET 'localhost:9200/test/data/_search?pretty' -d '
{
"query" : {
"query_string": {
"query": "name:abc OR text:hey"
}
}
}'
echo
echo "Query 2"
# 3 hits
curl -XGET 'localhost:9200/test/data/_search?pretty' -d '
{
"query" : {
"query_string": {
"query": "name:*"
}
}
}'
echo
echo "Query 3"
# 2 hits
curl -XGET 'localhost:9200/test/data/_search?pretty' -d '
{
"query" : {
"query_string": {
"query": "name:abc OR name:def"
}
}
}'
echo
echo "Query 4"
# 2 hits
curl -XGET 'localhost:9200/test/data/_search?pretty' -d '
{
"query" : {
"query_string": {
"query": "(name:abc OR name:def) AND text:*"
}
}
}'
echo
echo "Query 5"
# 2 hits
curl -XGET 'localhost:9200/test/data/_search?pretty' -d '
{
"query" : {
"query_string": {
"query": "name:abc OR (name:def AND text:*)"
}
}
}'
echo
echo "Query 6"
# 1 hit: DEF
curl -XGET 'localhost:9200/test/data/_search?pretty' -d '
{
"query" : {
"query_string": {
"query": "name:abc OR name:def AND text:*"
}
}
}'
echo
echo "Query 7"
# 1 hit: ABC
curl -XGET 'localhost:9200/test/data/_search?pretty' -d '
{
"query" : {
"query_string": {
"query": "text:* AND name:abc OR name:def"
}
}
}'
echo
echo "Query 8"
# 1 hit: ABC
curl -XGET 'localhost:9200/test/data/_search?pretty' -d '
{
"query" : {
"query_string": {
"query": "name:abc AND text:* OR name:def"
}
}
}'
echo
echo "Query 9"
# 1 hit: DEF
curl -XGET 'localhost:9200/test/data/_search?pretty' -d '
{
"query" : {
"query_string": {
"query": "name:abc OR name:def AND text:hello"
}
}
}'
echo
echo "Query 10"
# no hits
curl -XGET 'localhost:9200/test/data/_search?pretty' -d '
{
"query" : {
"query_string": {
"query": "text:hello AND name:abc OR name:def"
}
}
}'
echo
echo "Query 11"
# no hits
curl -XGET 'localhost:9200/test/data/_search?pretty' -d '
{
"query" : {
"query_string": {
"query": "name:abc AND text:hello OR name:def"
}
}
}'
echo
echo "Query 12"
# 2 hits: ABC, DEF
curl -XGET 'localhost:9200/test/data/_search?pretty' -d '
{
"query" : {
"query_string": {
"query": "name:abc OR name:def NOT text:NULL"
}
}
}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment