我正在寻找elasticsearch中最简单的查询系统,其中唯一的分隔符是空格。我想查询几乎所有类型的字符都附带的文本中的用户名,例如@用户名或@ 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"
的结果相同因此,总而言之,是否有任何简单的方法来配置elasticsearch或查询以仅使用空格(最好是基本标点符号)进行标记化?
最佳答案
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"
}
}
]
}
}
关于elasticsearch - 如何在elasticsearch simple_query_string中包含除空格以外的所有字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59435314/