Search code examples
elasticsearchdslelasticsearch-dsl

How DSL query for match type is not working?


Data set is below

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 query is below

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

mapping is below. Professor is in nested 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"
        }
      }
    }
  }
}

Expected out is 2(because "Greg" is similar to Gregg Payne) but i am getting one that is second document only which is exact match?

Why my query is not working? Even is it mandatory to match_phrase_prefix.

My other index are working fine, is it because of mapping nested json?


Solution

  • Since you have not provided mapping of your data, there can be two possibilities that either your professor field is of nested data type OR Object data type

    Considering you have created nested data type for professor field. To know more about Match Phrase prefix query refer to this ES documentation

    Mapping:

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

    Search Query:

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

    Now, considering the document contains an inner object professor. To know more about Object data type refer this

    Mapping:

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

    Search query:

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

    The above search returns documents that contain phrases beginning with Greg in the name field of professor. To know more about Match phrase prefix query you can refer this