这是我使用自定义脚本提出的一种解决方案:
PUT _ingest/pipeline/shingle
{
"description" : "Create basic shingles from title field and input in another field title_suggest",
"processors" : [
{
"script": {
"lang": "painless",
"source": """
String[] split(String s, char d) {
int count = 0;
for (char c : s.toCharArray()) {
if (c == d) {
++count;
}
}
if (count == 0) {
return new String[] {s};
}
String[] r = new String[count + 1];
int i0 = 0, i1 = 0;
count = 0;
for (char c : s.toCharArray()) {
if (c == d) {
r[count++] = s.substring(i0, i1);
i0 = i1 + 1;
}
++i1;
}
r[count] = s.substring(i0, i1);
return r;
}
if (!ctx.containsKey('title')) { return; }
def title_words = split(ctx['title'], (char)' ');
def title_suggest = [];
for (def i = 0; i < title_words.length; i++) {
def shingle = title_words[i];
title_suggest.add(shingle);
for (def j = i + 1; j < title_words.length; j++) {
shingle = shingle + ' ' + title_words[j];
title_suggest.add(shingle);
}
}
ctx['title_suggest'] = title_suggest;
"""
}
}
]
}