【问题标题】:How DSL query for match type is not working?匹配类型的 DSL 查询如何不起作用?
【发布时间】:2020-11-06 03:51:18
【问题描述】:

数据集如下

PUT /dataall/test/1
{
    "name": "Computer Science 101",
    "room": "C12",
    "professor": {
        "name": "Gregg Payne",
        "department": "engineering",
        "facutly_type": "full-time",
        "email": "payneg@onuni.com"
        },
    "students_enrolled": 33,
    "course_publish_date": "2013-08-27",
    "course_description": "CS 101 is a first year computer science introduction teaching fundamental data structures and alogirthms using python. "
}

PUT /dataall/test/2
{
    "name": "Theatre 410",
    "room": "T18",
    "professor": {
        "name": "Greg",
        "department": "art",
        "facutly_type": "part-time",
        "email": ""
        },
    "students_enrolled": 47,
    "course_publish_date": "2013-01-27",
    "course_description": "Tht 410 is an advanced elective course disecting the various plays written by shakespere during the 16th century"
}

DSL查询如下

GET dataall/_search
{
  "query": {
    "match": {
          "professor.name":"Greg"
}
  }
}

映射如下。教授在嵌套的json中

{
  "dataall" : {
    "mappings" : {
      "properties" : {
        "course_description" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "course_publish_date" : {
          "type" : "date"
        },
        "dataproduct" : {
          "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
            }
          }
        },
        "professor" : {
          "properties" : {
            "department" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "email" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "facutly_type" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "name" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            }
          }
        },
        "room" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "students_enrolled" : {
          "type" : "long"
        }
      }
    }
  }
}

预计是 2(因为“Greg”与 Gregg Payne 相似)但我得到的只是第二个文档,它是完全匹配的?

为什么我的查询不起作用?甚至必须匹配_phrase_prefix。

我的其他索引工作正常,是不是因为映射嵌套的json?

【问题讨论】:

  • 映射是什么?
  • @Gibbs 添加了映射

标签: elasticsearch dsl elasticsearch-dsl


【解决方案1】:

由于您没有提供数据映射,您的 professor 字段可能是嵌套数据类型或对象数据类型有两种可能性

考虑到您已经为professor 字段创建了嵌套数据类型。 要了解有关匹配短语前缀查询的更多信息,请参阅此ES documentation

映射:

{
  "mappings": {
    "properties": {
      "name": {
        "type": "text"
      },
      "room": {
        "type": "text"
      },
      "professor": {
        "type": "nested",
        "properties": {
          "name": {
            "type": "text"
          }
        }
      }
    }
  }
}

搜索查询:

{  
  "query":{  
    "nested":{  
      "path":"professor",
      "query":{  
        "match_phrase_prefix":{  
          "professor.name":"Greg"
        }
      }
    }
  }
}

现在,考虑到文档包含 内部对象 professor。要了解有关 Object 数据类型的更多信息,请参阅this

映射:

 {
  "mappings": {
    "properties": { 
      "name": {
        "type": "text"
      },
      "professor": { 
        "properties": {
          "name":  { "type": "text" }
          }
        }
      }
    }
  }

搜索查询:

    {
  "query": {
    "match_phrase_prefix": {
      "professor.name": "Greg"
    }
  }
}

以上搜索返回的文档在professorname 字段中包含以Greg 开头的短语。要了解更多关于匹配词组前缀查询的信息,您可以参考this

【讨论】:

  • match_phrase_prefix 的用途是什么。我也添加了映射
  • @aysh 因为您没有为正在使用的文档指定任何映射,ES 将根据您索引的数据生成默认动态映射(与您在问题中提到的映射相同),所以您可以使用我在问题中提到的第二个搜索查询。它会给你想要的搜索结果:)
  • @aysh 了解如何匹配前缀查询,您可以参考此elastic.co/guide/en/elasticsearch/reference/current/…,我还更新了我的答案,简要说明了搜索查询如何返回结果。请通过那个
  • @ESCoder 如果我们将该字段作为对象并且不指定单个字段,例如:{"professor" : { "type" : "object"}}。 -- 在不指定名字/姓氏等单个字段的情况下,我们是否能够搜索名字/姓氏。我无法仅使用最高级别的对象来搜索它
  • @profcalculus 如果您已将professor 定义为object 类型,那么您将能够搜索first name/last name,如果它们是professor 字段的内部对象
猜你喜欢
  • 1970-01-01
  • 2017-02-11
  • 2018-04-10
  • 1970-01-01
  • 2021-03-20
  • 1970-01-01
  • 2018-02-20
  • 2018-06-02
  • 1970-01-01
相关资源
最近更新 更多