elasticsearch - 带有空格的 Elasticsearch 术语查询不起作用
问题描述
我正在尝试执行以下查询:
{
"query": {
"bool": {
"must": [
{
"term": {
"Thing.Name": {
"value": "(item) test",
"boost": 1
}
}
}
],
"adjust_pure_negative": true,
"boost": 1
}
}
}
这没有产生结果,我不知道为什么。我有括号和空格。我在这里有什么选择?
解决方案
您想要匹配您使用术语查询的确切值。正如 Amit 在评论中提到的那样,术语查询不使用分析器,因此它将匹配包含完全相同标记的文档,您需要修改 Thing.Name 的映射,如下所示:
{
"Thing": {
"properties": {
"Name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
}
}
}
}
如果映射是由 elastic 自动生成的,那么它的 name 字段将具有与上面类似的属性。如果它已经是这样,那么您无需在映射中进行任何修改。更新您的查询以使用Thing.Name.keyword
,而不是Thing.Name
因为类型字段keyword
不分析值并生成单个标记,即输入值本身。
所以查询将是:
{
"query": {
"bool": {
"must": [
{
"term": {
"Thing.Name.keyword": {
"value": "(item) test",
"boost": 1
}
}
}
],
"adjust_pure_negative": true,
"boost": 1
}
}
}
推荐阅读
- javascript - 如何将新值推送到现有键?Javascript
- javascript - forEach 循环后数组分配的奇怪行为
- numpy - 使用 OpenCL 优化而不是多处理池映射
- vb.net - VB.NET ODBC 连接到 DB2
- git - git commitizen 安装后不起作用
- git - 如何关闭 Git 警告:无法打开目录:权限被拒绝?
- django - Ajax 添加新事件后,FullCalendar 事件不刷新
- debugging - 我们如何限制 GDB 仅调试特定源(例如 yacc/lex)以获取 step + next 指令?
- r - 在data.table R中以编程方式将不同的函数应用于不同的列
- python - 函数式编程,如何从单个迭代器一次有效地构建多个列表?