Brian Guan
Brian Guan

Reputation: 323

Elasticsearch query match + term boolean

I have documents in elasticsearch index with a "type" field, like this:

[
  {
    "id": 1,
    "serviceDescription": "a bunch of text",
    "serviceTitle": "title",
    "serviceTags":["tag1","tag2"]
    "type":"service"
  },
  {
    "id": 2,
    "companyDescription": "a bunch of text more",
    "companyTitle": "title",
    "companyTags":["tag1","tag2"]
    "type":"company"
  },...
]

I want to run a match query across all docs in my index, like this:

body = {
    "query": {
        "match": {
            "_all":"sequencing"
        }
    }
}

but add a filter to only return results where the "type" field equals "service".

Upvotes: 1

Views: 287

Answers (1)

Bhavya
Bhavya

Reputation: 16172

As far as I can understand your question, you want to query for sequencing query string, across all the fields, for that you can use the multi_match query that builds on the match query to allow multi-field queries.

If no fields are provided, the multi_match query defaults to the index.query.default_field index settings, which in turn defaults to *. This extracts all fields in the mapping that are eligible to term queries and filters the metadata fields. All extracted fields are then combined to build a query.

Search Query:

    {
  "query": {
    "bool": {
      "must": [
        {
          "multi_match": {
            "query": "bunch of text"
          }
        }
      ],
      "filter": {
        "term": {
          "type": "service"
        }
      }
    }
  }
}

Search Result:

"hits": [
      {
        "_index": "64867032",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.8630463,
        "_source": {
          "id": 1,
          "serviceDescription": "a bunch of text",
          "serviceTitle": "title",
          "serviceTags": [
            "tag1",
            "tag2"
          ],
          "type": "service"
        }
      }
    ]

Upvotes: 1

Related Questions