为了增强从 Elasticsearch 中获得的搜索结果,我想从我的 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”、 “对于”、“如果”、“在”、“进入”、"is"、“它”、 “不”、“不是”、“的”、“关于”、“或”、“这样”、 “那个”,“那个”,“他们的”,“然后”,“那里”,“这些”, “他们”、“这个”、“到”、“曾经”、“将”、“与”
但是我想增加这个库。
最佳答案
您走在正确的轨道上。在您的第一个列表 ( from the documentation about stopwords ) 中,您创建了一个名为 my_analyzer
的自定义分析器对于名为 my_index
的索引这将具有从您使用的文本中删除 "and" 和 "the" 的效果 my_analyzer
与。
现在要实际使用它,您应该:
- 确保你已经定义了
my_analyzer
在您查询的索引上(questionindex
?) - 使用
my_analyzer
为您的文档创建一个映射对于您要删除的字段 “and” 和 “the”(例如question_title
字段): 使用 Analyze API 测试您的分析器
GET /questionindex/_analyze?field=question.question_title&text=No quick brown fox jumps over my lazy dog and the indolent cat
重新索引您的文档
试试这个作为起点:
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"
}
}
}
}
}
关于java - 使用java在 Elasticsearch 中创建自定义停用词列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31625943/