Принимая следующее, например, из чата в качестве базы:
Some example titles:
title: Climate: The case of Nigerian agriculture
title: Are you ready to change the climate?
title: A literature review with a particular focus on the school staff
title: What are the effects of direct public transfers on social solidarity?
title: Community-Led Practical and/or Social Support Interventions for Adults Living at Home.
If I search by only "?" then it should return the 2nd and 4th results.
If I search by "/" then it should return only last record.
Search by climate then 1st and 2nd results.
Search by climate? then 1st, 2nd, and 4th results.
Для решения потребуется создать анализаторы для следующих случаев:
- Для поиска особый персонаж. Я рассматриваю их как пунктуацию, например
/
, ?
et c. - Для поиска по ключевому слову и специальному символу. Например,
climate?
- Для поиска по ключевому слову. например,
climate
Для case 1 мы будем использовать pattern tokenizer , но вместо использования pattern для разделения мы будем использовать pattern для извлечения специальных символов в качестве токенов, и для этого мы устанавливаем "group": 0
при определении токенизатора. например, для текста xyz a/b pq?
сгенерированные токены будут /
, ?
Для case 2 мы создадим собственный анализатор с filter
как lowercase
(для создания case нечувствителен) и tokenizer
как whitespace
(для сохранения специальных символов с ключевыми словами). например, для текста How many?
сгенерированные токены будут how
, many?
Для case 3 мы будем использовать standard
анализатор, который является анализатором по умолчанию.
Следующим шагом будет создание подполей для title
. title
будет иметь тип text
и по умолчанию будет иметь standard
анализатор. Это свойство отображения будет иметь два подполя withSplChar
типа text
и анализатор, созданный для case 2 (ci_whitespace
), splChars
типа text
и анализатор, созданный для case 1 (splchar
)
Давайте теперь посмотрим на вышесказанное в действии:
PUT test
{
"settings": {
"analysis": {
"tokenizer": {
"splchar": {
"type": "pattern",
"pattern": "\\p{Punct}",
"group": 0
}
},
"analyzer": {
"splchar": {
"tokenizer": "splchar"
},
"ci_whitespace": {
"type": "custom",
"filter": [
"lowercase"
],
"tokenizer": "whitespace"
}
}
}
},
"mappings": {
"properties": {
"title": {
"type": "text",
"fields": {
"withSplChar": {
"type": "text",
"analyzer": "ci_whitespace"
},
"splChars": {
"type": "text",
"analyzer": "splchar"
}
}
}
}
}
}
Давайте теперь индексируем документы, как в примере выше:
POST test/_bulk
{"index":{"_id":"1"}}
{"title":"Climate: The case of Nigerian agriculture"}
{"index":{"_id":"2"}}
{"title":"Are you ready to change the climate?"}
{"index":{"_id":"3"}}
{"title":"A literature review with a particular focus on the school staff"}
{"index":{"_id":"4"}}
{"title":"What are the effects of direct public transfers on social solidarity?"}
{"index":{"_id":"5"}}
{"title":"Community-Led Practical and/or Social Support Interventions for Adults Living at Home."}
Поиск ?
"hits" : [
{
"_index" : "test",
"_type" : "_doc",
"_id" : "2",
"_score" : 0.8025915,
"_source" : {
"title" : "Are you ready to change the climate?"
}
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "4",
"_score" : 0.8025915,
"_source" : {
"title" : "What are the effects of direct public transfers on social solidarity?"
}
}
]
Результат:
"hits" : [
{
"_index" : "test",
"_type" : "_doc",
"_id" : "2",
"_score" : 0.8025915,
"_source" : {
"title" : "Are you ready to change the climate?"
}
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "4",
"_score" : 0.8025915,
"_source" : {
"title" : "What are the effects of direct public transfers on social solidarity?"
}
}
]
Поиск climate
POST test/_search
{
"query": {
"query_string": {
"query": "climate",
"fields": ["title", "title.withSplChar", "title.splChars"]
}
}
}
Результат :
"hits" : [
{
"_index" : "test",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0341107,
"_source" : {
"title" : "Climate: The case of Nigerian agriculture"
}
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "2",
"_score" : 0.98455274,
"_source" : {
"title" : "Are you ready to change the climate?"
}
}
]
Поиск climate?
POST test/_search
{
"query": {
"query_string": {
"query": "climate\\?",
"fields": ["title", "title.withSplChar", "title.splChars"]
}
}
}
Результат:
"hits" : [
{
"_index" : "test",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.5366155,
"_source" : {
"title" : "Are you ready to change the climate?"
}
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0341107,
"_source" : {
"title" : "Climate: The case of Nigerian agriculture"
}
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "4",
"_score" : 0.8025915,
"_source" : {
"title" : "What are the effects of direct public transfers on social solidarity?"
}
}
]