我是 ES 新手,需要帮助解决这个用例。
我的 ES 索引有不同类型的文档(示例如下)。我需要根据以下条件返回匹配文档的搜索查询帮助:“返回在“标题”中具有匹配单词的“课程”和“教师”类型的所有文档,并且仅返回与“标题”中的单词匹配的“学生”类型文档'+'老师=
例如:如果登录的教师 ID 是“imaa-tea-2”并且搜索词是“science”,我希望搜索查询返回文档 2、4、5 和 8(8 因为教师 = imaa-tea- 2).
{
"type": "lesson",
"id": 1,
"subject": "mathematics",
"title": "second grade additions",
"class": "second",
"link": "http://myskoolblhahblha/simple_add",
"id": "math2-11",
"description": "this is custom syllabus for imaa"
}
{
"type": "lesson",
"id": 2,
"subject": "science",
"title": "living things",
"class": "second",
"link": "http://myskoolblhahblha/life",
"id": "sc2-01",
"description": "this is custom syllabus for imaa and mitkids"
}
{
"type": "teacher",
"id": 3,
"title": "second grade math teacher at imaa",
"details": {
"id": "imaa-tea-1",
"name": "david jack",
"school": "institue of math & science for all ages",
"degree": "bachelor of applied mathematics"
}
}
{
"type": "teacher",
"id": 4,
"title": "second grade science teacher at imaa",
"details": {
"id": "imaa-tea-2",
"name": "john wick",
"school": "institue of math & science for all ages",
"degree": "bachelor of science"
}
}
{
"type": "teacher",
"id": 5,
"title": "first grade science teacher at salsa",
"details": {
"id": "salsa-1",
"name": "big hero",
"school": "salsa elementary school",
"degree": "bachelor of education"
}
}
{
"type": "student",
"id": 6,
"title": "student of imaa",
"id": "imaa-stu-1",
"name": "lilly john",
"class": "second",
"school": "institue of math & science for all ages",
"teacher": "imaa-tea-1"
}
{
"type": "student",
"id": 7,
"title": "math student of imaa",
"id": "imaa-stu-2",
"name": "kala jam",
"class": "second",
"school": "institue of math & science for all ages",
"teacher": "imaa-tea-2"
}
{
"type": "student",
"id": 8,
"title": "science student of imaa",
"id": "imaa-stu-3",
"name": "adam dima",
"class": "third",
"school": "institue of math & science for all ages",
"teacher": "imaa-tea-2"
}
{
"type": "student",
"id": 9,
"title": "science student of salsa",
"id": "salsa-stu-3",
"name": "mary kumar",
"class": "first",
"school": "salsa elementary",
"teacher": "salsa-1"
}
据我了解,您正在寻找以下条件:
science
。teacher
或 lesson
student
并且教师字段为 <logged in teacher id>
基于此解释,我们期望查询返回 4、5 和 8 个文档。
请注意,您还提到了文档 2,尽管它的
为science
而不是subject
title
您可以使用BooleanQuery进行如此复杂的查询,包括And、Ors。这是您提供的文件的示例:
{
"query": {
"bool": {
"must": [ // this condition must be met for all documents!
{"match": {
"title": "science"
}}
],
"minimum_should_match": 1,
// here we have multiple conditions which at least one of them should be met.
//(type is teacher or lesson) OR (the type is student AND the teacher is imaa-tea-2)
"should": [
{
"term": {
"type.keyword": {
"value": "teacher"
}
}
},
{
"term": {
"type.keyword": {
"value": "lesson"
}
}
},
{
"bool": {
"must": [
{"term": {
"type.keyword": {
"value": "student"
}
}},
{
"term": {
"teacher.keyword": {
"value": "imaa-tea-2"
}
}
}
]
}
}
]
}
}
}
注意:您可能想知道为什么我使用
而不是type.keyword
,它的解释超出了这个问题的范围。所以我希望您已经明白了我的回答的要点,以解决您的问题。type