私は医療プロジェクトに取り組んでおり、そこでは複数の質問とそれに付随するトピックがあります。問題は、次のコードは正常に機能しますが、'must' 句では正常に機能するのに対し、'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に変換する必要があります.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"
]