elasticsearch多词关键词 - 标记化同义词分析

时间:2014-08-08 15:38:25

标签: elasticsearch synonym

我正在尝试使用_analyze API获取关键字标记化的多字同义词。 API返回单字同义词的预期结果,但不是多字的同义词。这是我的设置和分析链:

curl -XPOST "http://localhost:9200/test" -d'
{
  "settings": {
    "index": {
      "analysis": {
        "filter": {
          "my_syn_filt": {
            "type": "synonym",
            "synonyms": [
              "foo bar, fooo bar", 
              "bazzz, baz"
            ]
          }
        },
        "analyzer": {
          "my_synonyms": {
            "filter": [
              "lowercase",
              "my_syn_filt"
            ],
            "tokenizer": "keyword"
          }
        }
      }
    }
  }
}'

现在使用_analyze API进行测试:

curl 'localhost:9200/test/_analyze?analyzer=my_synonyms&text=baz'

调用返回我期望的内容(同样为'bazzz'返回相同的结果):

{
  "tokens": [
    {
      "position": 1,
      "type": "SYNONYM",
      "end_offset": 3,
      "start_offset": 0,
      "token": "bazzz"
    },
    {
      "position": 1,
      "type": "SYNONYM",
      "end_offset": 3,
      "start_offset": 0,
      "token": "baz"
    }
  ]
}

现在当我尝试使用多字同义词文本进行相同的调用时,API只返回一个类型为'word'的标记,没有同义词:

curl 'localhost:9200/test/_analyze?analyzer=my_synonyms&text=foo+bar'

(返回)

{
  "tokens": [
    {
      "position": 1,
      "type": "word",
      "end_offset": 7,
      "start_offset": 0,
      "token": "foo bar"
    }
  ]
}

为什么分析API不会返回类型为SYNONYM的“foo bar”和“fooo bar”令牌?

1 个答案:

答案 0 :(得分:13)

" tokenizer":"关键字"键值ALSO需要添加到my_syn_filt过滤器声明中,如下所示:

curl -XPOST "http://localhost:9200/test" -d'
{
  "settings": {
    "index": {
      "analysis": {
        "filter": {
          "my_syn_filt": {
            "tokenizer": "keyword",
            "type": "synonym",
            "synonyms": [
              "foo bar, fooo bar", 
              "bazzz, baz"
            ]
          }
        },
        "analyzer": {
          "my_synonyms": {
            "filter": [
              "lowercase",
              "my_syn_filt"
            ],
            "tokenizer": "keyword"
          }
        }
      }
    }
  }
}'

通过上面的映射,_analyze API返回所需的SYNONYM标记:

{
  "tokens": [
    {
      "position": 1,
      "type": "SYNONYM",
      "end_offset": 7,
      "start_offset": 0,
      "token": "foo bar"
    },
    {
      "position": 1,
      "type": "SYNONYM",
      "end_offset": 7,
      "start_offset": 0,
      "token": "fooo bar"
    }
  ]
}