2015-02-11 67 views
2

我是elasticsearch的新手,并且正在测试html_strip过滤器。理想情况下,我不应该搜索HTML标签。以下是步骤。为什么HTML标签可以搜索,即使它被弹性搜索过滤

指数:

curl -XPOST 'localhost:9200/foo/test/_analyzer?tokenizer=standard&char_filters=html_strip' -d ' 
{ 
    "content" : "<title>Dilip Kumar</title>" 
}' 

搜索:

http://localhost:9200/foo/test/_search?tokenizer=standard&char_filters=html_strip&q=title 

结果:

{ 
    "took": 3, 
    "timed_out": false, 
    "_shards": { 
    "total": 5, 
    "successful": 5, 
    "failed": 0 
    }, 
    "hits": { 
    "total": 1, 
    "max_score": 0.2169777, 
    "hits": [ 
     { 
     "_index": "foo", 
     "_type": "test", 
     "_id": "_analyzer", 
     "_score": 0.2169777, 
     "_source": { 
      "content": "<title>Dilip Kumar</title>" 
     } 
     } 
    ] 
    } 
} 

UPDATE 作为建议;我使用下面的映射并在删除现有索引后重复上述步骤,但仍然可以搜索标记。

curl -XPUT "http://localhost:9200/foo " -d' 
{ 
    "foo": { 
    "settings": { 
     "analysis": { 
     "analyzer": { 
      "html_analyzer": { 
      "type": "custom", 
      "tokenizer": "standard", 
      "filter": [ 
       "standard", 
       "lowercase", 
       "stop", 
       "asciifolding" 
      ], 
      "char_filter": [ 
       "html_strip" 
      ] 
      }, 
      "whitespace_analyzer": { 
      "type": "custom", 
      "tokenizer": "whitespace", 
      "filter": [ 
       "standard", 
       "lowercase", 
       "stop", 
       "asciifolding" 
      ] 
      } 
     } 
     } 
    }, 
    "mappings": { 
     "test": { 
     "properties": { 
      "content": { 
      "type": "string", 
      "index_analyzer": "html_analyzer", 
      "search_analyzer": "whitespace_analyzer" 
      } 
     } 
     } 
    } 
    } 
}' 

回答

0

您需要在对映射进行索引之前应用分析器。 这将确保索引的所有文档都通过此映射,并且在编制索引之前将所有标记除去。 就你而言,你在查询时应用分析器,这只会影响你的搜索短语,而不会影响你搜索的数据。

你可以阅读更多关于创建映射here

我不相信有格式像这样 -

http://localhost:9200/foo/test/_search?tokenizer=standard&char_filters=html_strip&q=title 

相反,如果你可以设置分析如下,应该很好地工作 -

curl -XPUT "http://localhost:9200/foo " -d' 
{ 
    "foo": { 
    "settings": { 
     "analysis": { 
     "analyzer": { 
      "html_analyzer": { 
      "type": "custom", 
      "tokenizer": "standard", 
      "filter": [ 
       "standard", 
       "lowercase", 
       "stop", 
       "asciifolding" 
      ], 
      "char_filter": [ 
       "html_strip" 
      ] 
      }, 
      "whitespace_analyzer": { 
      "type": "custom", 
      "tokenizer": "whitespace", 
      "filter": [ 
       "standard", 
       "lowercase", 
       "stop", 
       "asciifolding" 
      ] 
      } 
     } 
     } 
    }, 
    "mappings": { 
     "test": { 
     "properties": { 
      "content": { 
      "type": "string", 
      "analyzer": "html_analyzer" 
      } 
     } 
     } 
    } 
    } 
}' 

在这里,我使分析仪通用索引和搜索

+0

我没有得到你。我也在索引上应用了char_filters = html_strip。我是elasticsearch的新手......无法弄清楚你的观察结果。同时我正在阅读如何根据您分享的文档创建映射。 – joy 2015-02-11 08:17:29

+0

我使用了映射,但仍然是相同的问题。更新后添加映射详细信息。敬请期待。 – joy 2015-02-11 08:39:03

+0

我已经更新了答案 – 2015-02-11 10:41:59