elasticsearch 映射分析器 - GET 没有得到结果

elasticsearch mapping analyzer - GET not getting result

我正在尝试创建一个分析器,它将特殊字符替换为空格并将其转换为大写。然后,如果我想用小写字母搜索,它也应该可以工作。

映射分析器:

soundarya@soundarya-VirtualBox:~/Downloads/elasticsearch-2.4.0/bin$ curl -XPUT 'http://localhost:9200/aida' -d '{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "standard",
          "char_filter": [
            "my_char_filter"
          ],
          "filter": [
            "uppercase"
            ]
        }
      },
      "char_filter": {
        "my_char_filter": {
          "type": "pattern_replace",
          "pattern": "(\d+)-(?=\d)",
          "replacement": " "
        }
      }
    }
  }
}
'
{"acknowledged":true}


soundarya@soundarya-VirtualBox:~/Downloads/elasticsearch-2.4.0/bin$ curl -XPOST 'http://localhost:9200/aida/_analyze?pretty' -d '{
"analyzer":"my_analyzer",
"text":"My name is Soun*arya?jwnne&yuuk"
}'

它通过用空格替换特殊字符来正确标记单词。现在,如果我从文本中搜索一个词,它不会检索到任何结果。

soundarya@soundarya-VirtualBox:~/Downloads/elasticsearch-2.4.0/bin$ curl -XGET 'http://localhost:9200/aida/_search' -d '{
"query":{
"match":{
"text":"My"
}
}
}'

我没有从上面的 GET 查询中得到任何结果。得到这样的结果:

soundarya@soundarya-VirtualBox:~/Downloads/elasticsearch-2.4.0/bin$ curl -XGET 'http://localhost:9200/aida/_search' -d '{
"query":{
"match":{
"text":"my"
}
}
}'
{"took":5,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":0,"max_score":null,"hits":[]}}

谁能帮我解决这个问题!谢谢!

您似乎在创建索引后没有为任何数据编制索引。对 _analyze 的调用不会索引任何内容,只会向您展示如何分析您发送到 ES 的内容。

首先,您需要通过指定一个映射来创建索引,您在其中使用您定义的分析器:

curl -XPUT 'http://localhost:9200/aida' -d '{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "standard",
          "char_filter": [
            "my_char_filter"
          ],
          "filter": [
            "uppercase"
            ]
        }
      },
      "char_filter": {
        "my_char_filter": {
          "type": "pattern_replace",
          "pattern": "(\d+)-(?=\d)",
          "replacement": " "
        }
      }
    }
  },
  "mappings": {                        <--- add a mapping type...
    "doc": {
      "properties": {
        "text": {                      <--- ...with a field...
          "type": "string",
          "analyzer": "my_analyzer"    <--- ...using your analyzer
        }
      }
    }
  }
}'

然后你可以索引一个新的真实文档:

curl -XPOST 'http://localhost:9200/aida/doc' -d '{
   "text": "My name is Soun*arya?jwnne&yuuk"
}'

最后可以搜索:

curl -XGET 'http://localhost:9200/aida/_search' -d '{
  "query":{
    "match":{
      "text":"My"
    }
  }
}'