前缀搜索
prefix query不计算relevance score,与prefix filter唯一的区别就是,filter会cache bitset
性能比较差
<code># 创建mapping
PUT my_index
{
"mappings": {
"properties": {
"title": {
"type":"keyword"
}
}
}
}
# 添加数据
POST /my_index/_doc/1
{
"title":"C3D0-KD345"
}
POST /my_index/_doc/2
{
"title":"C3K5-DFG65"
}
POST /my_index/_doc/3
{
"title":"C4I8-UI365"
}
# 查询
GET /my_index/_search
{
"query": {
"prefix": {
"title": {
"value": "C3"
}
}
}
}
/<code>
通配符搜索 & 正则匹配
与prefix类似性能比较差
<code># 通配符搜索
GET /my_index/_search
{
"query": {
"wildcard": {
"title": {
"value": "C?K*5"
}
}
}
}
# 正则匹配
GET /my_index/_search
{
"query": {
"regexp": {
"title": "C[0-9].+"
}
}
}
/<code>
搜索推荐
<code># 更新数据
DELETE /my_index
PUT /my_index/_doc/1
{
"title":"hello world"
}
PUT /my_index/_doc/2
{
"title":"hello dog"
}
PUT /my_index/_doc/3
{
"title":"hello we"
}
PUT /my_index/_doc/4
{
"title":"hello wind"
}
# 查询
GET /my_index/_search
{
"query": {
"match_phrase_prefix": {
"title": "hello"
}
}
}
/<code>
ngram分词
<code># 更新数据
DELETE /my_index
PUT /my_index
{
"settings": {
"analysis": {
"filter": {
"autocomplete_filter": {
"type": "edge_ngram",
"min_gram": 1,
"max_gram": 20
}
},
"analyzer": {
"autocomplete": {
"type": "custom",
"tokenizer": "standard",
"filter": [
"lowercase",
"autocomplete_filter"
]
}
}
}
}
}
GET /my_index/_analyze
{
"analyzer": "autocomplete",
"text": "quick brown"
}
PUT /my_index/_mapping
{
"properties": {
"title": {
"type": "text",
"analyzer": "autocomplete",
"search_analyzer": "standard"
}
}
}
PUT /my_index/_doc/1
{
"title":"hello world"
}
PUT /my_index/_doc/2
{
"title":"hello dog"
}
PUT /my_index/_doc/3
{
"title":"hello we"
}
PUT /my_index/_doc/4
{
"title":"hello wind"
}
GET /my_index/_search
{
"query": {
"match_phrase": {
"title": "hello w"
}
}
}
/<code>
nagative boost
<code>
GET /forum/_search
{
"query": {
"boosting": {
"positive": { # 正序
"match": {
"content": "java"
}
},
"negative": { # 反序
"match": {
"content": "spark"
}
},
"negative_boost": 0.2
}
}
}
/<code>
function_score
<code>
POST /forum/_bulk
{ "update": { "_id": "1"} }
{ "doc" : {"follower_num" : 5} }
{ "update": { "_id": "2"} }
{ "doc" : {"follower_num" : 10} }
{ "update": { "_id": "3"} }
{ "doc" : {"follower_num" : 25} }
{ "update": { "_id": "4"} }
{ "doc" : {"follower_num" : 3} }
{ "update": { "_id": "5"} }
{ "doc" : {"follower_num" : 60} }
GET /forum/_search
{
"query": {
"function_score": {
"query": {
"multi_match": {
"query": "java spark",
"fields": ["title","content"]
}
},
"field_value_factor": {
"field": "follower_num",
"modifier": "log1p",
"factor": 0.5
},
"boost_mode": "sum",
"max_boost": 2
}
}
}
# boost_mode,可以决定分数与指定字段的值如何计算,multiply,sum,min,max,replace
# max_boost,限制计算出来的分数不要超过max_boost指定的值
/<code>
fuzzy搜索
<code>POST /my_index/_bulk
{ "index": { "_id": 1 }}
{ "text": "Surprise me!"}
{ "index": { "_id": 2 }}
{ "text": "That was surprising."}
{ "index": { "_id": 3 }}
{ "text": "I wasn't surprised."}
GET /my_index/_search
{
"query": {
"fuzzy": {
"text": {
"value": "surprize",
"fuzziness": 2
}
}
}
}
# 一般使用语法如下
GET /my_index/_search
{
"query": {
"match": {
"text": {
"query": "SURPIZE ME",
"fuzziness": "AUTO",
"operator": "and"
}
}
}
}
/<code>
閱讀更多 碼代碼 的文章
關鍵字: ElasticSearch