elasticsearch - 如果匹配数组中具有不同值的多个字段,则 Elastic Search 会获取记录

标签 elasticsearch

我在 ElasticSearch 中有以下文档

[
    {
      "class": " Grade 1",
      "subject": [
        "Mathematics",
        "German",
        "Geometry",
        "Arts",
        "Physical Education"
      ],
      "student": [
        {
          "name": "George",
          "id": "ORT-823FT"
        },
        {
          "name": "Travis",
          "id": "ORT-873FT"
        },
        {
          "name": "Scott",
          "id": "ORT-883FT"
        }
      ]
    },
    {
      "class": " Grade 2",
      "subject": [
        "Mathematics",
        "German",
        "Geometry",
        "French",
        "Arts",
        "Physical Education"
      ],
      "student": [
        {
          "name": "Gibbs",
          "id": "ORT-923DG"
        },
        {
          "name": "Elizabeth",
          "id": "ORT-973DG"
        },
        {
          "name": "Michale",
          "id": "ORT-983DG"
        }
      ]
    }
  ]

只有当学生姓名和id匹配时,我才需要获取文档,例如:如果学生姓名是George,id是ORT-823FT,那么应该返回第一个文档。另一方面,如果学生姓名是 Gibbs 并且 ID 是 ORT-923DG,则必须返回第二个文档。

下面的查询对我有用,但是有更好的写法吗?

{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "student.id": "ORT-823FT"
          }
        },
        {
          "match": {
            "student.name": "George"
          }
        }
      ]
      
      , "minimum_should_match": 2
    }
  }
}

已更新

学生的映射如下,我添加了“type”:“nested”,如文档中所述。

{
  "student": {
    "type": "nested",
    "properties": {
      "studentResidence": {
        "properties": {
          "residenceAddress": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "phoneNumber": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "parentEmail": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      },
      "studentParentRelationShip": {
        "properties": {
          "relationshipType": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "name": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "residenceAddress": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      },
      "comments": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "id": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "name": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "validFor": {
        "properties": {
          "endDateTime": {
            "type": "date"
          },
          "startDateTime": {
            "type": "date"
          }
        }
      }
    }
  }
}

相应的查询是:

  {
    "query": {
      "nested": {
        "path": "student",
        "query": {
          "bool": {
            "must": [
              {
                "match": {
                  "student.id": "ORT-823FT"
                }},{
                "match": {
                  "student.name": "George"
                }
                
              }
            ]
          }
        }
      }
    }
  }

我仍然得到不正确的输出。不知道我哪里出错了。

最佳答案

根据您的示例数据,下面的映射应该可以工作,请注意,我仅为 student 属性创建了 nested ,其余属性是正常的。

{
    "mappings": {
        "properties": {
            "class": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            },
            "student": {
                "type": "nested", --> note this
                "properties": {
                    "id": {
                        "type": "text",
                        "fields": {
                            "keyword": {
                                "type": "keyword",
                                "ignore_above": 256
                            }
                        }
                    },
                    "name": {
                        "type": "text",
                        "fields": {
                            "keyword": {
                                "type": "keyword",
                                "ignore_above": 256
                            }
                        }
                    }
                }
            },
            "subject": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            }
        }
    }
}

之后,您将您在问题+中提供的示例文档编入索引,并放在另一个示例下方以对其进行正确测试。

{
    "class": " Grade 2",
    "subject": [
        "Mathematics",
        "German",
        "Geometry",
        "French",
        "Arts",
        "Physical Education"
    ],
    "student": [
        {
            "name": "Gibbs",
            "id": "ORT-abc"
        },
        {
            "name": "Elizabeth",
            "id": "ORT-973DG"
        },
        {
            "name": "Michale",
            "id": "ORT-983DG"
        },
        {
            "name": "XYZ",
            "id": "ORT-923DG"
        }
    ]
}

现在,您提供的相同搜索查询将返回正确的结果。

{
    "query": {
        "nested": {
            "path": "student",
            "query": {
                "bool": {
                    "must": [
                        {
                            "match": {
                                "student.id": "ORT-823FT"
                            }
                        },
                        {
                            "match": {
                                "student.name": "George"
                            }
                        }
                    ]
                }
            }
        }
    }
}

SR

"hits": [
            {
                "_index": "nested_mapping_student",
                "_id": "1",
                "_score": 4.0313807,
                "_source": {
                    "class": " Grade 1",
                    "subject": [
                        "Mathematics",
                        "German",
                        "Geometry",
                        "Arts",
                        "Physical Education"
                    ],
                    "student": [
                        {
                            "name": "George",
                            "id": "ORT-823FT"
                        },
                        {
                            "name": "Travis",
                            "id": "ORT-873FT"
                        },
                        {
                            "name": "Scott",
                            "id": "ORT-883FT"
                        }
                    ]
                }
            }
        ]

关于elasticsearch - 如果匹配数组中具有不同值的多个字段,则 Elastic Search 会获取记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74750287/

相关文章:

ruby-on-rails - ElasticSearch/Tire-构面是否未通过查询过滤?

elasticsearch - 如何在具有multi_field类型的字段上查询和应用过滤器

elasticsearch - 删除旧索引elasticsearch?

elasticsearch - ElasticSearch:字段键上的聚合

elasticsearch - 在SOLR或Elasticsearch中排除记录的好方法

elasticsearch - 在 bool 中使用OR运算符的 Elasticsearch 查询

elasticsearch - 如何在elasticsearch中对一个字段进行分面,然后对另一个字段进行分面?

postgresql - 将表从 postgres 数据库同步/导入到 elasticsearch 的正确方法是什么?

regex - 正则表达式在kibana上搜索数组元素

java - Elasticsearch 分析