搜索弹性搜索中的名称和特殊字符

时间:2017-10-12 07:22:35

标签: elasticsearch

我在某些名字中搜索名称,它有特殊字符(&,(),')。     我的索引映射看起来像这样

{
    "settings": {
        "index": {
            "analysis": {
                "analyzer": {
                    "analyzer_startswith": {
                        "tokenizer": "keyword",
                        "filter": "lowercase"
                    }
                }
            }
        }
    },

  "mappings": {
        "doc": {
            "properties": {
                "namefeild": {
                    "search_analyzer": "analyzer_startswith",
                    "analyzer": "analyzer_startswith",
                    "type": "string"
                }
            }
        }
    }

我想搜索名称'你&我'。

{"query":{"bool":{"must":[{"match_phrase_prefix":{"namefeild":"you & me"}}}]}},"from":0,"size":100}

这时我搜索姓名直到'你&'我得到结果后,我得到结果null。     请帮帮我。

1 个答案:

答案 0 :(得分:0)

对我而言,#34;你和"和"你&我"

如果这仍然不适合您,请发布返回null结果的确切查询

PUT sf
{
    "settings": {
        "index": {
            "analysis": {
                "analyzer": {
                    "analyzer_startswith": {
                        "tokenizer": "keyword",
                        "filter": "lowercase"
                    }
                }
            }
        }
    },

  "mappings": {
        "doc": {
            "properties": {
                "namefeild": {
                    "search_analyzer": "analyzer_startswith",
                    "analyzer": "analyzer_startswith",
                    "type": "string"
                }
            }
        }
    }

}


POST sf/_analyze
{
  "analyzer": "analyzer_startswith",
  "text":     "you &"
}


POST _bulk
{ "index" : { "_index" : "sf", "_type" : "doc", "_id" : "1" } }
{"namefeild": "you" }
{ "index" : { "_index" : "sf", "_type" : "doc", "_id" : "2" } }
{"namefeild": "you & me" }


GET sf/_search
{
    "query": {
        "bool": {
            "must": [{
                "match_phrase_prefix": {
                    "namefeild": "you & me"
                }
            }
        ]}
    }