Удалять символы из текста во время токенизации в поиске elasti c - PullRequest
0 голосов
/ 23 апреля 2020

Я пытаюсь создать elasti c поисковый документ, который токенизирует данные определенным образом. Для такой строки, как Los Angeles (and vicinity), California, United States of America, я хочу, чтобы такие символы, как ( ) , et c, были исключены и включались только буквенно-цифровые символы.

https://www.elastic.co/guide/en/elasticsearch/reference/7.x/analysis-keep-types-tokenfilter.html#analysis -keep-types-tokenfilter-customize

Мои настройки индекса ES выглядят следующим образом

PUT /test-index
{
  "settings": {
    "analysis": {
      "filter": {
        "extract_alpha": {
          "type": "keep_types",
          "mode": "include",
          "types": [
            "<ALPHANUM>"
          ]
        }
      },
      "analyzer": {
        "my_autocomplete": { 
          "type":"custom",
          "tokenizer":"my_tokenizer",
          "filter" : [
            "lowercase",
            "extract_alpha"
          ]
        }
      },
      "tokenizer": {
        "my_tokenizer" : {
          "type": "whitespace"
        }
      }
    }
   },
   "mappings": {
      "properties": {
        "name": {
          "type": "text",
          "analyzer": "my_autocomplete"
        }
      } 
  }
}

Однако при запуске этот запрос

GET test-index/_analyze
{
  "analyzer": "my_autocomplete",
  "text": "Los Angeles (and vicinity), California, United States of America"
}

Я получаю вывод

{
  "tokens" : [ ]
}

Если я удаляю фильтр extract_alpha, я получаю токены, но с включенными символами

{
  "tokens" : [
    {
      "token" : "los",
      "start_offset" : 0,
      "end_offset" : 3,
      "type" : "word",
      "position" : 0
    },
    {
      "token" : "angeles",
      "start_offset" : 4,
      "end_offset" : 11,
      "type" : "word",
      "position" : 1
    },
    {
      "token" : "(and",
      "start_offset" : 12,
      "end_offset" : 16,
      "type" : "word",
      "position" : 2
    },
    {
      "token" : "vicinity),",
      "start_offset" : 17,
      "end_offset" : 27,
      "type" : "word",
      "position" : 3
    },
    {
      "token" : "california,",
      "start_offset" : 28,
      "end_offset" : 39,
      "type" : "word",
      "position" : 4
    },
    {
      "token" : "united",
      "start_offset" : 40,
      "end_offset" : 46,
      "type" : "word",
      "position" : 5
    },
    {
      "token" : "states",
      "start_offset" : 47,
      "end_offset" : 53,
      "type" : "word",
      "position" : 6
    },
    {
      "token" : "of",
      "start_offset" : 54,
      "end_offset" : 56,
      "type" : "word",
      "position" : 7
    },
    {
      "token" : "america",
      "start_offset" : 57,
      "end_offset" : 64,
      "type" : "word",
      "position" : 8
    }
  ]
}

Как мне это исправить, что я делаю не так?

1 Ответ

1 голос
/ 23 апреля 2020

Я не знаю, почему вы создаете пользовательский анализатор, об этом заботится стандартный анализатор по умолчанию , как показано ниже:

Запрос анализа

POST http://{{hostname}}:{{port}}/{{index-name}}/_analyze
{
    "text" : "Los Angeles (and vicinity), California, United States of America",
    "analyzer" : "standard"
}

И результирующие токены

{
    "tokens": [
        {
            "token": "los",
            "start_offset": 0,
            "end_offset": 3,
            "type": "<ALPHANUM>",
            "position": 0
        },
        {
            "token": "angeles",
            "start_offset": 4,
            "end_offset": 11,
            "type": "<ALPHANUM>",
            "position": 1
        },
        {
            "token": "and",
            "start_offset": 13,
            "end_offset": 16,
            "type": "<ALPHANUM>",
            "position": 2
        },
        {
            "token": "vicinity",
            "start_offset": 17,
            "end_offset": 25,
            "type": "<ALPHANUM>",
            "position": 3
        },
        {
            "token": "california",
            "start_offset": 28,
            "end_offset": 38,
            "type": "<ALPHANUM>",
            "position": 4
        },
        {
            "token": "united",
            "start_offset": 40,
            "end_offset": 46,
            "type": "<ALPHANUM>",
            "position": 5
        },
        {
            "token": "states",
            "start_offset": 47,
            "end_offset": 53,
            "type": "<ALPHANUM>",
            "position": 6
        },
        {
            "token": "of",
            "start_offset": 54,
            "end_offset": 56,
            "type": "<ALPHANUM>",
            "position": 7
        },
        {
            "token": "america",
            "start_offset": 57,
            "end_offset": 64,
            "type": "<ALPHANUM>",
            "position": 8
        }
    ]
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...