【问题标题】:elasticsearch how to use exact search and ignore the keyword special characters in keywords?elasticsearch如何使用精确搜索并忽略关键字中的关键字特殊字符?
【发布时间】:2019-07-25 08:26:27
【问题描述】:

我的弹性搜索索引中有一些 id 值(数字和文本组合),在我的程序中,用户可能会在搜索关键字中输入一些特殊字符。 而且我想知道无论如何可以让elasticsearch使用精确搜索并且还可以删除搜索关键字中的一些特殊字符

我已经使用自定义分析器通过一些特殊字符来分割搜索关键字。并使用 query->match 搜索数据,我仍然没有结果

  1. 数据
{
  "_index": "testdata",
  "_type": "_doc",
  "_id": "11112222",
  "_source": {
    "testid": "1MK444750"
  }
}
  1. 自定义分析器
"analysis" : {
  "analyzer" : {
    "testidanalyzer" : {
      "pattern" : """([^\w\d]+|_)""",
      "type" : "pattern"
    }
  }
}
  1. 映射
{
  "article" : {
    "mappings" : {
      "_doc" : {
        "properties" : {
          "testid" : {
            "type" : "text",
            "analyzer" : "testidanalyzer"
          }
        }
      }
    }
  }
}

这是我的弹性搜索查询

GET /testdata/_search
{
  "query": {
    "match": {
      // "testid": "1MK_444-750" // no result
      "testid": "1MK444750"
    }
  }
}

分析器成功分离了我的关键字,但我无法匹配结果中的任何内容

POST /testdata/_analyze
{
    "analyzer": "testidanalyzer",
    "text": "1MK_444-750"
}

{
  "tokens" : [
    {
      "token" : "1mk",
      "start_offset" : 0,
      "end_offset" : 3,
      "type" : "word",
      "position" : 0
    },
    {
      "token" : "444",
      "start_offset" : 4,
      "end_offset" : 7,
      "type" : "word",
      "position" : 1
    },
    {
      "token" : "750",
      "start_offset" : 8,
      "end_offset" : 11,
      "type" : "word",
      "position" : 2
    }
  ]
}

请帮忙,提前谢谢!

【问题讨论】:

    标签: database elasticsearch search


    【解决方案1】:

    首先,您应该将testid 字段建模为keyword 而不是text,这是一种更合适的数据类型。

    您想添加一个功能,在搜索时有效地忽略某些字符(_-)。你可以通过给你的字段一个规范化器来实现这一点,它告诉 Elasticsearch 在索引或搜索之前如何预处理这个字段的数据。具体来说,您可以在规范器中声明一个mapping char filter,将这些字符替换为空字符串。

    这就是所有这些更改将如何适应您的映射:

    PUT /testdata
    {
      "settings": {
        "analysis": {
          "char_filter": {
            "mycharfilter": {
              "type": "mapping",
              "mappings": [
                "_ => ",
                "- => "
              ]
            }        
          },
          "normalizer": {
            "mynormalizer": {
              "type": "custom",
              "char_filter": [
                "mycharfilter"
              ]
            }
          }
        }
      },
      "mappings": {
        "_doc": {
          "properties": {
            "testid" : {
              "type" : "keyword",
              "normalizer" : "mynormalizer"
            }
          }
        }
      }
    }
    

    以下搜索将产生相同的结果:

    GET /testdata/_search
    {
      "query": {
        "match": {
          "testid": "1MK444750"
        }
      }
    }
    
    GET /testdata/_search
    {
      "query": {
        "match": {
          "testid": "1MK_444-750"
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-23
      • 1970-01-01
      相关资源
      最近更新 更多