Elasticsearch中的带状疱疹,为什么自定义分析器的这个例子失败了?

Jab*_*abb 9 elasticsearch

我将我的问题改为完整的卷曲娱乐脚本.这样可以更容易地重现问题(使用自定义分析器搜索失败).我正在使用最新的ES版本

删除旧数据

curl -XDELETE "http://localhost:9200/test_shingling"
Run Code Online (Sandbox Code Playgroud)

使用设置创建索引

curl -XPOST "http://localhost:9200/test_shingling/" -d '{
  "settings": {
    "index": {
      "number_of_shards": 10,
      "number_of_replicas": 1
    },
    "analysis": {
      "analyzer": {
        "ShingleAnalyzer": {
          "tokenizer": "BreadcrumbPatternAnalyzer",
          "filter": [
            "standard",
            "lowercase",
            "filter_stemmer",
            "filter_shingle"
          ]
        }
      },
      "filter": {
        "filter_shingle": {
          "type": "shingle",
          "max_shingle_size": 2,
          "min_shingle_size": 2,
          "output_unigrams": false
        },
        "filter_stemmer": {
          "type": "porter_stem",
          "language": "English"
        }
      },
      "tokenizer": {
        "BreadcrumbPatternAnalyzer": {
          "type": "pattern",
          "pattern": " |\\$\\$\\$"
        }
      }
    }
  }
}'
Run Code Online (Sandbox Code Playgroud)

定义映射

curl -XPOST "http://localhost:9200/test_shingling/item/_mapping" -d '{
  "item": {
    "properties": {
      "Title": {
        "type": "string",
        "search_analyzer": "ShingleAnalyzer",
        "index_analyzer": "ShingleAnalyzer"
      }
    }
  }
}'
Run Code Online (Sandbox Code Playgroud)

创建文档

curl -XPOST "http://localhost:9200/test_shingling/item/" -d '{
  "Title":"Kyocera Solar Panel Test"
}'
Run Code Online (Sandbox Code Playgroud)

测试分析仪通过

curl 'localhost:9200/test_shingling/_analyze?pretty=1&analyzer=ShingleAnalyzer' -d 'Kyocera Solar Panel Test'
Run Code Online (Sandbox Code Playgroud)

等待ES同步(也就是刷新索引)

curl -XPOST "http://localhost:9200/test_shingling/_refresh"
Run Code Online (Sandbox Code Playgroud)

搜索"京瓷太阳能电池板测试"失败

curl -XPOST "http://localhost:9200/test_shingling/item/_search?pretty=true" -d '{
  "query": {
    "term": {
      "Title": "Kyocera Solar Panel Test"
    }
  }
}'
Run Code Online (Sandbox Code Playgroud)

搜索"太阳能电池板"失败

curl -XPOST "http://localhost:9200/test_shingling/item/_search?pretty=true" -d '{
  "query": {
    "term": {
      "Title": "Kyocera Solar Panel Test"
    }
  }
}'
Run Code Online (Sandbox Code Playgroud)

搜索"京瓷太阳能电池板测试"失败

curl -XPOST "http://localhost:9200/test_shingling/item/_search?pretty=true" -d '{
  "query": {
    "query_string": {
      "default_field": "Title",
      "query": "Kyocera Solar Panel Test"
    }
  }
}'
Run Code Online (Sandbox Code Playgroud)

搜索"太阳能电池板"失败

curl -XPOST "http://localhost:9200/test_shingling/item/_search?pretty=true" -d '{
  "query": {
    "query_string": {
      "default_field": "Title",
      "query": "solar panel"
    }
  }
}'
Run Code Online (Sandbox Code Playgroud)

Bla*_*POP 5

术语查询将搜索完全匹配,并且不会将ShingleAnalyzer应用于您的查询.

因此,您必须使用匹配查询,这将在搜索时将Analyzer应用于您的查询字符串.

全字搜索

curl -XPOST "http://localhost:9200/test_shingling/item/_search" -d'{
    "query": {
        "match": {
            "Title": "Kyocera Solar Panel Test"
        }
    }
}'
Run Code Online (Sandbox Code Playgroud)

部分单词搜索

curl -XPOST "http://localhost:9200/test_shingling/item/_search" -d'{
    "query": {
        "match": {
            "Title": "Panel Test"
        }
    }
}'
Run Code Online (Sandbox Code Playgroud)

另一个部分词搜索

curl -XPOST "http://localhost:9200/test_shingling/item/_search" -d'{
    "query": {
        "match": {
            "Title": "Solar Panel Test"
        }
    }
}'
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你..!