2

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

1 に答える 1

2

私が見ているのは、要素のルートに要素がないためfilter、パーツ全体が間違って配置されていることです。filteredクエリ内に移動する必要があります(公式ドキュメントを参照)。したがって、クエリは最初は次のようになります + ペイロードを送信しているため、GET の代わりに POST を使用する必要があります。filterfunction_score

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": {}
    }
  }
}

これらすべてを 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");

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フィールドが分​​析されるため、Childrens healthがトークン化され、2 つのトークンchildrensおよびとして分析されるため、で一致healthを試みても何も得られないためです。おそらく、2 つの用語に分割すると役立つでしょう。termschildrens health

              "must_not": [
                {
                  "terms": {
                    "topics": [
                      "children",
                      "child",
                      "childrens", 
                      "health"
                    ]
                  }
                }
              ]
于 2015-11-26T04:40:36.190 に答える