You can try this query to search for documents that match both the conditions ("name": "Bob" and "title": "Awesome Title"). Replace the <index_name> with the name of the index.
$ curl -XGET 'localhost:9200/<index_name>/_search?pretty' -H 'Content-Type: application/json' -d'
{
"query": {
"bool": {
"should": [
{
"match_phrase": {
"name": "Bob"
}
},
{
"match_phrase": {
"title": "Awesome Title"
}
}
],
"minimum_should_match": 2
}
}
}
'
Illustration:
(a) Index 4 documents
# Doc 1 - Only "name" matches
$ curl -X PUT "localhost:9200/office/doc/o001" -H 'Content-Type: application/json' -d'
{
"name" : "Bob",
"title" : "Senior Staff",
"description" : "Developing new products"
}
'
# Doc 2 - None of the criteria match
$ curl -X PUT "localhost:9200/office/doc/o002" -H 'Content-Type: application/json' -d'
{
"name" : "Tom",
"title" : "Marketing Manager",
"description" : "Shows and events"
}
'
# Doc 3 - Only "title" matches
$ curl -X PUT "localhost:9200/office/doc/o003" -H 'Content-Type: application/json' -d'
{
"name" : "Liz",
"title" : "Awesome Title",
"description" : "Recruiting talent"
}
'
# Doc 4 - Both "name" and "title" match - Expected in result
$ curl -X PUT "localhost:9200/office/doc/o004" -H 'Content-Type: application/json' -d'
{
"name" : "Bob",
"title" : "Awesome Title",
"description" : "Finance Operations"
}
'
(b) Verify that the documents were indexed
$ curl 'localhost:9200/office/_search?q=*'
# Output
{"took":19,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":4,"max_score":1.0,"hits":[{"_index":"office","_type":"doc","_id":"o003","_score":1.0,"_source":
{
"name" : "Liz",
"title" : "Awesome Title",
"description" : "Recruiting talent"
}
},{"_index":"office","_type":"doc","_id":"o001","_score":1.0,"_source":
{
"name" : "Bob",
"title" : "Senior Staff",
"description" : "Developing new products"
}
},{"_index":"office","_type":"doc","_id":"o004","_score":1.0,"_source":
{
"name" : "Bob",
"title" : "Awesome Title",
"description" : "Finance Operations"
}
},{"_index":"office","_type":"doc","_id":"o002","_score":1.0,"_source":
{
"name" : "Tom",
"title" : "Marketing Manager",
"description" : "Shows and events"
}
(c) Run the query. Result is the one doc (with id=o004) that matches both criteria:
$ curl -XGET 'localhost:9200/office/_search?pretty' -H 'Content-Type: application/json' -d'
{
"query": {
"bool": {
"should": [
{
"match_phrase": {
"name": "Bob"
}
},
{
"match_phrase": {
"title": "Awesome Title"
}
}
],
"minimum_should_match": 2
}
}
}
'
(d) Get the query result
{
"took" : 27,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 1.4261419,
"hits" : [
{
"_index" : "office",
"_type" : "doc",
"_id" : "o004",
"_score" : 1.4261419,
"_source" : {
"name" : "Bob",
"title" : "Awesome Title",
"description" : "Finance Operations"
}
}
]
}
}