【问题标题】:Searching English Text in HTML Using Elasticsearch使用 Elasticsearch 在 HTML 中搜索英文文本
【发布时间】:2018-04-05 10:01:12
【问题描述】:

我正在尝试使用 Elasticsearch 为英语语言的 HTML 文档编制索引。数据采用原始 HTML 格式。我找到了一个过滤 HTML 标记的设置,但我不能将此过滤器 英文分析器一起使用。

我希望此设置返回三个标记,但它返回五个标记,因为它将“html”视为标记两次

POST _analyze
{
  "analyzer": "english", 
  "char_filter": ["html_strip"], 
  "text": "<html>It will be raining in yosemite this weekend</html>"
}

我怎样才能只为上面的文本获取三个标记(无 HTML 标记),这样我的返回结果如下所示?

{
  "tokens": [
    {
      "token": "rain",
      "start_offset": 11,
      "end_offset": 18,
      "type": "<ALPHANUM>",
      "position": 3
    },
    {
      "token": "yosemit",
      "start_offset": 22,
      "end_offset": 30,
      "type": "<ALPHANUM>",
      "position": 5
    },
    {
      "token": "weekend",
      "start_offset": 36,
      "end_offset": 43,
      "type": "<ALPHANUM>",
      "position": 7
    }
  ]
}

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    定义一个自定义分析器,仅使用英语分析器作为基本模板,并向其中添加 html 条形过滤器。

    PUT /english_with_html_strip
    {
      "settings": {
        "analysis": {
          "filter": {
            "english_stop": {
              "type":       "stop",
              "stopwords":  "_english_" 
            },
            "english_keywords": {
              "type":       "keyword_marker",
              "keywords":   ["example"] 
            },
            "english_stemmer": {
              "type":       "stemmer",
              "language":   "english"
            },
            "english_possessive_stemmer": {
              "type":       "stemmer",
              "language":   "possessive_english"
            }
          },
          "analyzer": {
            "english_with_html_strip": {
              "tokenizer":  "standard",
              "char_filter": ["html_strip"],
              "filter": [
                "english_possessive_stemmer",
                "lowercase",
                "english_stop",
                "english_keywords",
                "english_stemmer"
              ]
            }
          }
        }
      }
    }
    

    那你就可以了

    POST /english_with_html_strip/_analyze
    {
      "analyzer": "english_with_html_strip", 
      "text": "<html>It will be raining in yosemite this weekend</html>"
    }
    

    这是假设您想使用英语分析器来分析文本。如果你只是想要它标记化剥离 html,你可以这样做

    POST _analyze
        {
          "tokenizer":      "standard", 
          "char_filter":  [ "html_strip" ],
          "text": "<html>It will be raining in yosemite this weekend</html>"
        }
    

    【讨论】:

      猜你喜欢
      • 2018-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-23
      • 2012-10-22
      • 2014-06-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多