Coder Social home page Coder Social logo

elasticsearch's Introduction

ElasticSearch

在laravel框架下写的model

在config目录下增加 elasticsearch.php配置文件,内容如下

<?php
return [
    'host'=>env('ELASTICSEARCH_HOST','127.0.0.1'),
    'port'=>9200
];

创建索引

$rs = Elastic::db()->createIndex([
            'index'=>'testdb',
            'type'=>'news',
            'properties'=>[
                'id'=>[
                    'type'=>'long',
                ],
                'theme_id'=>[
                    'type'=>'long',
                ],
                'cat_unique_name'=>[
                    'type'=>"text",
                ],
                'title'=>[
                    'type'=>'text',
                    'analyzer'=>'ik_max_word',
                    'search_analyzer'=>'ik_max_word',
                ],
                'summary'=>[
                    'type'=>'text',
                    'analyzer'=>'ik_max_word',
                    'search_analyzer'=>'ik_max_word',
                ],
                'keywords'=>[
                    'type'=>'text',
                    'analyzer'=>'ik_max_word',
                    'search_analyzer'=>'ik_max_word',
                ],
                'unique_name'=>[
                    'type'=>'text',
                ],
                'html'=>[
                    'type'=>'text',
                    'analyzer'=>'ik_max_word',
                    'search_analyzer'=>'ik_max_word',
                ]
            ]
        ]);

dd($rs);

Result:

array:2 [▼
  "status" => 1
  "data" => array:3 [▼
    "acknowledged" => true
    "shards_acknowledged" => true
    "index" => "testdb"
  ]
]

删除索引

Elastic::db()->index('testdb')->deleteIndex();
#或者
Elastic::db('testdb')->deleteIndex();

搜索

$rs = Elastic::db('theme')->type('article')->search([
            'query'=>[
                'match'=>['title'=>'git']
            ],
            'size'=>10
        ]);
$rs = Elastic::db('theme')->type('article')->search([
            'query'=>[
                "bool"=>[
                    'must'=>[
                        "term"=>["title"=>"redis"]
                    ],
                    "must_not"=>[
                        "term"=>["keywords"=>"get"]
                    ]
                ]
            ],
            'size'=>10
        ]);
$rs = Elastic::db('theme')->type('article')->search([
            'query'=>[
                "bool"=>[
                    'should'=>[
                        "match"=>["title"=>"redis"]
                    ],
                    "should"=>[
                        "match"=>["html"=>"redis"]
                    ]
                ]
            ],
            'size'=>10
        ]);

Result 搜索结果以分页形式返回

dump($rs->items()); #数据内容
dump($rs->links()); #分页

elasticsearch's People

Contributors

black-bow avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.