【问题标题】:GROUP BY in elasticsearchelasticsearch中的GROUP BY
【发布时间】:2017-03-17 01:00:55
【问题描述】:

我正在尝试使用 5.2 版在弹性搜索中编写 GROUP BY 查询

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

查询部分很好,但我正在努力将其限制为给定的标签。

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

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

}

【问题讨论】:

标签: elasticsearch kibana


【解决方案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 子句来满足查询要求。

【讨论】:

  • 如果我使用上面它不返回任何结果,使用应该返回结果而不是正确的文档
  • @simon1230756 错过了标签的嵌套部分,更新了查询。请立即检查。
【解决方案2】:

过滤器 内你不需要放 bool。

POST newindex/test/1460333
{
  "title": "Your top FIY tips",
  "content": "Fix It Yourself in April 2012.",
  "tags": [
    {
      "tagName": "Fix it yourself"
    },
    {
      "tagName": "customer tips"
    },
    {
      "tagName": "shoud not return"
    }
  ]
}

POST newindex/test/1460
{
  "title": "Your top FIY tips",
  "content": "Fix It Yourself in April 2012.",
  "tags": [
    {
      "tagName": "Fix it yourself"
    },
    {
      "tagName": "customer tips"
    },
    {
      "tagName": "competition"
    }
  ]
}

查询:

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

【讨论】:

  • 如果我使用上面它不返回任何结果,使用应该返回结果而不是正确的文档
  • @simon1230756 查看(上图)完整示例,使用 elasticsearch 5.2 可以正常工作
  • 谢谢,仍然没有为我工作,但注意到我的映射被设置为嵌套,现在一切都很好:-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-31
  • 1970-01-01
  • 2016-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多