當前位置:
首頁 > 知識 > Elasticsearch的DSL操作命令大全

Elasticsearch的DSL操作命令大全

文章目錄

奇葩需求:

es內置的分詞器:

修改mapping:

重構索引:

奇葩需求:

1.

按照論壇名稱,對論壇評論總量,1-5月按月聚合 相對應欄位為cmtCnt

按照論壇名稱,對論壇發帖點贊量,1-5月按月聚合 相對應欄位為adtCnt

GET xiao-2018-4-1,xiao-2018-6-12,xiao-2018-3-1/Socials/_search

{

"size" : 0,

"query" : {

"constant_score" : {

"filter" : {

"bool" : {

"must" : [

{

"term" : {

"sourceType" : "FORUM"

}

},

{

"range": {

"timeDay": {

"gte": "2018-01-01",

"lte": "2018-05-31"

}

}

}

]

}

}

}

},

"aggs" : {

"all_interests" : {

"terms" : {

"size" : 100000,

"field" : "website.keyword"

},

"aggs": {

"month_num": {

"date_histogram": {

"field": "timeDay",

"interval": "month",

"format": "yyyy-MM"

},

"aggs": {

"single_sum": {

"sum" : { "field" : "cmtCnt" }

}

}

}

}

}

}

}

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

結果:

{

"took": 9141,

"timed_out": false,

"_shards": {

"total": 350,

"successful": 350,

"failed": 0

},

"hits": {

"total": 735705,

"max_score": 0,

"hits": []

},

"aggregations": {

"all_interests": {

"doc_count_error_upper_bound": 0,

"sum_other_doc_count": 0,

"buckets": [

{

"key": "論壇",

"doc_count": 661238,

"month_num": {

"buckets": [

{

"key_as_string": "2018-01",

"key": 1514764800000,

"doc_count": 3,

"single_sum": {

"value": 0

}

},

{

"key_as_string": "2018-02",

"key": 1517443200000,

"doc_count": 0,

"single_sum": {

"value": 0

}

},

{

"key_as_string": "2018-03",

"key": 1519862400000,

"doc_count": 1403,

"single_sum": {

"value": 0

}

},

{

"key_as_string": "2018-04",

"key": 1522540800000,

"doc_count": 125895,

"single_sum": {

"value": 0

}

},

{

"key_as_string": "2018-05",

"key": 1525132800000,

"doc_count": 533937,

"single_sum": {

"value": 0

}

}

]

}

},

{

"key": "百度貼吧",

"doc_count": 21275,

"month_num": {

"buckets": [

{

"key_as_string": "2018-02",

"key": 1517443200000,

"doc_count": 290,

"single_sum": {

"value": 406

}

},

{

"key_as_string": "2018-03",

"key": 1519862400000,

"doc_count": 20472,

"single_sum": {

"value": 19174

}

},

{

"key_as_string": "2018-04",

"key": 1522540800000,

"doc_count": 483,

"single_sum": {

"value": 1740

}

},

{

"key_as_string": "2018-05",

"key": 1525132800000,

"doc_count": 30,

"single_sum": {

"value": 45

}

}

]

}

},

{

"key": "股吧",

"doc_count": 6395,

"month_num": {

"buckets": [

{

"key_as_string": "2018-02",

"key": 1517443200000,

"doc_count": 10,

"single_sum": {

"value": 26

}

},

{

"key_as_string": "2018-03",

"key": 1519862400000,

"doc_count": 6383,

"single_sum": {

"value": 24965

}

},

{

"key_as_string": "2018-04",

"key": 1522540800000,

"doc_count": 1,

"single_sum": {

"value": 0

}

},

{

"key_as_string": "2018-05",

"key": 1525132800000,

"doc_count": 1,

"single_sum": {

"value": 0

}

}

]

}

},

{

"key": "好大夫在線",

"doc_count": 2933,

"month_num": {

"buckets": [

{

"key_as_string": "2018-02",

"key": 1517443200000,

"doc_count": 5,

"single_sum": {

"value": 0

}

},

{

"key_as_string": "2018-03",

"key": 1519862400000,

"doc_count": 2927,

"single_sum": {

"value": 0

}

},

{

"key_as_string": "2018-04",

"key": 1522540800000,

"doc_count": 1,

"single_sum": {

"value": 0

}

}

]

}

}

。。。。。太多省略

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

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

2.

按照論壇名稱,對論壇正面情感總量(非負數),1-5月按月聚合 相對應欄位為sentimentOrient

GET xiao-2018-4-1,xiao-2018-6-12,xiao-2018-3-1/Socials/_search

{

"size" : 0,

"query" : {

"constant_score" : {

"filter" : {

"bool" : {

"must" : [

{

"term" : {

"sourceType" : "FORUM"

}

},

{

"range": {

"timeDay": {

"gte": "2018-01-01",

"lte": "2018-05-31"

}

}

}

],

"must_not" : [

{ "term" : { "sentimentOrient" : -1} },

{ "term" : { "sentimentOrient" : 0 } }

]

}

}

}

},

"aggs" : {

"all_interests" : {

"terms" : {

"size" : 100000,

"field" : "website.keyword"

},

"aggs": {

"month_num": {

"date_histogram": {

"field": "timeDay",

"interval": "month",

"format": "yyyy-MM"

}

}

}

}

}

}

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

疑問:本來一開始想按官網https://elasticsearch.cn/book/elasticsearch_definitive_guide_2.x/_extended_example.html上用extended_bounds來限制時間範圍死活不好使我也是奇了怪了,最後只能轉變思路在查詢的時候做手腳了。

3.

需要監測的關鍵詞:零跑,零跑汽車,零跑S01

需要過濾關鍵詞:零跑腿,專家門診

社交和新聞根據url欄位去重後6月2號到7月2號的總數值

GET xiao-2018-6-12,xiao-2018-6-19,xiao-2018-6-26,xiao-2018-6-5/Socials/_search

{

"size" : 0,

"query" : {

"constant_score" : {

"filter" : {

"bool" : {

"must" : [

{

"range": {

"timeDay": {

"gte": "2018-06-02",

"lte": "2018-07-02"

}

}

},

{

"query_string":{

"default_field":"textZh",

"query":"零跑 OR 零跑汽車 OR 零跑S01 NOT 零跑腿 NOT 專家門診"

或者

"query" : "( ( ""零跑"" ) OR ( ""零跑汽車"" ) OR ( ""零跑S01"" ) NOT ( ""零跑腿"" ) NOT ( ""專家門診"" ) )"

}

}

]

}

}

}

},

"aggs" : {

"distinct_colors" : {

"cardinality" : {

"field" : "url"

}

}

}

}

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

註:上面這個查詢結果不對,還是有問題

原因:這個索引mapping里textZh欄位的設置如下

"textZh": {

"type": "text",

"store": true,

"fields": {

"keyword": {

"type": "keyword",

"ignore_above": 256

}

},

"analyzer": "ik_smart"

}

1

2

3

4

5

6

7

8

9

10

11

導致你輸入「零跑」會被分詞為「零」和「跑」,所以搜索的結果並不是你想要的

解決:

社交(_type是「Socials」的為社交,_type是「News」的為新聞):

GET xiao-2018-6-12,xiao-2018-6-19,xiao-2018-6-26,xiao-2018-6-5/Socials/_search

{

"size" : 0,

"query" : {

"constant_score" : {

"filter" : {

"bool": {

"must": {

"range": {

"timeStr": {

"gte": "2018-06-02 00:00:00",

"lte": "2018-07-03 00:00:00"

}

}

},

"should": [

{

"match_phrase": {

"textZh" : {

"query" : "零跑"

}

}

},

{

"match_phrase": {

"textZh" : {

"query" : "零跑汽車"

}

}

},

{

"match_phrase": {

"textZh" : {

"query" : "零跑S01"

}

}

}

],

"must_not": {

"bool": {

"should": [

{

"match_phrase": {

"textZh" : "零跑腿"

}

},

{

"match_phrase": {

"textZh" : {

"query" : "專家門診"

}

}

}

]

}

}

}

}

}

},

"aggs" : {

"distinct_colors" : {

"cardinality" : {

"field" : "url"

}

}

}

}

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

4.25號到28號指定時間段的微博號總和(按發文量倒排)

GET xiaoqiang-2018-10-29/Socials/_search

{

"size": 0,

"query" : {

"constant_score" : {

"filter" : {

"bool" : {

"must" : [

{

"range": {

"timeHour": {

"gte": "2018-10-25 21",

"lte": "2018-10-28 21",

"format": "yyyy-MM-dd HH"

}

}

},

{

"term" : {

"sourceType": "weibo"

}

}

]

}

}

}

},

"aggs" : {

"all_interests" : {

"terms" : {

"script" : "String he=new SimpleDateFormat("HH").format(new Date(doc["timeHour"].value)); if(he.equals("01")){return he;}else{return null;}",

"order" : { "_term" : "desc" }

},

"aggs" : {

"per_count" : {

"terms" : {

"script" : "doc["url"].value.substring(17,27)"

}

}

}

}

}

}

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

結果:

{

"took": 4182,

"timed_out": false,

"_shards": {

"total": 10,

"successful": 10,

"failed": 0

},

"hits": {

"total": 2997411,

"max_score": 0,

"hits": []

},

"aggregations": {

"all_interests": {

"doc_count_error_upper_bound": 0,

"sum_other_doc_count": 0,

"buckets": [

{

"key": "01",

"doc_count": 154272,

"per_count": {

"doc_count_error_upper_bound": 0,

"sum_other_doc_count": 154116,

"buckets": [

{

"key": "5670137181",

"doc_count": 20

},

{

"key": "5408336201",

"doc_count": 16

},

{

"key": "1084088733",

"doc_count": 15

},

{

"key": "1283869875",

"doc_count": 15

},

{

"key": "1322676944",

"doc_count": 15

},

{

"key": "1668841355",

"doc_count": 15

},

{

"key": "1681901143",

"doc_count": 15

},

{

"key": "1704588860",

"doc_count": 15

},

{

"key": "1819301715",

"doc_count": 15

},

{

"key": "1823510107",

"doc_count": 15

}

]

}

}

]

}

}

}

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

5.近n天每天各個時段的微博號(按發文量倒排)

GET xiaoqiang-2018-10-29/Socials/_search

{

"size": 0,

"query" : {

"constant_score" : {

"filter" : {

"bool" : {

"must" : [

{

"range": {

"timeHour": {

"gte": "2018-10-26 21",

"lte": "2018-10-28 21",

"format": "yyyy-MM-dd HH"

}

}

},

{

"term" : {

"sourceType": "weibo"

}

}

]

}

}

}

},

"aggs" : {

"all_interests" : {

"terms" : {

"script" : "new SimpleDateFormat("HH").format(new Date(doc["timeHour"].value))",

"order" : { "_term" : "desc" }

},

"aggs": {

"month_num": {

"date_histogram": {

"field": "timeDay",

"interval": "day",

"format": "yyyy-MM-dd"

},

"aggs" : {

"per_count" : {

"terms" : {

"size" : 1,

"script" : "doc["url"].value.substring(17,27)"

}

}

}

}

}

}

}

}

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

結果:

{

"took": 15584,

"timed_out": false,

"_shards": {

"total": 10,

"successful": 10,

"failed": 0

},

"hits": {

"total": 2143757,

"max_score": 0,

"hits": []

},

"aggregations": {

"all_interests": {

"doc_count_error_upper_bound": 0,

"sum_other_doc_count": 1356451,

"buckets": [

{

"key": "23",

"doc_count": 93821,

"month_num": {

"buckets": [

{

"key_as_string": "2018-10-27",

"key": 1540598400000,

"doc_count": 37782,

"per_count": {

"doc_count_error_upper_bound": 0,

"sum_other_doc_count": 37767,

"buckets": [

{

"key": "1057449614",

"doc_count": 15

}

]

}

},

{

"key_as_string": "2018-10-28",

"key": 1540684800000,

"doc_count": 56039,

"per_count": {

"doc_count_error_upper_bound": 0,

"sum_other_doc_count": 56024,

"buckets": [

{

"key": "1218291087",

"doc_count": 15

}

]

}

}

]

}

},

{

"key": "22",

"doc_count": 92328,

"month_num": {

"buckets": [

{

"key_as_string": "2018-10-27",

"key": 1540598400000,

"doc_count": 37043,

"per_count": {

"doc_count_error_upper_bound": 0,

"sum_other_doc_count": 37028,

"buckets": [

{

"key": "1306672440",

"doc_count": 15

}

]

}

},

{

"key_as_string": "2018-10-28",

"key": 1540684800000,

"doc_count": 55285,

"per_count": {

"doc_count_error_upper_bound": 0,

"sum_other_doc_count": 55270,

"buckets": [

{

"key": "1221062225",

"doc_count": 15

}

]

}

}

]

}

},

{

"key": "21",

"doc_count": 92859,

"month_num": {

"buckets": [

{

"key_as_string": "2018-10-27",

"key": 1540598400000,

"doc_count": 36797,

"per_count": {

"doc_count_error_upper_bound": 0,

"sum_other_doc_count": 36782,

"buckets": [

{

"key": "3799113457",

"doc_count": 15

}

]

}

},

{

"key_as_string": "2018-10-28",

"key": 1540684800000,

"doc_count": 56062,

"per_count": {

"doc_count_error_upper_bound": 0,

"sum_other_doc_count": 56047,

"buckets": [

{

"key": "1360660712",

"doc_count": 15

}

]

}

}

]

}

},

{

"key": "20",

"doc_count": 95391,

"month_num": {

"buckets": [

{

"key_as_string": "2018-10-27",

"key": 1540598400000,

"doc_count": 38552,

"per_count": {

"doc_count_error_upper_bound": 0,

"sum_other_doc_count": 38537,

"buckets": [

{

"key": "2722601793",

"doc_count": 15

}

]

}

},

{

"key_as_string": "2018-10-28",

"key": 1540684800000,

"doc_count": 56839,

"per_count": {

"doc_count_error_upper_bound": 0,

"sum_other_doc_count": 56824,

"buckets": [

{

"key": "1799231193",

"doc_count": 15

}

]

}

}

]

}

},

{

"key": "19",

"doc_count": 94108,

"month_num": {

"buckets": [

{

"key_as_string": "2018-10-27",

"key": 1540598400000,

"doc_count": 37687,

"per_count": {

"doc_count_error_upper_bound": 0,

"sum_other_doc_count": 37672,

"buckets": [

{

"key": "1750745673",

"doc_count": 15

}

]

}

},

{

"key_as_string": "2018-10-28",

"key": 1540684800000,

"doc_count": 56421,

"per_count": {

"doc_count_error_upper_bound": 0,

"sum_other_doc_count": 56406,

"buckets": [

{

"key": "1814872401",

"doc_count": 15

}

]

}

}

]

}

},

{

"key": "18",

"doc_count": 95298,

"month_num": {

"buckets": [

{

"key_as_string": "2018-10-27",

"key": 1540598400000,

"doc_count": 37882,

"per_count": {

"doc_count_error_upper_bound": 0,

"sum_other_doc_count": 37867,

"buckets": [

{

"key": "3933789681",

"doc_count": 15

}

]

}

},

{

"key_as_string": "2018-10-28",

"key": 1540684800000,

"doc_count": 57416,

"per_count": {

"doc_count_error_upper_bound": 0,

"sum_other_doc_count": 57401,

"buckets": [

{

"key": "1508661252",

"doc_count": 15

}

]

}

}

]

}

},

{

"key": "17",

"doc_count": 87719,

"month_num": {

"buckets": [

{

"key_as_string": "2018-10-27",

"key": 1540598400000,

"doc_count": 34590,

"per_count": {

"doc_count_error_upper_bound": 0,

"sum_other_doc_count": 34575,

"buckets": [

{

"key": "2038738841",

"doc_count": 15

}

]

}

},

{

"key_as_string": "2018-10-28",

"key": 1540684800000,

"doc_count": 53129,

"per_count": {

"doc_count_error_upper_bound": 0,

"sum_other_doc_count": 53114,

"buckets": [

{

"key": "2237799475",

"doc_count": 15

}

]

}

}

]

}

},

{

"key": "16",

"doc_count": 64030,

"month_num": {

"buckets": [

{

"key_as_string": "2018-10-27",

"key": 1540598400000,

"doc_count": 27736,

"per_count": {

"doc_count_error_upper_bound": 0,

"sum_other_doc_count": 27721,

"buckets": [

{

"key": "1911197885",

"doc_count": 15

}

]

}

},

{

"key_as_string": "2018-10-28",

"key": 1540684800000,

"doc_count": 36294,

"per_count": {

"doc_count_error_upper_bound": 0,

"sum_other_doc_count": 36279,

"buckets": [

{

"key": "1984810814",

"doc_count": 15

}

]

}

}

]

}

},

{

"key": "15",

"doc_count": 44697,

"month_num": {

"buckets": [

{

"key_as_string": "2018-10-27",

"key": 1540598400000,

"doc_count": 19721,

"per_count": {

"doc_count_error_upper_bound": 0,

"sum_other_doc_count": 19706,

"buckets": [

{

"key": "1564695515",

"doc_count": 15

}

]

}

},

{

"key_as_string": "2018-10-28",

"key": 1540684800000,

"doc_count": 24976,

"per_count": {

"doc_count_error_upper_bound": 0,

"sum_other_doc_count": 24961,

"buckets": [

{

"key": "1772563447",

"doc_count": 15

}

]

}

}

]

}

},

{

"key": "14",

"doc_count": 27055,

"month_num": {

"buckets": [

{

"key_as_string": "2018-10-27",

"key": 1540598400000,

"doc_count": 11754,

"per_count": {

"doc_count_error_upper_bound": 0,

"sum_other_doc_count": 11740,

"buckets": [

{

"key": "5054956136",

"doc_count": 14

}

]

}

},

{

"key_as_string": "2018-10-28",

"key": 1540684800000,

"doc_count": 15301,

"per_count": {

"doc_count_error_upper_bound": 0,

"sum_other_doc_count": 15286,

"buckets": [

{

"key": "1690639232",

"doc_count": 15

}

]

}

}

]

}

}

]

}

}

}

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

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

獲取多層聚合的數據api:

private boolean handleresaggWeibo(SearchResponse response,JSONObject result,JSONObject message) {

boolean flag = true;

JSONObject json = null;

List<String> jsons = null;

String timeinterval = message.getString("timeinterval");

//獲取聚合參數

Map<String, Aggregation> aggMap = response.getAggregations().asMap();

StringTerms gradeTerms = (StringTerms) aggMap.get("timeinterval");

Iterator<Bucket> gradeBucketIt = gradeTerms.getBuckets().iterator();

while(gradeBucketIt.hasNext()) {

jsons = new ArrayList<>();

Bucket gradeBucket = gradeBucketIt.next();

System.out.println(gradeBucket.getKey() + "時間段共有 " + gradeBucket.getDocCount() +"個文檔。");

StringTerms classTerms = (StringTerms) gradeBucket.getAggregations().asMap().get("weiboIds");

Iterator<Bucket> classBucketIt = classTerms.getBuckets().iterator();

int i = 0;

while(classBucketIt.hasNext()) {

Bucket classBucket = classBucketIt.next();

String pattern = "^\d+$";

String weiboid = classBucket.getKeyAsString();

Pattern p = Pattern.compile(pattern);

Matcher matcher = p.matcher(weiboid);

if (matcher.matches()) {

jsons.add(i, weiboid);

i++;

}

}

result.put(timeinterval, jsons);

result.put("flag", "1");

result.put("status", "返回列表成功");

flag = true;

}

return flag;

}

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

es內置的分詞器:

standard analyzer

simple analyzer

whitespace analyzer

language analyzer(特定的語言的分詞器)

例句:Set the shape to semi-transparent by calling set_trans(5)

不同分詞器的分詞結果:

standard analyzer:set, the, shape, to, semi, transparent, by, calling, set_trans, 5(默認的是standard)

simple analyzer:set, the, shape, to, semi, transparent, by, calling, set, trans

whitespace analyzer:Set, the, shape, to, semi-transparent, by, calling, set_trans(5)

language analyzer(特定的語言的分詞器,比如說,english,英語分詞器):set, shape, semi, transpar, call, set_tran, 5

分詞器測試:

GET /_analyze

{

"analyzer": "standard",

"text":"I love you"

}

1

2

3

4

5

結果:

{

"tokens": [

{

"token": "i",

"start_offset": 0,

"end_offset": 1,

"type": "<ALPHANUM>",

"position": 0

},

{

"token": "love",

"start_offset": 2,

"end_offset": 6,

"type": "<ALPHANUM>",

"position": 1

},

{

"token": "you",

"start_offset": 7,

"end_offset": 10,

"type": "<ALPHANUM>",

"position": 2

}

]

}

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

修改mapping:

1.創建索引:

PUT hui

1

2.刪除索引:

DELETE hui

1

3.mapping增加欄位

(Elasticsearch的mapping一旦創建,只能增加欄位,而不能修改已經mapping的欄位)

POST hui/News/_mapping

{

"News": {

"properties": {

"hui":{

"type": "text",

"store": true

}

}

}

}

1

2

3

4

5

6

7

8

9

10

11

4.修改:

POST hui/News/_mapping

{

"News": {

"properties": {

"hui":{

"type": "integer"

}

}

}

}

1

2

3

4

5

6

7

8

9

10

報錯:

{

"error": {

"root_cause": [

{

"type": "illegal_argument_exception",

"reason": "mapper [hui] of different type, current_type [text], merged_type [integer]"

}

],

"type": "illegal_argument_exception",

"reason": "mapper [hui] of different type, current_type [text], merged_type [integer]"

},

"status": 400

}

1

2

3

4

5

6

7

8

9

10

11

12

13

原因:

如果一個欄位的類型修改以後,那麼該欄位的所有數據都需要重新索引。Elasticsearch底層使用的是lucene庫,欄位類型修改以後索引和搜索要涉及分詞方式等操作,不允許修改類型在是符合lucene機制的

重構索引:

1.重建索引hui插入數據並設置別名:

PUT hui

POST hui/News/_mapping

{

"News": {

"properties": {

"hui":{

"type": "text",

"fields": {

"keyword": {

"type": "keyword",

"ignore_above": 256

}

}

}

}

}

}

POST hui/News/1

{

"hui" : "hehe"

}

POST hui/_alias/xiao

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

2.創建索引qiang並插入數據:

PUT qiang

POST qiang/News/_mapping

{

"News": {

"properties": {

"hui":{

"type": "text",

"fields": {

"keyword": {

"type": "keyword",

"ignore_above": 256

}

},

"store": true

}

}

}

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

3.開始執行重構索引命令:

POST _reindex

{

"source": {

"index": "hui"

},

"dest": {

"index": "qiang",

"version_type": "internal"

}

}

1

2

3

4

5

6

7

8

9

10

註:數據量大的時候會如下顯示連接超時,但是卻不影響功能實現。我重新導入了十九萬的數據大約用了十多分鐘

{

"statusCode": 504,

"error": "Gateway Timeout",

"message": "Client request timeout"

}

1

2

3

4

5

4.使用Task API查詢進度:

GET _tasks?detailed=true&actions=*reindex

{

"nodes": {

"yFpET0TETpuWGCxxyodXmg": {

"name": "yFpET0T",

"transport_address": "192.168.0.100:9300",

"host": "192.168.0.100",

"ip": "192.168.0.100:9300",

"roles": [

"master",

"data",

"ingest"

],

"attributes": {

"ml.max_open_jobs": "10",

"ml.enabled": "true"

},

"tasks": {

"yFpET0TETpuWGCxxyodXmg:6319552": {

"node": "yFpET0TETpuWGCxxyodXmg",

"id": 6319552,

"type": "transport",

"action": "indices:data/write/reindex",

"status": {

"total": 194111,

"updated": 0,

"created": 50000,

"deleted": 0,

"batches": 51,

"version_conflicts": 0,

"noops": 0,

"retries": {

"bulk": 0,

"search": 0

},

"throttled_millis": 0,

"requests_per_second": -1,

"throttled_until_millis": 0

},

"description": "reindex from [mei_toutiao] to [mei_toutiao_v2]",

"start_time_in_millis": 1532338516013,

"running_time_in_nanos": 176981696219,

"cancellable": true

}

}

}

}

}

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

5.如果複製完成則顯示:

{

"nodes": {}

}

1

2

3

6.別名轉換:

POST /_aliases

{

"actions": [

{ "remove": {

"alias": "xiao",

"index": "hui"

}},

{ "add": {

"alias": "xiao",

"index": "qiang"

}}

]

}

Elasticsearch的DSL操作命令大全

打開今日頭條,查看更多精彩圖片
喜歡這篇文章嗎?立刻分享出去讓更多人知道吧!

本站內容充實豐富,博大精深,小編精選每日熱門資訊,隨時更新,點擊「搶先收到最新資訊」瀏覽吧!


請您繼續閱讀更多來自 程序員小新人學習 的精彩文章:

數據增量同步 最佳實踐 數據集成(同步)用戶指南
獲取一個字元串值在指定字元串第n次出現的位置

TAG:程序員小新人學習 |