0

How mapping have role to find the search??

GET courses/_search

return is below

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.0226655,
    "hits" : [
      {
        "_index" : "courses",
        "_type" : "classroom",
        "_id" : "7",
        "_score" : 1.0226655,
        "_source" : {
          "name" : "Computer Internals 250",
          "room" : "C8",
          "professor" : {
            "name" : "Gregg Va",
            "department" : "engineering",
            "facutly_type" : "part-time",
            "email" : "payneg@onuni.com"
          },
          "students_enrolled" : 33,
          "course_publish_date" : "2012-08-20",
          "course_description" : "cpt Int 250 gives students an integrated and rigorous picture of applied computer science, as it comes to play in the construction of a simple yet powerful computer system. "
        }
      },
      {
        "_index" : "courses",
        "_type" : "classroom",
        "_id" : "4",
        "_score" : 0.2876821,
        "_source" : {
          "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 algorithms using python. "
        }
      }
    ]
  }
}

mapping is below

{
  "courses" : {
    "mappings" : {
      "properties" : {
        "course_description" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "course_publish_date" : {
          "type" : "date"
        },
        "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"
        }
      }
    }
  }
}

I need to return the exact match phrase professor.name=Gregg Payne

I tried below query as per direction from https://www.elastic.co/guide/en/elasticsearch/guide/current/_finding_exact_values.html

GET courses/_search
{
    "query" : {
        "constant_score" : {
            "filter" : {
                "term" : {
                    "professor.name" : "Gregg Payne"
                }
            }
        }
    }
}

1 Answers1

1

Based on your mapping, here is the query that shall work for you -

POST http://localhost:9200/courses/_search

{
    "query" : {
        "constant_score" : {
            "filter" : {
                "term" : {
                    "professor.name.keyword" : "Gregg Payne"
                }
            }
        }
    }
}

Answering your question in the comments - search is always about mappings :) In your case you use Term query which is about searching for exact values and it needs a keyword field. Text fields get analyzed:

Avoid using the term query for text fields.

By default, Elasticsearch changes the values of text fields as part of analysis. This can make finding exact matches for text field values difficult.

To search text field values, use the match query instead

Kiryl Z
  • 1,273
  • 8
  • 15
  • How to add below one ` "professor.name.keyword" : "Gregg Payne" , "professor.facutly_type.keyword" : "part-time"` . i got error –  Jul 09 '20 at 09:23
  • what are u trying to achieve exactly? search by several fields in the same request? – Kiryl Z Jul 09 '20 at 09:25
  • professor.name=Gregg Payne and professor.facutly_type.keyword=part-time –  Jul 09 '20 at 09:40
  • Kiryl sorry to disturb you, two more queries required if i need exact match 1. `professor.name=Gregg Payne and professor.facutly_type.keyword=part-time` 2. `professor.name=Gregg Payne or professor.facutly_type.keyword=part-time` . second one is or and first one is and –  Jul 09 '20 at 09:50
  • Check out https://stackoverflow.com/questions/21202775/elasticsearch-multi-term-filter and https://stackoverflow.com/questions/28538760/elasticsearch-bool-query-combine-must-with-or – Kiryl Z Jul 09 '20 at 10:00
  • Basically you'll need a compound bool query with "must" for AND and "should" for OR – Kiryl Z Jul 09 '20 at 10:01