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

时间:2015-11-12 11:52:34

标签: elasticsearch

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"
                    }
                  }
                ]
              ]
            }

2 个答案:

答案 0 :(得分:1)

我觉得multi match查询是最好的解决方案。

有一种多匹配查询,称为cross_fields。 它的功能如文档所述

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

答案 1 :(得分:1)

我的建议涉及使用copy_to来创建"合并"字段:

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

更新的查询很简单:

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