在弹性搜索中搜索子串

时间:2021-02-20 03:43:41

标签: elasticsearch elasticsearch-6.5

下面是我想要的-

假设在下面的弹性搜索中存储

  1. 快速狐狸测试
  2. 快狐
  3. 快速 123 @ 测试
  4. 快速
  5. 狐狸测试
  6. Fox 快速测试

现在当我搜索

然后我必须得到

  • 快速狐狸测试
  • 快狐
  • 快速 123 @ 测试
  • 快速
  • Fox 快速测试

狐狸测试 那我必须得

  • 快速狐狸测试
  • 狐狸测试

快速测试 123 那我必须要

  • 空响应

123 @ 那么我必须得到

  • 快速 123 @ 测试

123 $ 那么我必须得到

  • 空响应

1 个答案:

答案 0 :(得分:1)

您可以使用 match_phrase query 来获得预期的输出,并且您需要将 whitespace tokenizerlowercase 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": []