【问题标题】:How to get the exact match of using DSL如何获得使用 DSL 的精确匹配
【发布时间】:2020-10-29 03:47:05
【问题描述】:

映射对查找搜索有何作用?

获取课程/_搜索

返回低于

{
  "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. "
        }
      }
    ]
  }
}

映射如下

{
  "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"
        }
      }
    }
  }
}

我需要返回完全匹配的短语professor.name=Gregg Payne

我按照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"
                }
            }
        }
    }
}

【问题讨论】:

  • 您的映射看起来如何?
  • @KirylZ 添加了映射,映射对查找搜索有何作用??
  • 答案中解释

标签: elasticsearch dsl


【解决方案1】:

根据您的映射,下面是适合您的查询 -

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

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

在 cmets 中回答您的问题 - 搜索始终与映射有关 :) 在您的情况下,您使用 Term query 这是关于搜索确切值的,它需要一个关键字字段。 Text fields get analyzed:

避免对文本字段使用术语查询。

默认情况下,Elasticsearch 会更改文本字段的值作为 分析。这可以找到文本字段值的完全匹配 困难。

要搜索文本字段值,请改用匹配查询

【讨论】:

  • 如何在下面添加一个`“professor.name.keyword”:“Gregg Payne”,“professor.facutly_type.keyword”:“part-time”`。我有错误
  • 你到底想达到什么目的?在同一个请求中搜索多个字段?
  • professor.name=Gregg Payne 和professor.facutly_type.keyword=part-time
  • Kiryl 很抱歉打扰您,如果我需要完全匹配,还需要两个查询 1.professor.name=Gregg Payne and professor.facutly_type.keyword=part-time 2.professor.name=Gregg Payne or professor.facutly_type.keyword=part-time 。第二个是 or 而第一个是 and
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-19
  • 1970-01-01
相关资源
最近更新 更多