在Elasticsearch中,我如何使用空格进行通配符搜索



我当前有一系列对象,其中包括:

        {
          "key": "party",
          "str_val": "THE TWITTER INC"
        }

        {
          "key": "party",
          "str_val": "twitter inc"
        }

我映射的相关部分是:

          "key": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "str_val": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }

当我尝试执行通配符匹配时,如下所示:

"wildcard": {
    "metadata.str_val": "*twitter*"
}

返回两个文档。但是,如果我修改查询以包括一个空间:

"wildcard": {
    "metadata.str_val": "*twitter inc*"
}

没有返回文件。我如何使用空格进行通配符搜索?

选项1:

在您当前的映射(即不使用分析仪)的情况下,query_string应该适合您。

PUT my_index2/doc/1
{
  "key": "party",
  "str_val": "THE TWITTER INC"
}
PUT my_index2/doc/2
{
  "key": "party",
  "str_val": "twitter inc"
}
POST my_index2/_search
{
  "query": {
    "query_string": {
      "default_field": "str_val",
      "query": "*twitter inc*"
    }
  }
}

选项2:

但是,如果要使用通配符,则需要添加以下自定义分析仪:

PUT my_index/
{
  "settings": {
    "analysis": {
      "analyzer": {
        "k_lowercase": {
          "type": "custom",
          "tokenizer": "keyword",
          "filter": [
            "lowercase"
          ]
        }
      }
    }
  },
   "mappings": {
      "doc": {
        "properties": {
          "key": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }              
          },
          "str_val": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            },
             "analyzer":"k_lowercase"
          }
        }
      }
    }
}

现在添加分析仪后添加记录:

PUT my_index/doc/1
{
  "key": "party",
  "str_val": "THE TWITTER INC"
}
PUT my_index/doc/2
{
  "key": "party",
  "str_val": "twitter inc"
}
POST my_index/_search
{
  "query": {
    "wildcard": {
      "str_val": {
        "value": "*twitter inc*"
      }
    }
  }
}

最新更新