2

我正在从事医疗项目,我有多个问题以及附加的主题。问题是以下代码工作正常,但它不考虑“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"
]
4

1 回答 1

2

我看到的是整个filter部分放错了位置,它应该进入filtered查询内部,因为filter元素的根部没有function_score元素(参见官方文档)。因此,您的查询首先应该看起来像这样 + 您应该使用 POST 而不是 GET,因为您正在发送有效负载:

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被标记化并被分析为两个标记childrenshealth,因此尝试terms匹配childrens health不会产生任何结果。也许,分成两个术语会有所帮助:

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