elasticsearch

Elasticsearch搜索
例如:["物理","物 理","物,,理","地理","生物","奇妙物理学","理物"]
搜索“物理”只会出现["物理","奇妙物理学"],只出现不分词,不乱序,中间没有空格和符号的结果,
看了半天match_phrase比较符合,但是中间有符号和空格的也会出现,
大家有什么好的解决办法吗?

可能需要分词器吧,es自身对中文好像不大友好

可以使用一个keyword类型的字段来存储原始的字符串,然后使用match查询来进行搜索。

  1. 创建一个新的索引,并为需要查询的字段设置一个新的keyword类型的字段,例如将原始字段名为“text”,新的字段名为“text_keyword”:
PUT my_index
{
  "mappings": {
    "properties": {
      "text": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword"
          }
        }
      }
    }
  }
}
  1. 将数据导入到新的索引中:
PUT my_index/_doc/1
{
  "text": "物理"
}
PUT my_index/_doc/2
{
  "text": "物 理"
}
PUT my_index/_doc/3
{
  "text": "物,,理"
}
PUT my_index/_doc/4
{
  "text": "地理"
}
PUT my_index/_doc/5
{
  "text": "生物"
}
PUT my_index/_doc/6
{
  "text": "奇妙物理学"
}
PUT my_index/_doc/7
{
  "text": "理物"
}

3.使用match查询来进行搜索,查询时指定使用新的keyword字段:

GET my_index/_search
{
  "query": {
    "match": {
      "text_keyword": "物理"
    }
  }
}