2016-09-22 64 views
0

我想写一个自定义分析器,它打破特殊字符上的标记并在索引前将其转换为大写,我应该能够得到结果,如果我搜索小写也..自定义分析器打破特殊字符和小写/大写的标记

例如,如果我给数据@源 - 它应该用空白替换@任何特殊的字符,它应该用空白替换,并给我像数据源的结果。

以下是我尝试实施的方法。

PUT sound 
{ 
    "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": "$1 " 
     } 
     } 
    } 
    } 
} 


POST sound/_analyze 
{ 
    "analyzer": "my_analyzer", 
    "text": "data-source&abc" 
} 

它拆分令牌嗯,就像 -

{ 
    "tokens": [ 
     { 
     "token": "DATA", 
     "start_offset": 0, 
     "end_offset": 4, 
     "type": "<ALPHANUM>", 
     "position": 0 
     }, 
     { 
     "token": "SOURCE", 
     "start_offset": 5, 
     "end_offset": 11, 
     "type": "<ALPHANUM>", 
     "position": 1 
     }, 
     { 
     "token": "ABC", 
     "start_offset": 12, 
     "end_offset": 15, 
     "type": "<ALPHANUM>", 
     "position": 2 
     } 
    ] 
} 

但是,如果我在这个以小写甚至大写字母搜索,它不工作..喜欢:

GET sound/_search?text="data" 

GET sound/_search?text="data" 

GET /sound/_search 
{ 
    "query": { 
    "match": { 
     "text": "data" 
    } 
    } 
} 

它没有给我的结果,如果我像上面的查询搜索..

+1

请参考这个答案:http://stackoverflow.com/a/ 39662994/4604579 – Val

回答

0

你只需要使用一些稍微不同的语法您的搜索:

GET sound/_search?q=data 

GET sound/_search?q=data 

POST sound/_search 
{ 
    "query": { 
    "match": { 
     "NAME_OF_YOUR_FIELD": "data" 
    } 
    } 
} 

NAME_OF_YOUR_FIELD需要为你存储在您的数据字段的名称更多的Infor在match query here

+0

嗨Ceilingfish-它仍然不适合我!我尝试了你给出的选项。 –

+0

/Downloads/elasticsearch-2.4.0/bin$ curl -XGET http:// localhost:9200/sound/_search?q ='text' {“took”:8,“timed_out”:false,“_ shards”: {“total”:5,“success”:5,“failed”:0},“hits”:{“total”:0,“max_score”:null,“hits”:[]}}我无法用GET检索结果。 –

+0

curl -XGET'http:// localhost:9200/sound/_search?pretty' - 即使这看起来不工作。 –