【问题标题】:Search substring in elastic search在弹性搜索中搜索子字符串
【发布时间】:2021-02-20 03:43:41
【问题描述】:

下面是我想要的-

假设在弹性搜索下面存储了

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

现在当我搜索时

那么我必须得到

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

狐狸测试那么我必须得到

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

快速测试 123 那么我必须得到

  • 空响应

123 @那么我必须得到

  • 快速 123 @ 测试

123 $那么我必须得到

  • 空响应

【问题讨论】:

    标签: elasticsearch elasticsearch-6.5


    【解决方案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": []
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-07
      相关资源
      最近更新 更多