我正在从事医疗项目,我有多个问题以及附加的主题。问题是以下代码工作正常,但它不考虑“must_not”过滤器,而与“must”子句一起工作正常。帮我解决这个问题。
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());
'stopdata' 索引的映射如下
{
"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"
]