Skip to content

Instantly share code, notes, and snippets.

@clintongormley
Created February 11, 2013 12:45
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 clintongormley/4754230 to your computer and use it in GitHub Desktop.
Save clintongormley/4754230 to your computer and use it in GitHub Desktop.
Create your index:
curl -XPUT 'http://127.0.0.1:9200/site462/?pretty=1' -d '
{
"mappings" : {
"contact_notes" : {
"type" : "object",
"properties" : {
"notes_emails" : {
"type" : "object",
"properties" : {
"subject" : {
"type" : "string",
"term_vector" : "with_positions_offsets"
},
"_id" : {
"type" : "integer"
},
"modification_date" : {
"type" : "date"
},
"creation_date" : {
"type" : "date"
},
"text" : {
"type" : "string",
"term_vector" : "with_positions_offsets"
},
"reference_type" : {
"type" : "integer"
},
"modified_by" : {
"type" : "integer"
},
"type" : {
"index" : "not_analyzed",
"type" : "string"
},
"activity_type" : {
"index" : "not_analyzed",
"type" : "string"
},
"created_by" : {
"index" : "not_analyzed",
"type" : "integer"
}
}
}
}
}
}
}
'
Bulk index some data:
curl -XPOST 'http://127.0.0.1:9200/site462/_bulk?pretty=1' -d '
{"index" : {"_index" : "site462", "_id" : "1340", "_type" : "contact_notes"}}
{"notes_emails" : {"modification_date" : "2012-03-16T09:40:31.725Z", "creation_date" : "2012-03-16T09:40:31.725Z", "reference_type" : 10, "text" : "my test note", "_id" : 117, "modified_by" : 342, "type" : "ActivityContact", "created_by" : 342}}
{"index" : {"_index" : "site462", "_id" : "1706", "_type" : "contact_notes"}}
{"notes_emails" : {"modification_date" : "2012-07-04T22:16:18.644Z", "creation_date" : "2012-07-04T21:57:57.665Z", "reference_type" : 10, "text" : "another test note", "_id" : 329, "modified_by" : 363, "type" : "ActivityContact", "created_by" : 363}}
{"index" : {"_index" : "site462", "_id" : "1703", "_type" : "contact_notes"}}
{"notes_emails" : {"modification_date" : "2012-07-03T11:08:53.699Z", "creation_date" : "2011-12-28T07:29:07.715Z", "reference_type" : 70, "text" : "test note", "_id" : 37, "modified_by" : 342, "type" : "ActivityContact", "created_by" : 342}}
{"index" : {"_index" : "site462", "_id" : "1341", "_type" : "contact_notes"}}
{"notes_emails" : {"modification_date" : "2011-12-05T13:21:57.668Z", "creation_date" : "2011-12-05T13:21:57.668Z", "text" : "test d", "_id" : 26, "modified_by" : 342, "type" : "ActivityContact", "created_by" : 342}}
'
Your original query (with "from" adjusted):
curl -XGET 'http://127.0.0.1:9200/site462/_search?pretty=1' -d '
{
"from" : 0,
"query" : {
"filtered" : {
"filter" : {
"term" : {
"notes_emails.reference_type" : "10"
}
},
"query" : {
"bool" : {
"must" : [
{
"term" : {
"notes_emails.reference_type" : "10"
}
}
]
}
}
}
},
"size" : 10
}
'
# [Mon Feb 11 13:22:14 2013] Response:
# {
# "hits" : {
# "hits" : [
# {
# "_source" : {
# "notes_emails" : {
# "modification_date" : "2012-03-16T09:40:31.725Z",
# "creation_date" : "2012-03-16T09:40:31.725Z",
# "_id" : 117,
# "text" : "my test note",
# "reference_type" : 10,
# "modified_by" : 342,
# "type" : "ActivityContact",
# "created_by" : 342
# }
# },
# "_score" : 1,
# "_index" : "site462",
# "_id" : "1340",
# "_type" : "contact_notes"
# },
# {
# "_source" : {
# "notes_emails" : {
# "modification_date" : "2012-07-04T22:16:18.644Z",
# "creation_date" : "2012-07-04T21:57:57.665Z",
# "_id" : 329,
# "text" : "another test note",
# "reference_type" : 10,
# "modified_by" : 363,
# "type" : "ActivityContact",
# "created_by" : 363
# }
# },
# "_score" : 1,
# "_index" : "site462",
# "_id" : "1706",
# "_type" : "contact_notes"
# }
# ],
# "max_score" : 1,
# "total" : 2
# },
# "timed_out" : false,
# "_shards" : {
# "failed" : 0,
# "successful" : 5,
# "total" : 5
# },
# "took" : 4
# }
Your query rewritten to just use a filter clause:
curl -XGET 'http://127.0.0.1:9200/site462/_search?pretty=1' -d '
{
"query" : {
"constant_score" : {
"filter" : {
"term" : {
"notes_emails.reference_type" : 10
}
}
}
}
}
'
Or combined with a full text query:
curl -XGET 'http://127.0.0.1:9200/site462/_search?pretty=1' -d '
{
"query" : {
"filtered" : {
"filter" : {
"term" : {
"notes_emails.reference_type" : 10
}
},
"query" : {
"match" : {
"text" : "test"
}
}
}
}
}
'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment