Uninote
Uninote
用户根目录
每日点滴
 mynote es elasticsearch
mynote es elasticsearch
http://uninote.com.cn/book/1082456099#1434
params.php:
'es_host' => '127.0.0.1',
'es_port' => '9200',
'es_index' => 'mynote',
\app\controllers\api\ArticleController::actionSearch
\app\controllers\SearchController::actionIndex
 \app\models\api\Search::index
增删改查核心
E:\uninote\mynote\basic\models\ESBase.php

note

搜索时,既要从数据库搜索tag等,又要从es搜索文章。

es commits

SHA-1: 2e2145fd7b10e856b012b0a8a051bb7ec6df3a94
* search: 完成文章搜索

索引管理

访问此链接,建立文章索引
http://uninote.com.cn/api/synes
删除index:
DELETE /mynote
清空es:
POST http://[域名]:9200/[es数据库名称]/article/_delete_by_query?refresh&slices=5&pretty 发送 
{
  "query": {
    "match_all": {}
  }
}

create mapping

http://uninote.com.cn/book/1079089832#1116
PUT /mynote
{
    "settings": {
        "analysis": {
            "analyzer": {
                "ik_pinyin_analyzer": {
                    "type":"custom",
                    "tokenizer": "ik_smart",
                    "filter": ["my_pinyin","word_delimiter"]
                }
            },
            "filter": {
                "my_pinyin": {
                  "keep_joined_full_pinyin": "true",
                  "keep_none_chinese_in_first_letter": "false",
                  "lowercase": "true",
                  "keep_original": "false",
                  "keep_first_letter": "false",
                  "trim_whitespace": "true",
                  "type": "pinyin",
                  "keep_none_chinese": "false",
                  "limit_first_letter_length": "16",
                  "keep_full_pinyin": "false"
                }
            }
        }
    },
    "mappings":{
        "article":{
            "properties":{
                "abstract":{
                    "type":"text",
                    "analyzer":"ik_max_word"
                },
                "article_id":{
                    "type":"keyword"
                },
                "content":{
                    "type":"text",
                    "fields":{
                        "pinyin":{
                            "type":"text",
                            "analyzer":"ik_pinyin_analyzer"
                        }
                    },
                    "analyzer":"ik_max_word"
                },
                "id":{
                    "type":"long"
                },
                "recommend":{
                    "type":"byte"
                },
                "sort":{
                    "type":"long"
                },
                "status":{
                    "type":"byte"
                },
                "tag":{
                    "type":"keyword"
                },
                "thum":{
                    "type":"text"
                },
                "time":{
                    "type":"date"
                },
                "title":{
                    "type":"text",
                    "fields":{
                        "keyword":{
                            "type":"keyword"
                        },
                        "pinyin":{
                            "type":"text",
                            "analyzer":"ik_pinyin_analyzer"
                        }
                    },
                    "analyzer":"ik_max_word"
                },
                "updata_time":{
                    "type":"date"
                },
                "username":{
                    "type":"text",
                    "fields":{
                        "keyword":{
                            "type":"keyword"
                        },
                        "pinyin":{
                            "type":"text",
                            "analyzer":"ik_pinyin_analyzer"
                        }
                    },
                    "analyzer":"ik_max_word"
                }
            }
        }
    }
}

es 查询

全文搜索 normal

GET /mynote/article/_search
{
  
  "query": {
    "match": {
      "content": "创建完成"
    }
  }
}

实际使用的查询

GET /mynote/article/_search
{
            "query": {
                "dis_max": {
                    "tie_breaker": 0.7,
                    "boost": 1.2,
                    "queries": [
                        {
                            "bool": {
                                "should": [
                                    {
                                        "multi_match": {
                                            "query": "日志",
                                            "fields": ["content", "content.pinyin", "username", "username.pinyin", "title","title.pinyin"],
                                            "analyzer":"ik_max_word"
                                        }
                                    },
                                    {
                                        "term": {
                                            "tag": {
                                                "value": "123"
                                            }
                                        }
                                    }
                                ]
                            }
                        }
                    ]
                }
            },
            "highlight": {
                "fields": [
                    {"tag" :{}},
                    {"title" :{}},
                    {"content" :{}},
                    {"username" :{}},
                    {"title.pinyin" :{}},
                    {"content.pinyin" :{}},
                    {"username.pinyin" :{}}
                ]
            },
            "size": 10,
            "from": 0,
            "_source":[
                "highlight", "title", "article_id", "title", "abstract", "thum", "id"
            ]
        }

mynote_dev_log

mynote_init

点赞(0) 阅读(218) 举报
目录
标题