elasticsearch - 如何在elasticsearch simple_query_string中包含除空格之外的所有字符?
问题描述
我正在寻找弹性搜索中最简单的查询系统,其中唯一的分隔符是空格。我想查询带有几乎所有类型字符的文本中的用户名,例如@username 或@u$er-na_me
它认为这很容易,但经过长时间的搜索,我发现了一个“空白”分析器:
query.json:
{
"query": {
"simple_query_string" : {
"query": "@username",
"fields": ["mytextfield"],
"analyzer": "whitespace",
"default_operator": "and"
}
}
}
其中,我运行:curl -X POST "http://localhost:9200/my.index/_search?pretty" -H 'Content-Type: application/json' -d '@query.json'
但是,它什么也不返回。
其他详情:
"query": "username"
仍然有效如果我删除
"analyzer": "whitespace"
,那么我会得到相同的结果"query": "username"
和"query": "@username"
我在 SO 中看到过类似的帖子(例如,带有特殊字符的简单查询字符串,例如 (和 = ),它们似乎创建了新的索引或映射。如果这是要走的路,我将不胜感激一些资源来理解工作流程。
因此,总而言之,是否有任何简单的方法可以配置弹性搜索或查询以仅使用空格(以及理想的基本标点符号)进行标记?
解决方案
toto_tico,您需要分析您的字段并在您的映射中指定它,如下所示:
1) 使用单个分析字段创建索引:
PUT totoindex
{
"mappings": {
"_doc": {
"properties": {
"mytextfield":{"type":"text", "analyzer": "whitespace"}
}
}
}
}
2)索引一些示例文档:
POST totoindex/_doc/
{
"mytextfield": "@toto_tico"
}
3)搜索文档:
POST totoindex/_doc/_search
{
"query": {
"simple_query_string" : {
"query": "@toto_tico",
"fields": ["mytextfield"]
}
}
}
回复 :
{
"took": 12,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.2876821,
"hits": [
{
"_index": "totoindex",
"_type": "_doc",
"_id": "MtorKW8BavUEUOqEr6k_",
"_score": 0.2876821,
"_source": {
"mytextfield": "@toto_tico"
}
}
]
}
}
推荐阅读
- haskell - Haskell 定义数据类型
- php - Http 到 https 错误“ERR_TOO_MANY_REDIRECTS”
- python - 如何迭代 cython(或 numba)中的列表列表?
- html - How to make a text disapper under an image, after scrolling it up?
- node.js - sequelize-cli 在哪里保存种子列表?
- linux - Postgresql - 显示 INTEGER、BIGINT 的用法
- microsoft-graph-toolkit - c#和js之间的登录组合 Msal-provider
- pandas - 基于特定条件的 Pandas 中的 Groupby 累积总和
- java - 使用带有 Node.js 的 aws-secretsmanager-jdbc 连接到 RDS?
- javascript - JS 中的范围导出