Git Product home page Git Product logo

springboot-elasticsearch-example's Introduction

springboot + elasticsearch + kafka 学习实例

本地安装启动elasticsearch和kafka 使用docker方式:

elasticsearch

参考:https://www.elastic.co/guide/en/elasticsearch/reference/6.4/docker.html

  • docker pull docker.elastic.co/elasticsearch/elasticsearch:6.2.4
  • docker run -d -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:6.2.4

启动时报错,执行: sysctl -w vm.max_map_count=262144

kafka

  • docker pull wurstmeister/kafka
  • docker run -d -p 2181:2181 -p 9092:9092 spotify/kafka

elasticsearch的CRUD

查看版本:

curl -XGET http://localhost:9200

返回:

{
  "name" : "zDOs--s",
  "cluster_name" : "docker-cluster",
  "cluster_uuid" : "LgMm46NWSAqOoEnZKCMkNg",
  "version" : {
    "number" : "6.2.4",
    "build_hash" : "ccec39f",
    "build_date" : "2018-04-12T20:37:28.497551Z",
    "build_snapshot" : false,
    "lucene_version" : "7.2.1",
    "minimum_wire_compatibility_version" : "5.6.0",
    "minimum_index_compatibility_version" : "5.0.0"
  },
  "tagline" : "You Know, for Search"
}

创建index mapping:

curl -XPUT -H "Content-Type:application/json" http://localhost:9200/product_index -d '

{
    "mappings": {
        "_doc": {
            "properties": {
                "brandId": {
                    "type": "long"
                },
                "categoryId": {
                    "type": "long"
                },
                "categoryIdPath": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "categoryId_path": {
                    "type": "keyword"
                },
                "currency": {
                    "type": "keyword"
                },
                "detail": {
                    "type": "text"
                },
                "inventoryInfos": {
                    "type": "nested",
                    "properties": {
                        "itemQuantities": {
                            "type": "nested",
                            "properties": {
                                "itemId": {
                                    "type": "long"
                                },
                                "quantity": {
                                    "type": "integer"
                                }
                            }
                        },
                        "warehouseId": {
                            "type": "long"
                        }
                    }
                },
                "keyword": {
                    "type": "text"
                },
                "language": {
                    "type": "keyword"
                },
                "productId": {
                    "type": "long"
                },
                "productModel": {
                    "type": "keyword"
                },
                "productName": {
                    "type": "text"
                },
                "providerId": {
                    "type": "long"
                },
                "skuInfos": {
                    "properties": {
                        "createTime": {
                            "type": "date",
                            "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
                        },
                        "discount": {
                            "type": "double"
                        },
                        "offlineTime": {
                            "type": "date",
                            "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
                        },
                        "onlineTime": {
                            "type": "date",
                            "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
                        },
                        "retailPrice": {
                            "type": "double"
                        },
                        "salesVolume": {
                            "type": "integer"
                        },
                        "selectedShops": {
                            "type": "long"
                        },
                        "selectingShops": {
                            "type": "long"
                        },
                        "sku": {
                            "type": "keyword"
                        },
                        "skuId": {
                            "type": "long"
                        },
                        "status": {
                            "type": "integer"
                        },
                        "tagPrice": {
                            "type": "double"
                        },
                        "unselectingShops": {
                            "type": "long"
                        }
                    }
                },
                "status": {
                    "type": "integer"
                }
            }
        }
    }
}

'

查看结果:

curl -XGET http://localhost:9200/product_index/_doc/_mapping?pretty

修改index设置(否则插入数据时会报错:FORBIDDEN/12/index-read-only):

curl -XPUT -H "Content-Type:application/json" http://localhost:9200/product_index/_settings -d '

{
    "index": {
        "blocks": {
            "read_only_allow_delete": "false"
        }
    }
}

'

插入一条数据:

curl -XPOST -H "Content-Type:application/json" http://localhost:9200/product_index/_doc/228878077351166418 -d '

{
  "productId": 228878077351166418,
  "productModel": "PSKU-icecream2072500032",
  "productName": "3D printing flower Korean version glossy women non-slip sandals sliper indoors/outdoor flip-flop",
  "brandId": 220923510746775563,
  "categoryId": 228525066177937409,
  "categoryIdPath": "228525066177937409",
  "providerId": 200269873509105689,
  "language": "english",
  "currency": "USD",
  "status": 1,
  "detail": null,
  "keyword": null,
  "skuInfos": [
    {
      "skuId": 228878077556687331,
      "sku": "SKU-icecream2072500032",
      "tagPrice": 1000,
      "retailPrice": 900,
      "status": 1,
      "discount": 0,
      "createTime": "2018-07-25 13:59:46",
      "onlineTime": "2018-07-25 14:42:54",
      "offlineTime": null,
      "salesVolume": 0
    }
  ]
}

'

查询:

curl -XGET http://localhost:9200/product_index/_doc/228878077351166418?pretty

##Elasticsearch查询:

Query Name Functions Sample Query Matching Text Not Matching Text
term
range
wildcard 通配符查询,支持的通配符*(匹配任意个字符)和?(匹配一个字符),比较慢,不应该使用*或?开头
regexp 正则表达式匹配
fuzzy 计算字符串相似度算法(Levenshtein)
ids 根据_uid字段查询
match
match
match

springboot-elasticsearch-example's People

Contributors

zhhongcai avatar

Watchers

 avatar

Forkers

nifengfeiyang

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.