我正在从事医疗项目,在那里我有多个问题以及附带的主题。问题是以下代码工作正常,但它不考虑' must_not'过滤器,但它必须'工作正常条款。帮助我解决这个问题。
GET stopdata/_search
{
"query": {
"function_score": {
"query": {
"filtered": {
"query": {
"match": {
"question": "Hello Dr. Iam suffering from fever with cough nd cold since 3 days"
}
}
}
},
"filter": {
"bool": {
"must": [
{
"terms": {
"topics": [
"fever",
"cough"
]
}
}
],
"must_not": [
{
"terms": {
"topics": [
"children",
"child",
"childrens health"
]
}
}
]
}
},
"random_score": {}
}
},
"highlight": {
"fields": {
"keyword": {}
}
}
}
另外,我需要将代码转换为Java,我正在尝试使用以下代码。
Set<String> mustNot = new HashSet<String>();
mustNot.add("child");
mustNot.add("children");
mustNot.add("childrens health");
Set<String> must = new HashSet<String>();
must.add("fever");
must.add("cough");
FunctionScoreQueryBuilder fsqb = new FunctionScoreQueryBuilder(QueryBuilders.matchQuery("question", "Hello Dr. Iam suffering from fever with cough nd cold since 3 days"));
fsqb.add(ScoreFunctionBuilders.randomFunction((new Date()).getTime()));
BoolQueryBuilder bqb = boolQuery()
.mustNot(termsQuery("topics", mustNot));
SearchResponse response1 = client.prepareSearch("stopdata")
.setQuery(fsqb)
.execute()
.actionGet();
System.out.println(response1.getHits().getTotalHits());
&#39; stopdata&#39;索引如下
{
"stopdata": {
"mappings": {
"questions": {
"properties": {
"answers": {
"type": "string"
},
"id": {
"type": "long"
},
"question": {
"type": "string",
"analyzer": "my_english"
},
"relevantQuestions": {
"type": "long"
},
"topics": {
"type": "string"
}
}
}
}
}
}
添加上述索引的样本数据
"question": "My son of age 8 months is suffering from cough and cold and fever. What treatment I have to follow?"
"topics": [
"Cough",
"Fever",
"Hydration",
"Nutrition",
"Tens",
"Childrens health"
]
"question": "Hi.My daughter, 4 years old , has on and of fever with severe coughing and colds for 3 days now.She vomited as well last night.Do you think it's viral?"
"topics": [
"Vomiting",
"Flu",
"Cough",
"Fever",
"Pneumonia",
"Meningitis",
"Tamiflu",
"Incision",
"Childrens health",
"Oseltamivir"
]
"question": "If you have a fever of 101 with chills and sweats for 2 day with a slight cough, should you go to the drs or let is wear off?"
"topics": [
"Cough",
"Fever"
]
答案 0 :(得分:1)
我看到的是整个filter
部分放错地方,它应该放在filtered
查询中,因为{{1}的根处没有filter
元素元素(见official docs)。所以你的查询首先应该是这样的+你应该使用POST而不是GET,因为你要发送一个有效载荷:
function_score
现在用Java编写所有这些内容,就像这样:
POST stopdata/_search
{
"query": {
"function_score": {
"query": {
"filtered": {
"query": {
"match": {
"question": "Hello Dr. Iam suffering from fever with cough nd cold since 3 days"
}
},
"filter": {
"bool": {
"must": [
{
"terms": {
"topics": [
"fever",
"cough"
]
}
}
],
"must_not": [
{
"terms": {
"topics": [
"children",
"child",
"childrens health"
]
}
}
]
}
}
}
},
"random_score": {}
}
},
"highlight": {
"fields": {
"keyword": {}
}
}
}
<强>更新强>
Set<String> mustNot = new HashSet<String>();
mustNot.add("child");
mustNot.add("children");
mustNot.add("childrens health");
Set<String> must = new HashSet<String>();
must.add("fever");
must.add("cough");
MatchQueryBuilder query = QueryBuilders.matchQuery("question", "Hello Dr. Iam suffering from fever with cough nd cold since 3 days");
BoolFilterBuilder filter = FilterBuilders.boolFilter()
.must(FilterBuilders.termsFilter("topics", must))
.mustNot(FilterBuilders.termsFilter("topics", mustNot));
FilteredQueryBuilder fqb = QueryBuilders.filteredQuery(query, filter);
FunctionScoreQueryBuilder fsqb = QueryBuilders.functionScoreQuery(fqb);
fsqb.add(ScoreFunctionBuilders.randomFunction((new Date()).getTime()));
SearchResponse response1 = client.prepareSearch("stopdata")
.setQuery(fsqb)
.execute()
.actionGet();
System.out.println(response1.getHits().getTotalHits());
与must_not
不匹配的原因是因为分析了childrens health
字段,因此topics
被标记化并分析为两个标记{{1 }和Childrens health
,因此在childrens
上尝试health
匹配不会产生任何结果。也许,分成两个术语会有所帮助:
terms