关于Elasticsearch:如何获得使用DSL的精确匹配

How to get the exact match of using DSL

映射如何发挥作用以找到搜索内容?

获取课程/ _搜索

返回值低于

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
{
 "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" :"[email protected]"
          },
         "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" :"[email protected]"
          },
         "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."
        }
      }
    ]
  }
}

映射低于

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
{
 "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/zh-cn/elasticsearch/guide/current/_finding_exact_values.html

中的说明在以下查询中进行查询

1
2
3
4
5
6
7
8
9
10
11
12
GET courses/_search
{
   "query" : {
       "constant_score" : {
           "filter" : {
               "term" : {
                   "professor.name" :"Gregg Payne"
                }
            }
        }
    }
}


根据您的映射,以下是对您有用的查询-

1
2
3
4
5
6
7
8
9
10
11
12
13
POST http://localhost:9200/courses/_search

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

在评论中回答您的问题-搜索始终是关于映射的:)在您的情况下,您将使用术语查询来搜索精确值,并且需要一个关键字字段。分析文本字段:

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