弹性搜索中的GROUP BY

时间:2017-03-16 15:43:16

标签: elasticsearch kibana

我正在尝试使用版本5.2

在弹性搜索中编写GROUP BY查询

我想查询数据并将其限制为具有特定“标记”的数据。在下面的情况下。我想在标题或内容字段中选择包含单词“FIY”的项目,然后将其缩小,以便只搜索那些标记为“FIY”和“Competition”的文档

查询部分很好,但我很难将其限制为给定的标记。

到目前为止,我已经得到了,但我收到了错误。

“reason”:“[bool]查询不支持[terms]”,

GET advice-articles/_search
{
  "query": {
  "bool": {
  "must": [
    {
      "multi_match": {
        "query": "FIY",
        "fields": ["title", "content"]
      }
    }
  ], "filter": {
    "bool": {
      "terms": {
        "tags.tagName": [
           "competition"
        ]
      }
    }
  }
}
 }
}

示例索引是

"_index": "advice-articles",
    "_type": "article",
    "_id": "1460",
    "_score": 4.3167734,
    "_source": {
      "id": "1460",
      "title": "Your top FIY tips",
      "content": "Fix It Yourself in April 2012.",
      "tags": [
        {
          "tagName": "Fix it yourself"
        },
        {
          "tagName": "customer tips"
        },
        {
          "tagName": "competition"
        }
      ]  

我的映射如下

{
"advice-articles": {
"mappings": {
  "article": {
    "properties": {
      "content": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "tags": {
        "type": "nested",
        "properties": {
          "tagName": {
            "type": "text",
            "fields": {
              "raw": {
                "type": "keyword"
              }
            }
          }
        }
      }
    }
  }
}
}

}

2 个答案:

答案 0 :(得分:1)

过滤器内,您不需要放置bool。

GET newindex/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "multi_match": {
            "query": "FIY",
            "fields": [
              "title",
              "content"
            ]
          }
        }
      ],
      "filter": {
        "terms": {
          "tags.tagName": [
            "competition"
          ]
        }
      }
    }
  }
}

查询:

{
  "took": 4,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.2876821,
    "hits": [
      {
        "_index": "newindex",
        "_type": "test",
        "_id": "1460",
        "_score": 0.2876821,
        "_source": {
          "title": "Your top FIY tips",
          "content": "Fix It Yourself in April 2012.",
          "tags": [
            {
              "tagName": "Fix it yourself"
            },
            {
              "tagName": "customer tips"
            },
            {
              "tagName": "competition"
            }
          ]
        }
      }
    ]
  }
}

结果:

DateInterval

答案 1 :(得分:1)

bool query使用一个或多个布尔子句构建,每个子句都有一个类型化的子句。发生类型是: mustmust_notfiltershould

GET _search
{
  "query": {
    "bool": {
      "must": [
        {
          "multi_match": {
            "query": "FIY",
            "fields": [
              "title",
              "content"
            ]
          }
        },
        {
          "nested": {
            "path": "tags",
            "query": {
              "terms": {
                "tags.tagName": [
                  "competition"
                ]
              }
            }
          }
        }
      ]
    }
  }
} 

以下是如何为查询要求使用must子句。