Skip to content

Instantly share code, notes, and snippets.

@robfaraj
Created March 15, 2011 14:37
Show Gist options
  • Save robfaraj/e251f1d3f0e17c2fa7ae to your computer and use it in GitHub Desktop.
Save robfaraj/e251f1d3f0e17c2fa7ae to your computer and use it in GitHub Desktop.
// Mapping
curl -XPUT 'http://localhost:9200/index/' -d '{
"settings" : {
"number_of_shards" : 1,
"number_of_replicas" : 1
},
"mappings" : {
"type" : {
"_source" : {"enabled" : false},
"_id" : {"store" : "yes"},
"properties" : {
"title" : {"type" : "string", "store": "yes", "boost" : 5.0},
"description" : {"type" : "string", "store": "no"},
"item" : {"type" : "string", "store": "no", "boost" : 3.0 },
"site" : {"type" : "string", "store": "no", "boost": 2.0, "index" : "not_analyzed"},
"category" : {"type" : "integer", "store" : "no"},
"subcategory" : {"type" : "integer", "store" : "no"},
"status_id" : {"type" : "integer", "store" : "no"},
"site_id" : {"type" : "integer", "store" : "yes"},
"item_id" : {"type" : "integer", "store" : "yes"}
}
}
}
}'
// Put Some Sample Data
curl -s -XPUT http://localhost:9200/index/type/1/ -d '
{
"title":"foobar",
"site":"elasticsearch",
"description":"elasticsearch vs lucene vs solr vs sphinx vs mysql fulltext",
"item":"forum post",
"category":4,
"subcategory":5,
"status_id":1,
"site_id":7,
"item_id":9
}
'
curl -s -XPUT http://localhost:9200/index/type/2/ -d '
{
"title":"foobared",
"site":"elasticsearch",
"description":"elasticsearch vs the machine",
"item":"forum post",
"category":3,
"subcategory":8,
"status_id":1,
"site_id":7,
"item_id":13
}
'
// Searching for term + filter
curl -XPOST 'http://localhost:9200/index/type/_search?pretty=true&fields=_id,title' -d '
{
"query" : {
"query_string" : {
"query" : "foobar"
}
},
"filter" : {
"term" : {
"status_id" : 1
}
}
}
'
// Searching for filter across all records (with no term provided)
curl -XPOST 'http://localhost:9200/index/type/_search?pretty=true&fields=_id,item_id,title' -d '
{
"query" : {
"constant_score" : {
"filter": {
"term": {
"status_id": 1
}
}
}
}
}
'
// Searching for filter across all records (w/no term) + with facets
curl -XPOST 'http://localhost:9200/index/type/_search?pretty=true&fields=_id,site_id,item_id,title' -d '
{
"query" : {
"constant_score" : {
"filter": {
"term": {
"status_id": 1
}
},
"facets" : {
"sites" : {
"terms" : { "field" : "site_id" }
}
}
}
}
}
'
@clintongormley
Copy link

// Searching for term + filter
// Use a filtered query:

curl -XPOST 'http://localhost:9200/index/type/_search?pretty=true&fields=_id,item_id,title' -d '
{
    "query" : {
        "filtered": {
            "filter": {
                "term": {
                    "status_id": 1
                }
            },
            "query": {
                "query_string" : {
                  "query" : "foobar"
                }
            },
        }
    }
}
'

// Searching for filter across all records (with no term provided)
// Wrap your filter in a constant_score query

curl -XPOST 'http://localhost:9200/index/type/_search?pretty=true&fields=_id,item_id,title' -d '
{
    "query" : {
        "constant_score" : {
            "filter":   {
                "term": {
                    "status_id": 1
                }
            }
        }
    }
}
'

@robfaraj
Copy link
Author

First one didn't work for me. I got a parse error. Changed it to this and it works...
curl -XPOST 'http://localhost:9200/index/type/_search?pretty=true&fields=_id,title' -d '
{
"query" : {
"query_string" : {
"query" : "foobar"
}
},

  "filter" : {
    "term" : {
      "status_id" : 1
      }
   }  
}
'

Constant score works nicely.

@clintongormley
Copy link

clintongormley commented Mar 15, 2011 via email

@robfaraj
Copy link
Author

np. thanks for the help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment