随记 elasticsearch

mysql自增主键在大量删除后如何重新设置避免断层
@ResponseBody的作用
@Transactional 注解,要么都成功要么都失败

bin/elasticsearch 启动elasticsearch
netstat -an | grep 9200 查看elasticsearch默认9200端口
bin/kibana 启动kibana
bin/elasticsearch -d 后台启动
tail -f dianping-app.log
kill -9
bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.3.0/elasticsearch-analysis-ik-7.3.0.zip

bin/logstash-plugin install logstash-input-jdbc

./logstash -f mysql/jdbc.conf

//增加 my.cnf配置
server-id = 1
binlog_format = ROW
log_bin = mysql_bin

show variables like ‘log_bin’; 开启logbin
mvn clean package -DskipTests

GET /shop/_search

//非结构化新建索引

PUT /movie
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 1
  },
  "mappings": {
    "properties": {
      "title":{"type":"text","analyzer": "english"},
      "tagline":{"type":"text","analyzer": "english"},
      "release_date":{"type":"date","format": "8yyyy/MM/dd||yyyy/M/dd||yyyy/MM/d||yyyy/M/d"},
      "popularity":{"type":"double"},
      "overview":{"type":"text","analyzer": "english"},
      "cast":{
        "type":"object",
        "properties": {
          "character":{"type":"text","analyzer":"standard"},
          "name":{"type":"text","analyzer":"standard"}
        }
      }
    }
  }
}

//搜索内容match

GET /movie/_search
{
  "query": {
    "match": {
      "title": "steve"
    }
  }
}

//term 查询 不进行分词分析,直接去索引内查询

GET /movie/_search
{
  "query": {
    "term":{"title":"steve zissou"}
  }
}

//分词后的and和or逻辑 match 默认使用的是or

GET /movie/_search
{
  "query":{
    "match": {
      "title": "basketball with cartoom aliens"
    }
  }
}

// 改成and

GET /movie/_search
{
  "query":{
    "match": {
      "title": "basketball with cartoom aliens",
      "operator": "and"
    }
  }
}

//最小词匹配项

GET /movie/_search
{
  "query":{
    "match": {
      "title": {
        "query":"basketball love aliens",
        "operator": "or",
        "minimum_should_match": 2
      }
    }
  }
}

//短语查询

GET /movie/_search
{
  "query": {
    "match": {
      "title": "steve zissou"
    }
  }  
}

GET /movie/_analyze
{
  "field": "title",
  "text":"steven zissou"
}

GET /movie/_search
{
  "explain": true, 
  "query": {
    "term":{"title":"steve"}
  }
}

#多字段查询

GET /movie/_search
{
  "query":{
    "multi_match": {
      "query": "basketball with cartoom aliens",
      "fields": ["title","overview"]
    }
  }
}

#优化多字段查询

GET /movie/_search
{
  "explain": true, 
  "query":{
    "multi_match": {
      "query": "basketball with cartoom aliens",
      "fields": ["title^10","overview"],
      "tie_breaker": 0.3
    }
  }
}

#bool查询
#must:必须都为true
#should:其中只有一个为true即可
#为true的越多则得分越高

GET /movie/_search
{
  "query": {
    "bool":{
      "should":[
          {"match":{"title":"basketball with cartoom aliens"}},
          {"match": {
            "overview": "basketball with cartoom aliens"}
          }
        ]
    }
  }
}

#不同的multi_query其实是有不同的type
#best_fields:默认的得分方式,取得最高的分数作为对应文档的对应分数,“最匹配模式”,dis_max

GET /movie/_search
{
  "explain": true,
  "query":{
    "multi_match": {
      "query": "basketball with cartoom aliens",
      "fields": ["title","overview"],
      "type":"best_fields"
    }
  }
}
GET /movie/_search
{
  "explain": true, 
  "query": {
    "dis_max":{
      "queries":[
          {"match":{"title":"basketball with cartoom aliens"}},
          {"match": {
            "overview": "basketball with cartoom aliens"}
          }
        ]
    }
  }
}

#most_fields:考虑绝大多数(所有的)文档的字段得分相加获得想要的结果

GET /movie/_search
{
  "explain": true,
  "query":{
    "multi_match": {
      "query": "basketball with cartoom aliens",
      "fields": ["title^10","overview^0.1"],
      "type":"most_fields"
    }
  }
}
GET /movie/_validate/query?explain
{
  "query": {
    "multi_match": {
      "query": "basketball with cartoom alien",
      "fields": ["title^10","overview^0.1"],
      "type":"most_fields"
    }
  }
}

#cross_fields:以分词为单位计算栏位的总分,适用于词导向的匹配

GET /movie/_search
{
  "explain": true,
  "query":{
    "multi_match": {
      "query": "steve job",
      "fields": ["title","overview"],
      "type":"cross_fields"
    }
  }
}
#query string 
#方便的利用 and or not
GET /movie/_search
{
  "query": {
    "query_string": {
      "fields":["title"],
      "query": "steve OR jobs"
    }
  }
}

#filter过滤查询
#单条件过滤

GET /movie/_search
{
  "query":{
    "bool":{
      "filter": {
        "term": {
          "title": "steve"
        }
      }
    }
  }
}

#多条件过滤

GET /movie/_search
{
  "query":{
    "bool":{
      "filter": [
        {"term":{"title":"steve"}},
        {"term":{"cast.name":"gaspard"}},
        {"range":{"release_date": {"lte":"2015/01/01"}}},
        {"range":{"popularity":{"gte":"25"}}}
        ]
    }
  },
  "sort":[
      {"popularity": {"order":"desc"}}
    ]
}
#带match打分的filter
GET /movie/_search
{
  "query": {
    "bool":{
      "must":[
        {"match":{"title":"Search"}},
        {"match": {
          "title": "ElasticSearch"
        }}
        ],
      "filter": [
        {"term":{"title":"steve"}}, 
        {"term":{"cast.name":"gaspard"}},
        {"range": { "release_date": { "lte": "2015/01/01" }}}, 
        {"range": { "popularity": { "gte": "25" }}}
        ]
    }
  }
}

#functionscore

GET /movie/_search
{
  "explain": true, 
  "query": {
    "function_score": {
      "query": {
        "multi_match": {
          "query": "steve job",
          "fields": ["title","overview"],
          "operator":"or",
          "type":"most_fields"
          }
      },
      "functions": [
        {
          "field_value_factor": {
            "field": "popularity",
            "modifier": "log2p",
            "factor": 10
          }
        },
        {
          "field_value_factor": {
            "field": "popularity"
            , "modifier": "log2p",
            "factor": 5
          }
        }
      ],
      "score_mode": "sum",
      "boost_mode": "sum" 
    }
  }
}

#测试ik分词器,智能分词法

GET _analyze?pretty
{
	"analyzer":"ik_smart",
	"text":"中华人民共和国国歌"
}

#最大化分词

GET _analyze?pretty
{
	"analyzer":"ik_max_word",
	"text":"中华人民共和国国歌"
}

#最佳实践:索引的时候使用max_word 查询的时候使用smartword

#创建shop索引

PUT /shop
{
  "settings" : { 
    "number_of_shards" : 1,        
    "number_of_replicas" : 1
  },
  "mappings": {
    "properties": {
      "id":{"type":"integer"},
      "name":{"type":"text","analyzer": "ik_max_word","search_analyzer":"ik_smart"}, 
      "tags":{"type":"text","analyzer": "whitespace","fielddata":true}, 
      "location":{"type":"geo_point"},
      "remark_score":{"type":"double"},
      "price_per_man":{"type":"integer"},
      "category_id":{"type":"integer"},
      "category_name":{"type":"keyword"},
      "seller_id":{"type":"integer"},
      "seller_remark_score":{"type":"double"},
      "seller_disabled_flag":{"type":"integer"}
    } 
  }
}
GET /shop/_search
{
  "query":{
    "multi_match": {
      "query": "凯悦",
      "fields": ["name"]
    }
  }
}

GET /shop/_analyze
{
  "analyzer": "ik_smart",
  "text":"凯悦"
}

GET /shop/_analyze
{
  "analyzer": "ik_max_word",
  "text":"花悦庭果木烤鸭"
}

#带上距离字段
GET /shop/_search
{
  "query": {
    "match": {
      "name": "凯悦"
    }
  },
  "_source":"*",
  "script_fields": {
    "distance":{
      "script":{
        "source":"haversin(lat,lon,doc['location'].lat,doc['location'].lon)",
        "lang":"expression",
        "params":{"lat":31.37,"lon":127.12}
      }
    }
  }
}
#排序
GET /shop/_search
{
  "query": {
    "match": {
      "name": "凯悦"
    }
  },
  "_source":"*",
  "script_fields": {
    "distance":{
      "script":{
        "source":"haversin(lat,lon,doc['location'].lat,doc['location'].lon)",
        "lang":"expression",
        "params":{"lat":31.37,"lon":127.12}
      }
    }
  },
  "sort":[
    {
      "_geo_distance": {
        "location": {
          "lat": 31.37,
          "lon": 127.12
        },
        "order": "asc",
        "unit":"km",
        "distance_type": "arc"
      }
    }
    ]
}

#使用 function score 解决排序模型
#加入酒店类目过滤
GET /shop/_search
{
  "_source":"*",
  "script_fields": {
    "distance":{
      "script":{
        "source":"haversin(lat,lon,doc['location'].lat,doc['location'].lon)",
        "lang":"expression",
        "params":{"lat":31.23916171,"lon":121.48789949}
      }
    }
  },
  "query":{
    "function_score": {
      "query": {
        "bool":{
          "must":[
            {"match":{"name":{"query":"凯悦","boost":"0.1"}}},
            {"term":{"seller_disabled_flag": 0}},
           {"term":{"category_id":2}}
          ]
        }
      },
      "functions": [
        {
          "gauss": {
            "location": {
              "origin": "31.23916171,121.48789949",
              "scale": "100km",
              "offset":"0km",
              "decay":0.5
            }
          },
          "weight": 9
        },
        {
          "field_value_factor": {
            "field": "remark_score"
          },
          "weight": 0.2
        },
        {
          "field_value_factor": {
            "field": "seller_remark_score"
          },
          "weight": 0.2
        }
      ],
      "score_mode": "sum",
      "boost_mode": "sum"
    }
  },
  "sort": [
    {
      "_score": {
        "order": "desc"
      }
    }
  ],
  "aggs":{
    "group_by_tags":{
      "terms":{
        "field":"tags"
      }
    }
  }
}
#加入同义词
PUT /shop
{
  "settings" : { 
    "number_of_shards" : 1,        
    "number_of_replicas" : 1,
    "analysis":{
      "filter":{
        "my_synonym_filter":{
          "type":"synonym",
          "synonyms_path":"analysis-ik/synonyms.txt"
        }
      },
      "analyzer":{
        "ik_syno":{
          "type":"custom",
          "tokenizer":"ik_smart",
          "filter":["my_synonym_filter"]
        },
        "ik_syno_max":{
          "type":"custom",
          "tokenizer":"ik_max_word",
          "filter":["my_synonym_filter"]
        }
      }
    }
  },
  
  "mappings": {
    "properties": {
      "id":{"type":"integer"},
      "name":{"type":"text","analyzer": "ik_syno_max","search_analyzer":"ik_syno"}, 
      "tags":{"type":"text","analyzer": "whitespace","fielddata":true}, 
      "location":{"type":"geo_point"},
      "remark_score":{"type":"double"},
      "price_per_man":{"type":"integer"},
      "category_id":{"type":"integer"},
      "category_name":{"type":"keyword"},
      "seller_id":{"type":"integer"},
      "seller_remark_score":{"type":"double"},
      "seller_disabled_flag":{"type":"integer"}
    } 
  }
}

GET /shop/_search
{
  "query": {
    "match": {
      "name": "锡伯"
    }
  }
}

GET /shop/_analyze

POST /shop/_update_by_query
{
  "query":{
    "bool":{
      "must":[
        {"term":{"name":"凯"}},
        {"term":{"name":"悦"}}
      ]
    }
  }
}

GET /shop/_analyze
{
  "field": "name",
  "text": "凯悦"
}

GET /shop/_analyze
{
  "analyzer": "ik_max_word",
  "text": "凯悦"
}
#引入分类
GET /shop/_search
{
  "_source":"*",
  "script_fields": {
    "distance":{
      "script":{
        "source":"haversin(lat,lon,doc['location'].lat,doc['location'].lon)",
        "lang":"expression",
        "params":{"lat":31.23916171,"lon":121.48789949}
      }
    }
  },
  "query":{
    "function_score": {
      "query": {
        "bool":{
          "must":[
            {
              "bool": {
                "should": [
                  {"match":{"name":{"query":"住宿","boost":"0.1"}}},
                  {"term":{"category_id": {"value":2,"boost":0.1}}}
                ]
              }
            },
            {"term":{"seller_disabled_flag": 0}}
          ]
        }
      },
      "functions": [
        {
          "gauss": {
            "location": {
              "origin": "31.23916171,121.48789949",
              "scale": "100km",
              "offset":"0km",
              "decay":0.5
            }
          },
          "weight": 9
        },
        {
          "field_value_factor": {
            "field": "remark_score"
          },
          "weight": 0.2
        },
        {
          "field_value_factor": {
            "field": "seller_remark_score"
          },
          "weight": 0.2
        }
      ],
      "score_mode": "sum",
      "boost_mode": "sum"
    }
  },
  "sort": [
    {
      "_score": {
        "order": "desc"
      }
    }
  ],
  "aggs":{
    "group_by_tags":{
      "terms":{
        "field":"tags"
      }
    }
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值