Elasticsearch edge_ngram token_chars空格

时间:2019-01-08 10:39:03

标签: elasticsearch token n-gram

有人可以告诉我Elasticsearch edge_ngram token_chars空格吗? Token_chars应该定义将包含在令牌中的符号。因此,如果我使用token_chars: ['letter', 'digit', 'whitespace'],那么在短语“两只赤狐”的含义是什么?据我了解,它将以这种方式产生令牌 ['2','2','2r','2re','2red'] 。我对标记没有在空格上分割,但是标记中会包含空格吗? 谢谢。

1 个答案:

答案 0 :(得分:1)

总的来说,您是对的,但这还取决于min_grammax_gram参数值。

在下面看看。

示例1。

min_gram = 2max_gram = 10

映射

PUT /so54090009
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "my_tokenizer"
        }
      },
      "tokenizer": {
        "my_tokenizer": {
          "type": "edge_ngram",
          "min_gram": 2,
          "max_gram": 10,
          "token_chars": [
            "letter",
            "digit",
            "whitespace"
          ]
        }
      }
    }
  }
}

分析

POST /so54090009/_analyze
{
  "analyzer": "my_analyzer",
  "text": "2 red foxes"
}

响应

{
  "tokens": [
    {
      "token": "2 ",
      "start_offset": 0,
      "end_offset": 2,
      "type": "word",
      "position": 0
    },
    {
      "token": "2 r",
      "start_offset": 0,
      "end_offset": 3,
      "type": "word",
      "position": 1
    },
    {
      "token": "2 re",
      "start_offset": 0,
      "end_offset": 4,
      "type": "word",
      "position": 2
    },
    {
      "token": "2 red",
      "start_offset": 0,
      "end_offset": 5,
      "type": "word",
      "position": 3
    },
    {
      "token": "2 red ",
      "start_offset": 0,
      "end_offset": 6,
      "type": "word",
      "position": 4
    },
    {
      "token": "2 red f",
      "start_offset": 0,
      "end_offset": 7,
      "type": "word",
      "position": 5
    },
    {
      "token": "2 red fo",
      "start_offset": 0,
      "end_offset": 8,
      "type": "word",
      "position": 6
    },
    {
      "token": "2 red fox",
      "start_offset": 0,
      "end_offset": 9,
      "type": "word",
      "position": 7
    },
    {
      "token": "2 red foxe",
      "start_offset": 0,
      "end_offset": 10,
      "type": "word",
      "position": 8
    }
  ]
}

示例2。

min_gram = 1max_gram = 5

映射

PUT /so54090009
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "my_tokenizer"
        }
      },
      "tokenizer": {
        "my_tokenizer": {
          "type": "edge_ngram",
          "min_gram": 1,
          "max_gram": 5,
          "token_chars": [
            "letter",
            "digit",
            "whitespace"
          ]
        }
      }
    }
  }
}

分析

POST /so54090009/_analyze
{
  "analyzer": "my_analyzer",
  "text": "2 red foxes"
}

响应

{
  "tokens": [
    {
      "token": "2",
      "start_offset": 0,
      "end_offset": 1,
      "type": "word",
      "position": 0
    },
    {
      "token": "2 ",
      "start_offset": 0,
      "end_offset": 2,
      "type": "word",
      "position": 1
    },
    {
      "token": "2 r",
      "start_offset": 0,
      "end_offset": 3,
      "type": "word",
      "position": 2
    },
    {
      "token": "2 re",
      "start_offset": 0,
      "end_offset": 4,
      "type": "word",
      "position": 3
    },
    {
      "token": "2 red",
      "start_offset": 0,
      "end_offset": 5,
      "type": "word",
      "position": 4
    }
  ]
}