elasticsearch mapping

curl -XPUT localhost:9200/local //建立一个索引库名为local的库
//设置索引库的mapping
curl -XPUT localhost: 9200 /local -d '{
     "settings"  : {
         "analysis"  : {
             "analyzer"  : {
                 "stem"  : {
                     "tokenizer"  : "standard" ,
                     "filter"  : [ "standard" , "lowercase" , "stop" , "porter_stem" ]
                 }
             }
         }
     },
     "mappings"  : {
         "article"  : {
             "dynamic"  : true ,
             "properties"  : {
                 "title"  : {
                     "type"  : "string" ,
                     "analyzer"  : "stem"
                 }
             }
         }
     }
}'
  
# Sample Analysis
curl -XGET localhost: 9200 /local/_analyze?analyzer=stem -d '{Fight for your life}'
curl -XGET localhost: 9200 /local/_analyze?analyzer=stem -d '{Bruno fights Tyson tomorrow}'
  
# Index Data
curl -XPUT localhost: 9200 /local/article/ 1  -d '{"title": "Fight for your life"}'
curl -XPUT localhost: 9200 /local/article/ 2  -d '{"title": "Fighting for your life"}'
curl -XPUT localhost: 9200 /local/article/ 3  -d '{"title": "My dad fought a dog"}'
curl -XPUT localhost: 9200 /local/article/ 4  -d '{"title": "Bruno fights Tyson tomorrow"}'
  
# search on the title field, which is stemmed on index and search
curl -XGET localhost: 9200 /local/_search?q=title:fight
  
# searching on _all will not do  anystemming, unless also configured on the mapping to be stemmed...
curl -XGET localhost: 9200 /local/_search?q=fight

  

 

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
curl -XPUT http: //localhost:9200/test_index/ -d '
{
     "index" : {
         "analysis" : {
             "analyzer" : {
                 "index_analyzer" : {
                     "tokenizer" : "nGram" ,
                     "filter" : [ "lowercase" , "snowball" ]
                 },
                 "search_analyzer" : {
                     "tokenizer" : "nGram" ,
                     "filter" : [ "lowercase" , "snowball" ]
                 }
             },
             "filter" : {
                 "snowball" : {
                     "type" : "snowball" ,
                     "language" : "English"
                 }
             }
         }
     }
}'
  
{
     "item" : {
       "properties" : {
         "title" : {
           "type" : "string" ,
           "boost" : 2.0 ,
           "index" : "analyzed" ,
           "store" : "yes" ,
           "term_vector"  : "with_positions_offsets"
         },
         "description" : {
           "type" : "string" ,
           "boost" : 1.0 ,
           "index" : "analyzed" ,
           "store" : "yes" ,
           "term_vector"  : "with_positions_offsets"
         },
         "link" : {
           "type" : "string"
         }
       }
     }
}'

  

 

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
# curl -XDELETE http: //localhost:9200/test-index
  
# "analyzer" . "default"  => default  name for  index and search
# "tokenizer"  : "standard"  => splits words at punctuation characters
# http: //www.elasticsearch.org/guide/reference/index-modules/analysis/
  
curl -XPUT http: //localhost:9200/test-index/ -d '
{
     "index" : {
         "analysis" : {
             "analyzer" : {
                 "default" : {
                     "tokenizer" : "standard" ,
                     "filter" : [ "lowercase" , "snowball" ]
                 }
             }
         }
     }
}'
  
  
# http: //www.elasticsearch.org/guide/reference/api/search/highlighting.html
# "store" : "yes"  => enable highlighting
# "term_vector"  : "with_positions_offsets"  => for  performance
  
curl -XPUT http: //localhost:9200/test-index/test-item/_mapping -d '
{
     "test-item" : {
         "properties" : {
             "name" : {
                 "type" : "string" ,
                 "store" : "yes" ,
                 "term_vector" : "with_positions_offsets"
             },
             "description" : {
                 "type" : "string" ,
                 "store" : "yes" ,
                 "term_vector" : "with_positions_offsets"
             },
             "field-without-highlighting" : {
                 "type" : "string"
             }
         }
     }
}'
  
  
curl -XPUT http: //localhost:9200/test-index/test-item/1 -d '
{
     "name" : "Example One" ,
     "description" : "Create a test item for to the index." ,
     "field-without-highlighting" : "test"
}'
  
  
# "highlight"  => wrap search result in <em> tags (define own tags with pre_tags/post_tags)
# "number_of_fragments" : 0  => don"t split field in multiple fragments
# http: //www.elasticsearch.org/guide/reference/api/search/highlighting.html
  
curl -XGET http: //localhost:9200/test-index/test-item/_search?pretty=true -d '
{
     "highlight" : {
         "fields" : {
             "name" : {
                 "number_of_fragments" : 0
             },
             "description" : {
                 "number_of_fragments" : 0
             }
         }
     },
     "query" : {
         "query_string" : {
             "fields" : [ "name" , "description" ],
             "query" : "test"
         }
     }
}'
  
=>
  
{
     "took" : 4 ,
     "timed_out" : false ,
     "_shards" : {
         "total" : 5 ,
         "successful" : 5 ,
         "failed" : 0
     },
     "hits" : {
         "total" : 1 ,
         "max_score" : 0.029424578 ,
         "hits" : [{
             "_index" : "test-index" ,
             "_type" : "test-item" ,
             "_id" : "1" ,
             "_score" : 0.029424578 ,
             "_source" : {
                 "name" : "Example One" ,
                 "description" : "Create a test item for to the index." ,
                 "field-without-highlighting" : "test"
             },
             "highlight" : {
                 "description" : [ "Create a <em>test</em> item for to the index." ]
             }
         }]
     }
}
  
curl -XGET http: //localhost:9200/test-index/test-item/_search?pretty=true -d '
{
     "highlight" : {
         "fields" : {
             "name" : {
                 "number_of_fragments" : 0
             },
             "description" : {
                 "number_of_fragments" : 0
             }
         }
     },
     "query" : {
         "query_string" : {
             "fields" : [ "name" , "description" ],
             "query" : "created EXAMPLES"
         }
     }
}'
  
=>
  
{
     "took" : 6 ,
     "timed_out" : false ,
     "_shards" : {
         "total" : 5 ,
         "successful" : 5 ,
         "failed" : 0
     },
     "hits" : {
         "total" : 1 ,
         "max_score" : 0.06241896 ,
         "hits" : [{
             "_index" : "test-index" ,
             "_type" : "test-item" ,
             "_id" : "1" ,
             "_score" : 0.06241896 ,
             "_source" : {
                 "name" : "Example One" ,
                 "description" : "Create a test item for to the index." ,
                 "field-without-highlighting" : "test"
             },
             "highlight" : {
                 "description" : [ "<em>Create</em> a test item for to the index." ],
                 "name" : [ "<em>Example</em> One" ]
             }
         }]
     }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值