如何使用Elasticsearh解析字符串

时间:2018-11-20 16:42:17

标签: elasticsearch

我想使用ElasticSearch来分隔字符串中的标记,说我有一个字符串,我想从中提取名称:

John Smith had a little lamb

我的想法是创建它的多个版本,在名称索引中搜索它们,然后获得得分最高的版本:

John
John Smith
John Smith had
....

用ElasticSearch做到这一点的最佳方法是什么?

编辑: 我想要这样的东西:

//this combination is not right, gives me a low score
{
  "query": {
    "bool" : {
      "should" : [
        { "match" : { "name" : "John" } },
      ],
    }
  }
}
//this combination is right, gives me a high score
{
  "query": {
    "bool" : {
      "should" : [
        { "match" : { "name" : "John" } },
        { "match" : { "name" : "Smith" } },
      ],
    }
  }
}
//this one also gives me a low score, stop searching here
{
  "query": {
    "bool" : {
      "should" : [
        { "match" : { "name" : "John" } },
        { "match" : { "name" : "Smith" } },
        { "match" : { "name" : "had" } },
      ],
    }
  }
}

我如何仅用一个查询就能完成此过程?

1 个答案:

答案 0 :(得分:0)

我不太清楚您将如何从此文档中提取名称。当然,elasticsearch不能做的事情并且将需要来自某些逻辑…… 名称是否总是以大写字母开头的单词分类?如果是这样,请使用空格分析器,因为英语分析器将小写并阻止这些术语

关于分数,我不清楚这对您有什么帮助……分数最高的是最不常用的术语-羔羊在您的索引中可能不如约翰或史密斯一词那么常见,但“小”可能更常见,所以我不知道这将如何帮助您知道名字叫“约翰·史密斯” 还是说您正在寻找两个标记/单词的集合...

也许这里的问题是围绕澄清您想要实现的目标,然后您可以检查ES如何做到这一点

相关问题