【问题标题】:Analyzers in ElasticSearch not workingElasticSearch 中的分析器不起作用
【发布时间】:2014-04-14 11:09:02
【问题描述】:

我正在使用 ElasticSearch 来存储我从 Twitter 流 API 收到的推文。在存储它们之前,我想对推文内容应用英语词干分析器,为此我尝试使用 ElasticSearch 分析器,但没有成功。

这是我正在使用的当前模板:

PUT _template/twitter
{
  "template": "139*",
  "settings" : {
    "index":{
      "analysis":{
        "analyzer":{
          "english":{
            "type":"custom",
            "tokenizer":"standard",
            "filter":["lowercase", "en_stemmer", "stop_english", "asciifolding"]
          }
        },
        "filter":{
          "stop_english":{
            "type":"stop",
            "stopwords":["_english_"]
          },
          "en_stemmer" : {
            "type" : "stemmer",
            "name" : "english"
          }
        }
      }
    }
  },
  "mappings": {
    "tweet": {
      "_timestamp": {
        "enabled": true,
        "store": true,
        "index": "analyzed"
      },
      "_index": {
        "enabled": true,
        "store": true,
        "index": "analyzed"
      },
      "properties": {
        "geo": {
          "properties": {
            "coordinates": {
              "type": "geo_point"
            }
          }
        },
        "text": {
          "type": "string",
          "analyzer": "english"
        }
      }
    }
  }
}

当我启动 Streaming 并创建索引时,我定义的所有映射似乎都正确应用,但文本存储为来自 Twitter,完全原始。索引元数据显示:

"settings" : {
    "index" : {
        "uuid" : "xIOkEcoySAeZORr7pJeTNg",
        "analysis" : {
            "filter" : {
                "en_stemmer" : {
                    "type" : "stemmer",
                    "name" : "english"
                 },
                 "stop_english" : {
                     "type" : "stop",
                     "stopwords" : [
                         "_english_"
                     ]
                 }
             },
             "analyzer" : {
                 "english" : {
                     "type" : "custom",
                     "filter" : [
                         "lowercase",
                         "en_stemmer",
                         "stop_english",
                         "asciifolding"
                     ],
                     "tokenizer" : "standard"
                 }
             }
         },
        "number_of_replicas" : "1",
        "number_of_shards" : "5",
        "version" : {
            "created" : "1010099"
        }
    }
},
"mappings" : {
    "tweet" : {
        [...]
        "text" : {
            "analyzer" : "english",
            "type" : "string"
        },
        [...]
    }
}

我做错了什么?分析器似乎已正确应用,但没有发生任何事情:/

谢谢!

PS:我用来实现分析器的搜索查询没有被应用:

curl -XGET 'http://localhost:9200/_all/_search?pretty' -d '{
  "query": {
    "filtered": {
      "query": {
        "bool": {
          "should": [
            {
              "query_string": {
                "query": "_index:1397574496990"
              }
            }
          ]
        }
      },
      "filter": {
        "bool": {
          "must": [
            {
              "match_all": {}
            },
            {
              "exists": {
                "field": "geo.coordinates"
              }
            }
          ]
        }
      }
    }
  },
  "fields": [
    "geo.coordinates",
    "text"
  ],
  "size": 50000
}'

这应该将词干文本作为字段之一返回,但响应是:

{
   "took": 29,
   "timed_out": false,
   "_shards": {
      "total": 47,
      "successful": 47,
      "failed": 0
   },
   "hits": {
      "total": 2,
      "max_score": 0.97402453,
      "hits": [
         {
            "_index": "1397574496990",
            "_type": "tweet",
            "_id": "456086643423068161",
            "_score": 0.97402453,
            "fields": {
               "geo.coordinates": [
                  -118.21122533,
                  33.79349318
               ],
               "text": [
                  "Happy turtle Tuesday ! The week is slowly crawling to Wednesday good morning everyone ????????????????☀️#turtles… http://t.co/wAVmcxnf76"
               ]
            }
         },
         {
            "_index": "1397574496990",
            "_type": "tweet",
            "_id": "456086701451259904",
            "_score": 0.97333175,
            "fields": {
               "geo.coordinates": [
                  -81.017636,
                  33.998741
               ],
               "text": [
                  "Tuesday is Twins Day over here, apparently (it's a far too often occurrence) #tuesdaytwinsday… http://t.co/Umhtp6SoX6"
               ]
            }
         }
      ]
   }
}

文本字段与来自 Twitter 的完全相同(我使用的是流式 API)。我期望的是在应用分析器时提取的文本字段。

【问题讨论】:

  • “什么都没有发生”是什么意思?分析器不会影响数据的存储方式。它们只影响数据的索引方式。您是否尝试在分析的字段上进行搜索以查看词干提取是否有效?您是否尝试使用analyze 方法来查看您的分析器是否被应用?
  • 我的自定义分析器的分析方法有效,但是当我尝试使用 GET 查询检索“文本”字段时,分析器没有被应用,所以我做错了:/
  • 您能否添加一个您尝试搜索的数据和无效的搜索查询的示例?
  • 我添加了我正在使用的搜索查询和我获得的响应。至于数据,是从 Twitter Streaming API 得到的响应,所以比较大。关键是:“文本”字段没有被阻止,即使映射看起来也很好。谢谢你!

标签: elasticsearch analyzer


【解决方案1】:

分析器不会影响数据的存储方式。因此,无论您使用哪种分析器,您都将从源字段和存储字段中获得相同的文本。搜索时应用分析器。因此,通过搜索 text:twin 之类的内容并查找带有单词 Twins 的记录,您就会知道应用了词干分析器。

【讨论】:

    猜你喜欢
    • 2019-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-12
    • 1970-01-01
    • 1970-01-01
    • 2012-10-01
    • 1970-01-01
    相关资源
    最近更新 更多