【问题标题】:Using Elasticsearch to search special characters使用 Elasticsearch 搜索特殊字符
【发布时间】:2016-12-13 17:47:24
【问题描述】:

如何强制 Elasticsearch query_string 将“@”识别为简单字符?

假设我有一个索引,并且我添加了一些文档,通过以下语句:

POST test/item/_bulk
{"text": "john.doe@gmail.com"} 
{"text": "john.doe@outlook.com"}
{"text": "john.doe@gmail.com, john.doe@outlook.com"}
{"text": "john.doe[at]gmail.com"}
{"text": "john.doe gmail.com"}

我想要这个搜索:

GET test/item/_search
{
    "query": 
    {
        "query_string": 
        {
            "query": "*@gmail.com",
            "analyze_wildcard": "true",
            "allow_leading_wildcard": "true",
            "default_operator": "AND"
        }
    }
}

只返回第一个第三个文档。

我尝试了 3 种映射: 首先我尝试了-

PUT test
{
  "settings": {
    "analysis": {
      "analyzer": {
        "email_analyzer": {
          "tokenizer": "email_tokenizer"
        }
      },
      "tokenizer": {
        "email_tokenizer": {
          "type": "uax_url_email"
        }
      }
    }
  },
  "mappings": {
    "item": {
      "properties": {
        "text": {
          "type": "string",
          "analyzer": "email_analyzer"
        }
      }
    }
  }
}

比我试过的 -

PUT test
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "my_tokenizer"
        }
      },
      "tokenizer": {
        "my_tokenizer": {
          "type": "whitespace"
        }
      }
    }
  },
  "mappings": {
    "item": {
      "properties": {
        "text": {
          "type": "string",
          "analyzer": "my_analyzer"
        }
      }
    }
  }
}

我也试过这个 -

PUT test
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "my_tokenizer"
        }
      },
      "tokenizer": {
        "my_tokenizer": {
          "type": "whitespace"
        }
      }
    }
  },
  "mappings": {
    "item": {
      "properties": {
        "text": {
          "type": "string",
          "index": "not_analyzed"
        }
      }
    }
  }
}

以上都不起作用,实际上他们都返回了所有文件。 是否有一个分析器/标记器/参数可以让 Elasticsearch 像对待任何其他字符一样确认“@”符号

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    这适用于您的最后一个设置,将文本置于不分析:

    GET test/item/_search
    {
        "query": 
        {
            "wildcard": 
            {
                "text": "*@gmail.com*"
            }
        }
    }
    

    使用未分析字段时,应使用术语级别查询,而不是全文级别查询:https://www.elastic.co/guide/en/elasticsearch/reference/2.3/term-level-queries.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-11
      • 2015-07-24
      • 1970-01-01
      • 2014-04-25
      • 2015-08-24
      • 1970-01-01
      • 1970-01-01
      • 2012-02-02
      相关资源
      最近更新 更多