【问题标题】:Create list of custom stop words in elastic search using java使用 java 在弹性搜索中创建自定义停用词列表
【发布时间】:2015-10-16 00:51:57
【问题描述】:

为了增强从弹性搜索获得的搜索结果,我想从我的 java 代码中增加我的停用词库。到现在为止,我使用的是停止分析器的默认列表,它在列表中没有疑问词,例如 What、Who、Why 等。我们希望在查询结果时从搜索中删除这些词和一些额外的词。 我已经尝试过这里的代码(最后一个答案)tried

PUT /my_index
{
"settings": {
"analysis": {
  "analyzer": {
    "my_analyzer": { 
      "type": "standard", 
      "stopwords": [ "and", "the" ] 
    }
  }
}

} }

这段代码在java中。 但它对我不起作用。 重要查询

如何创建我们自己的停用词列表以及如何在我们的代码中使用查询实现它

QueryStringQueryBuilder qb=new QueryStringQueryBuilder(text).analyzer("stop");
            qb.field("question_title");
            qb.field("level");
            qb.field("category");
            qb.field("question_tags");
            SearchResponse response = client.prepareSearch("questionindex")
            .setSearchType(SearchType.QUERY_AND_FETCH)
            .setQuery(qb)
            .execute()
            .actionGet();
            SearchHit[] results = response.getHits().getHits();
            System.out.println("respose-"+results.length);

目前我正在使用默认停止分析器。这只是停止有限的停用词,如

“a”、“an”、“and”、“are”、“as”、“at”、“be”、“but”、“by”、 “对于”、“如果”、“在”、“进入”、“是”、“它”、 “不”、“不”、“的”、“在”、“或”、“这样”、 “那个”、“那个”、“他们的”、“那么”、“那里”、“这些”、 “他们”、“这个”、“到”、“曾经”、“将”、“与”

但是我想增加这个库。

【问题讨论】:

  • 您需要添加 StopFilter 以添加自定义停用词。你能分享你到目前为止开发的java代码吗?

标签: java elasticsearch


【解决方案1】:

你在正确的轨道上。在您的第一个列表 (from the documentation about stopwords) 中,您为名为 my_index 的索引创建了一个名为 my_analyzer 的自定义分析器,这将具有删除 "and""the" 来自您使用 my_analyzer 的文本。

现在要实际使用它,您应该:

  1. 确保您已在要查询的索引上定义了my_analyzer (questionindex?)
  2. 为您的文档创建一个映射,将my_analyzer 用于您要删除"and""the" 的字段(例如@987654328 @字段):
  3. 使用分析 API 测试您的分析器

    GET /questionindex/_analyze?field=question.question_title&text=No quick brown fox jumps over my lazy dog and the indolent cat

  4. 重新索引您的文档


试试这个作为起点:

POST /questionindex
{
    "settings" : {
        "analysis": {
            "analyzer": {
                "my_analyzer": { 
                    "type": "standard", 
                    "stopwords": [ "and", "the" ] 
                }
            }
        }
    },
    "mappings" : {
        "question" : {
            "properties" : {
                "question_title" : { 
                    "type" : "string", 
                    "analyzer" : "my_analyzer" 
                },
                "level" : { 
                    "type" : "integer" 
                },
                "category" : { 
                    "type" : "string" 
                },
                "question_tags" : { 
                    "type" : "string" 
                }
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-04
    • 1970-01-01
    • 2017-08-01
    • 2011-02-01
    • 1970-01-01
    • 2019-06-15
    相关资源
    最近更新 更多