martins
martins

Reputation: 10009

I want my query to treat the content of two columns as one

I have a set of news articles. These have both tags and articleTags. Our API has a endpoint that returns articles that matches all tags. E.g. searching for an article that contains both sport and fail:

            "bool": {
              "must": [
                [
                  {
                    "term": {
                      "tags": "sport"
                    }
                  },
                  {
                    "term": {
                      "tags": "fail"
                    }
                  },
                  {
                    "term": {
                      "articleTags": "sport"
                    }
                  },
                  {
                    "term": {
                      "articleTags": "fail"
                    }
                  }
                ]
              ]
            }

This worked when we only had tags, but when we introduced articleTags then it obviously didn't work as expected.

Is there a way we could make Elasticsearch treat tags and articleTags as one namespace so I could do a query like this?

            "bool": {
              "must": [
                [
                  {
                    "term": {
                      "mergedTags": "sport"
                    }
                  },
                  {
                    "term": {
                      "mergedTags": "fail"
                    }
                  }
                ]
              ]
            }

Upvotes: 1

Views: 31

Answers (2)

Andrei Stefan
Andrei Stefan

Reputation: 52368

My suggestion involves using copy_to to create that "merged" field:

    "tags": {
      "type": "string",
      "copy_to": "mergedTags"
    },
    "articleTags": {
      "type": "string",
      "copy_to": "mergedTags"
    },
    "mergedTags": {
      "type": "string"
    }

And the updated query is a simple as:

  "query": {
    "bool": {
      "must": [
        [
          {
            "term": {
              "mergedTags": "sport"
            }
          },
          {
            "term": {
              "mergedTags": "fail"
            }
          }
        ]
      ]
    }
  }

Upvotes: 1

Vineeth Mohan
Vineeth Mohan

Reputation: 19283

I feel multi match query would be the best solution here.

There is a type of multi match query which is called cross_fields . And its function as told by the documentation is

Treats fields with the same analyzer as though they were one big field. Looks for each word in any field. See cross_fields.

Upvotes: 1

Related Questions