elasticsearch简单配置

全新基于elasticsearch的搜索API
使用Laravel

1
ssh://git@127.0.0.1:9922/apitmt2.git

1.INDEX创建

1
curl -xput 'http://localhost:9200/tmtpost'

2.创建users document的mapping

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
curl -xput 'http://localhost:9200/tmtpost/users/_mapping'

'{
"users": {
"properties": {
"name": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word",
"include_in_all": "true",
"boost": 8
},
"bio": {
"type": "text",
"analyzer": "ik_smart",
"search_analyzer": "ik_smart",
"include_in_all": "true",
"boost": 8
}
}
}
}'

3.创建posts document的mapping

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
curl -xput 'http://localhost:9200/tmtpost/posts/_mapping'

'{
"users": {
"properties": {
"title": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word",
"include_in_all": "true",
"boost": 8
},
"summary": {
"type": "text",
"analyzer": "ik_smart",
"search_analyzer": "ik_smart",
"include_in_all": "true",
"boost": 8
}
}
}
}'

4.导入数据

1
2
3
4
5
6
7
8
9
10
11
12
13
curl -xput 'http://localhost:9200/tmtpost/users/1'

'{
"name": "阿Jo",
"bio": "他是一个好人"
}'

curl -xput 'http://localhost:9200/tmtpost/users/2'

'{
"name": "王老王",
"bio": "他是一个坏人"
}'

5.导入数据

1
2
3
4
5
6
curl -xput 'http://localhost:9200/tmtpost/posts/1'

'{
"title": "阿Jo",
"summary": "他是一个好人"
}'

6.测试搜索

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
curl -xpost 'http://localhost:9200/tmtpost/users/_search'  or  curl -xpost 'http://localhost:9200/tmtpost/_search'

'{
"query": {
"match": {
"bio": "一个"
}
},
"highlight": {
"pre_tags": [
"<tag1>",
"<tag2>"
],
"post_tags": [
"</tag1>",
"</tag2>"
],
"fields": {
"bio": {}
}
}
}'

{
"query": {
"multi_match": {
"query": "一个",
"type": "most_fields",
"fields": [ "title", "name", "bio", "summary" ]
}
}
}

7.查询结果

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
{
"took": 13,
"timed_out": false,
"_shards": {
"total": 2,
"successful": 2,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 2.025282,
"hits": [{
"_index": "tmtpost",
"_type": "users",
"_id": "1",
"_score": 2.025282,
"_source": {
"name": "阿Jo",
"bio": "他是一个好人"
}
},
{
"_index": "tmtpost",
"_type": "posts",
"_id": "1",
"_score": 2.025282,
"_source": {
"title": "阿Jo",
"summary": "他是一个好人"
}
}
]
}
}