# /

# Search APIs

搜索API用于搜索和聚合存储在Elasticsearch索引和数据流中的数据。有关概述和相关教程,请参见搜索数据。
除了explain API之外,大多数搜索API都支持多目标语法。

Core search 核心搜索
Search 搜索
Multi search 多重搜索
Async search 异步搜索
Point in time 时间点
Scroll 纸卷
Clear scroll 清除滚动
Suggesters 建议者

Search testing 搜索测试
Explain 解释
Field capabilities 现场能力
Profile 轮廓
Ranking evaluation 排名评估
Search shards 搜索碎片
Validate 验证

Search templates 搜索模板
Search template 搜索模板
Multi search template 多搜索模板
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

# Suggesters

通过使用suggester,根据提供的文本提出外观相似的术语。部分建议功能仍在开发中。

Term suggester
Phrase Suggester
Completion Suggester
Context Suggester
1
2
3
4
5
6
# Term suggester
在提出术语之前,对所提供的建议文本进行分析。建议的术语是根据分析的建议文本标记提供的。

缺点:只适合关键字搜索。
1
2
3
PUT easyes_document/_doc/1
{
  "content" : "测试内容1",
  "creator" : "老汉",
  "gmt_create" : "2023-11-14 20:50:48",
  "title" : "测试1"
}

PUT easyes_document/_doc/2
{
  "content" : "测试内容2",
  "creator" : "新人",
  "gmt_create" : "2023-11-14 20:50:48",
  "title" : "测算1"
}

PUT easyes_document/_doc/3
{
  "content" : "测试内容3",
  "creator" : "路人",
  "gmt_create" : "2023-11-14 20:50:48",
  "title" : "测量1"
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
POST easyes_document/_search
{
  "suggest": {
    "-": {
      "text": "测算测试测量",
      "term": {
        "analyzer": "ik_max_word",
        "suggest_mode": "missing",//如果术语与关键字一样,则不显示
        "min_word_length": 1,
        "field": "title.keyword"
      }
    }
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "suggest" : {
    "-" : [
      {
        "text" : "测算",
        "offset" : 0,
        "length" : 2,
        "options" : [
          {
            "text" : "测算1",
            "score" : 0.5,
            "freq" : 1
          }
        ]
      },
      {
        "text" : "测试",
        "offset" : 2,
        "length" : 2,
        "options" : [
          {
            "text" : "测试1",
            "score" : 0.5,
            "freq" : 1
          }
        ]
      },
      {
        "text" : "测量",
        "offset" : 4,
        "length" : 2,
        "options" : [
          {
            "text" : "测量1",
            "score" : 0.5,
            "freq" : 1
          }
        ]
      }
    ]
  }
}

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
# Phrase Suggester
Phrase Suggester
1
# Completion Suggester
completion suggester 提供键入时自动完成/搜索功能。
这是一个导航功能,可在用户键入时引导用户找到相关结果,从而提高搜索精度。
它不是用来拼写纠正的,或者是像term或phrase suggester这样的功能。

理想情况下,自动完成功能应该和用户键入的一样快,以提供与用户已经键入的内容相关的即时反馈。
因此,completion suggester针对速度进行了优化。使用的数据结构可以实现快速查找,但构建成本高昂,并且存储在内存中。

优点:支持前缀查询、前缀模糊查询、前缀正则查询。适合作为前缀搜索,比edge N-grams分词器效果好。
缺点:只适合前缀搜索,如果搜索字段不是前缀,召回效果不好。
1
2
3
4
5
6
7
8
9
#添加两首音乐
PUT music
{
  "mappings": {
    "properties": {
      "suggest": {
        "type": "completion"
      }
    }
  }
}

PUT music/_doc/1?refresh
{
  "suggest" : [ "Nevermind", "Nirvana" ]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# prefix query
#前缀搜索音乐nir
POST music/_search?pretty
{
  "suggest": {
    "song-suggest": {
      "prefix": "nir",        
      "completion": {         
        "field": "suggest"  
      }
    }
  }
}

#搜索结果
{
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits": ...
  "took": 2,
  "timed_out": false,
  "suggest": {
    "song-suggest" : [ {
      "text" : "nir",
      "offset" : 0,
      "length" : 3,
      "options" : [ {
        "text" : "Nirvana",
        "_index": "music",
        "_id": "1",
        "_score": 1.0,
        "_source": {
          "suggest": ["Nevermind", "Nirvana"]
        }
      } ]
    } ]
  }
}
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
# fuzzy query
POST music/_search?pretty
{
  "suggest": {
    "song-suggest": {
      "prefix": "nor",
      "completion": {
        "field": "suggest",
        "fuzzy": {
          "fuzziness": 2
        }
      }
    }
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# regex query
POST music/_search?pretty
{
  "suggest": {
    "song-suggest": {
      "regex": "n[ever|i]r",
      "completion": {
        "field": "suggest"
      }
    }
  }
}
1
2
3
4
5
6
7
8
9
10
11
# 一个字段多重数据类型(Text field type)
text字段的参数
fields 多重字段允许以多种方式对同一字符串值进行索引,以达到不同的目的,例如一个字段用于搜索,多个字段用于排序和聚合,或者由不同的分析器分析同一个字符串值。
1
2
PUT music
{
  "mappings": {
    "properties": {
      "suggest": {
        "type": "completion"
      }
    }
  }
}
POST music/_search?pretty
{
  "suggest": {
    "song-suggest": {
      "prefix": "nir",        
      "completion": {         
        "field": "suggest"  
      }
    }
  }
}


#一个字段多重数据类型(text、completion)
PUT music
{
  "mappings": {
    "properties": {
      "title":{
        "type":"text",
        "fields":{
          "suggest": { "type": "completion" }
        }
      }
    }
  }
}
POST music/_search?pretty
{
  "suggest": {
    "song-suggest": {
      "prefix": "nir",        
      "completion": {         
        "field": "title.suggest"  
      }
    }
  }
}
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
# Context Suggester
Context Suggester
1