0

is it possible to query different fields with different searchstrings in one query? For example:

{
  "query": [
    { "match": { "name": "Bob" }},
    { "match": { "title": "Awesome Title" }}]
}

where "name" and "title" are fields of documents. I know there's a multi_match query, but there I query a list of fields all with the same string...

1 Answer 1

1

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"
        }
      }
    ]
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.