问题描述
为了增强从弹性搜索获得的搜索结果,我想从我的java代码中增加我的停止词库。 到目前为止,我正在使用默认的停止分析器列表,它没有像What,Who,Why等列表中的疑问词。我们想在查询结果时从搜索中删除这些词和一些额外的词。 我曾尝试代码从这里(最后ANS)
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”,“for”,“if”,“in”,“into” “,”是“,”它是“,”不是“,”不是“,”,“,”,“或”,“,”,“,”,“,”,“,”,“,”,“然后”, “那里”,“这些”,“他们”,“这个”,“来”,“是”,“将”,“带”
但我想增加这个库。
1楼
你走在正确的轨道上。
在你的第一个上市( )创建调用自定义分析my_analyzer
被叫指数my_index
这将对消除了效果“与”和“的”从文本您使用my_analyzer
用。
现在要实际使用它,你应该:
-
确保你在你要查询的索引上定义了
my_analyzer
(questionindex
?) -
为您要删除“and”和“the”的字段(例如
question_title
字段)创建使用my_analyzer
的文档的映射: 使用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"
}
}
}
}
}