下面是我想要的-
假设在下面的弹性搜索中存储
现在当我搜索
快然后我必须得到
狐狸测试 那我必须得
快速测试 123 那我必须要
123 @ 那么我必须得到
123 $ 那么我必须得到
答案 0 :(得分:1)
您可以使用 match_phrase query 来获得预期的输出,并且您需要将 whitespace tokenizer 与 lowercase token filter 一起使用才能使其工作。将添加工作示例。
索引映射
{
"settings": {
"analysis": {
"analyzer": {
"lwhitespace": {
"type": "custom",
"tokenizer": "whitespace",
"filter": [
"lowercase"
]
}
}
}
},
"mappings": {
"properties": {
"message": {
"type": "text",
"analyzer": "lwhitespace"
}
}
}
}
索引示例文档
{
"message" : "Quick Fox Test"
}
{
"message" : "Quick Fox"
}
{
"message" : "Quick 123@ Test"
}
{
"message" : "Quick"
}
{
"message" : "Fox Test"
}
{
"message" : "Fox Quick Test"
}
搜索查询
{
"query": {
"match_phrase": {
"message": "Quick"
}
}
}
搜索结果
"hits": [
{
"_index": "66287786",
"_type": "_doc",
"_id": "4",
"_score": 0.3147369,
"_source": {
"message": "Quick"
}
},
{
"_index": "66287786",
"_type": "_doc",
"_id": "2",
"_score": 0.25613075,
"_source": {
"message": "Quick Fox"
}
},
{
"_index": "66287786",
"_type": "_doc",
"_id": "1",
"_score": 0.21592417,
"_source": {
"message": "Quick Fox Test"
}
},
{
"_index": "66287786",
"_type": "_doc",
"_id": "3",
"_score": 0.21592417,
"_source": {
"message": "Quick 123@ Test"
}
},
{
"_index": "66287786",
"_type": "_doc",
"_id": "6",
"_score": 0.21592417,
"_source": {
"message": "Fox Quick Test"
}
}
]
搜索查询 fox test
{
"query": {
"match_phrase": {
"message": "fox test"
}
}
}
"hits": [
{
"_index": "66287786",
"_type": "_doc",
"_id": "5",
"_score": 0.93851364,
"_source": {
"message": "Fox Test"
}
},
{
"_index": "66287786",
"_type": "_doc",
"_id": "1",
"_score": 0.79118896,
"_source": {
"message": "Quick Fox Test"
}
}
]
123@
的搜索查询
"query": {
"match_phrase": {
"message": "123@"
}
}
}
搜索响应
"hits": [
{
"_index": "66287786",
"_type": "_doc",
"_id": "3",
"_score": 1.3792357,
"_source": {
"message": "Quick 123@ Test"
}
}
]
Quick Test 123
的搜索查询
{
"query": {
"match_phrase": {
"message": "Quick Test 123"
}
}
}
搜索结果为空
"hits": []