通配符——在查询中使用正斜杠查询数组

Wildcard-querying an array with forward slash in the query

在我的由 elasticsearch 索引的文档中,我有一个名为 IPC8s.IPC8 的字段,它是一个字符串数组,看起来像这样:

["B63H011/00"]
["B60F3", "B60K1", "B60K17", "B60K17/23", "B60K6", "B60K6"]
["G06F017/00"]
etc...

(对于好奇的人,这些是 CPC 专利分类号)

我需要使用尾随通配符查询此字段。换句话说,如果我输入 "B63H",包含 "B63H011/00" 的文档应该匹配。如果我输入 "B63H011/" 或 "B63H011/0".

也一样

我尝试了多个查询,none 成功了:

{
  query_string: {
    default_field: "IPC8s.IPC8",
    query: "(B63H*) OR (B63H011/*)",
    analyze_wildcard: true
  }
}

我也用 \"B63H*\" OR \"B63H011/*\" 试过这个,没用。

然后我尝试了:

[{
  wildcard: {
    "IPC8s.IPC8": { value: "B63H*" }
  }
},
{
  wildcard: {
    "IPC8s.IPC8": { value: "B63H011/*" }
  }
}]

这也不行。然后我尝试转义“/”,因为它必须按字面意思理解。没用。

我做错了什么?谢谢。

编辑:这是该特定字段的映射:

"IPC8s": {
  "properties": {
    "IPC8": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    }
  }
}

这是我最近的尝试,但仍然没有用(如果我不转义正斜杠,elasticsearch returns 一个错误):

{
  query_string: {
    default_field: "IPC8s.IPC8",
    query: "(B63H*) OR (B63H011\/*)",
    analyze_wildcard: true,
    analyzer: "keyword"
  }
}

编辑 2:这似乎可以解决问题:

    {
      query_string: {
        default_field: "IPC8s.IPC8.keyword",
        query: "(B63H*) OR (B63H011\/*)",
        analyze_wildcard: true,
        analyzer: "keyword"
      }
    }

使用标准分析器的文本类型将创建以下标记,因此您无法搜索 /

{
  "tokens" : [
    {
      "token" : "b63h011",
      "start_offset" : 0,
      "end_offset" : 7,
      "type" : "<ALPHANUM>",
      "position" : 0
    },
    {
      "token" : "00",
      "start_offset" : 8,
      "end_offset" : 10,
      "type" : "<NUM>",
      "position" : 1
    }
  ]
}

使用type关键字为IPC8创建一个子字段,它将按原样存储文本

GET index21/_search
{
  "query": {
    "wildcard": {
      "IPC8s.IPC8.keyword": {
        "value": "B63H011/*"
      }
    }
  }
}`