0

I am trying to search a index in elastic search with the following body:

{
    "query": {
        "bool": {
            "must": [
                {
                    "query_string": {
                        "query": "a*",
                        "default_operator": "AND"
                },
                {
                    "match": {
                        "country": "Spain"
                    }
                },
                {
                    "match": {
                        "city": "Madrid"
                    }
                }
            ]
        }
    }
}

here is my indexed results:

 [
  {
    _id: "T__cU34BAF3zuzOUa7rD"
    _index: "es"
    _score: 1
    _source: {
      name: "Santiago",
      country: "Spain",
      city: "Madrid",
    }
    _type: "_doc"
  },
  ...
]

So this supposed to search for a word found in field city inside the country.

country and city are optional by the way and it should return all queries that start with a if city and country are empty.

3
  • Hi @Angels, could you add some examples of documents ? Commented Jan 13, 2022 at 14:24
  • And mappings please Commented Jan 13, 2022 at 20:43
  • I take the document from the DB which includes those fields that I just shared. I think I was not able to explain the problem correctly. Commented Jan 14, 2022 at 8:32

1 Answer 1

2

According to your problem, you want to return documents that have values that start with supposing s. And, along with this, the country and city conditions are optional.

You can combine query string and match query using the boolean query. Here must clause, works as a logical AND operator, and the should clause works as a logical OR operator.

Adding a working example with index data, search query and search result

Index Data:

PUT <index-name>/_doc/1
{
  "name": "Santiago",
  "country": "Spain",
  "city": "Madrid"
}

PUT <index-name>/_doc/2
{
  "name": "San Franciscor",
  "country": "",
  "city": "Madrid"
}

Search Query:

POST <index-name>/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "query": "s*"
          }
        }
      ],
      "should": [
        {
          "match": {
            "country": "Spain"
          }
        },
        {
          "match": {
            "city": "Madrid"
          }
        }
      ]
    }
  }
}

Search Result:

 "hits" : [
      {
        "_index" : "<index-name>",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.4700036,
        "_source" : {
          "name" : "Santiago",
          "country" : "Spain",
          "city" : "Madrid"
        }
      },
      {
        "_index" : "<index-name>",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.1823215,
        "_source" : {
          "name" : "San Franciscor",
          "country" : "",
          "city" : "Madrid"
        }
      }
    ]
Sign up to request clarification or add additional context in comments.

2 Comments

@Angels its been a long time ! Can you please accept and upvote the answer, if it helped you resolve your issue. TIA ;-)
Sorry for the late reply. I have a new question could u please have a look when you had time please?

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.