0

有没有可以做带状疱疹的处理器,或者我可以以某种方式定制一个?

在下面的管道处理器中,我拆分了空格字符,但我也想组合单词,比如 shingle 分析器会:

PUT _ingest/pipeline/split
{
  "processors": [
    {
      "split": {
        "field": "title",
        "target_field": "title_suggest.input",
        "separator": "\\s+"
      }
    }
  ]
}

例子:

“高级业务开发人员”需要包含这些术语的建议字段。

  1. 高级业务开发人员
  2. 企业发展者
  3. 开发商

以下是启发这个问题的文章和答案的链接:

  1. https://blog.mimacom.com/autocomplete-elasticsearch-part3/
  2. 如何跨多个文本字段组合完成、建议和匹配短语?
4

1 回答 1

0

这是我使用自定义脚本提出的一种解决方案:

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;
              
            """
      }
    }
  ]
}
于 2021-03-18T19:13:15.260 回答