美文网首页
2020-06-26

2020-06-26

作者: 规约 | 来源:发表于2020-06-26 00:43 被阅读0次

    1.增加文档

    PUT /customer/_doc/1
    {
      "name": "John Doe"
    }
    

    这个请求会自动创建customer,如果这个索引不存在,增加一个新的,名为1的id的文档,并且储存和索引这个name字段,并且会返回一下结果

    {
        "_index": "customer",
        "_type": "_doc",
        "_id": "1",
        "_version": 3,
        "result": "updated",
        "_shards": {
            "total": 2,
            "successful": 1,
            "failed": 0
        },
        "_seq_no": 2,
        "_primary_term": 1
    }
    

    2.查询

    GET /customer/_doc/1
    

    这个请求会返回指定的id,以及源字段

    {
      "_index" : "customer",
      "_type" : "_doc",
      "_id" : "1",
      "_version" : 1,
      "_seq_no" : 26,
      "_primary_term" : 4,
      "found" : true,
      "_source" : {
        "name": "John Doe"
      }
    }
    

    3.搜索

    (1) 全部匹配

    GET /bank/_search
    {
      "query": { "match_all": {} },
      "sort": [
        { "account_number": "asc" }
      ]
    }
    

    默认情况下,会返回10条数据

    {
        "took": 11,
        "timed_out": false,
        "_shards": {
            "total": 1,
            "successful": 1,
            "skipped": 0,
            "failed": 0
        },
        "hits": {
            "total": {
                "value": 1000,
                "relation": "eq"
            },
            "max_score": null,
            "hits": [
                {
                    "_index": "bank",
                    "_type": "_doc",
                    "_id": "10",
                    "_score": null,
                    "_source": {
                        "account_number": 10,
                        "balance": 46170,
                        "firstname": "Dominique",
                        "lastname": "Park",
                        "age": 37,
                        "gender": "F",
                        "address": "100 Gatling Place",
                        "employer": "Conjurica",
                        "email": "dominiquepark@conjurica.com",
                        "city": "Omar",
                        "state": "NJ"
                    },
                    "sort": [
                        10
                    ]
                },
                {
                    "_index": "bank",
                    "_type": "_doc",
                    "_id": "11",
                    "_score": null,
                    "_source": {
                        "account_number": 11,
                        "balance": 20203,
                        "firstname": "Jenkins",
                        "lastname": "Haney",
                        "age": 20,
                        "gender": "M",
                        "address": "740 Ferry Place",
                        "employer": "Qimonk",
                        "email": "jenkinshaney@qimonk.com",
                        "city": "Steinhatchee",
                        "state": "GA"
                    },
                    "sort": [
                        11
                    ]
                },
                {
                    "_index": "bank",
                    "_type": "_doc",
                    "_id": "12",
                    "_score": null,
                    "_source": {
                        "account_number": 12,
                        "balance": 37055,
                        "firstname": "Stafford",
                        "lastname": "Brock",
                        "age": 20,
                        "gender": "F",
                        "address": "296 Wythe Avenue",
                        "employer": "Uncorp",
                        "email": "staffordbrock@uncorp.com",
                        "city": "Bend",
                        "state": "AL"
                    },
                    "sort": [
                        12
                    ]
                },
                {
                    "_index": "bank",
                    "_type": "_doc",
                    "_id": "13",
                    "_score": null,
                    "_source": {
                        "account_number": 13,
                        "balance": 32838,
                        "firstname": "Nanette",
                        "lastname": "Bates",
                        "age": 28,
                        "gender": "F",
                        "address": "789 Madison Street",
                        "employer": "Quility",
                        "email": "nanettebates@quility.com",
                        "city": "Nogal",
                        "state": "VA"
                    },
                    "sort": [
                        13
                    ]
                },
                {
                    "_index": "bank",
                    "_type": "_doc",
                    "_id": "14",
                    "_score": null,
                    "_source": {
                        "account_number": 14,
                        "balance": 20480,
                        "firstname": "Erma",
                        "lastname": "Kane",
                        "age": 39,
                        "gender": "F",
                        "address": "661 Vista Place",
                        "employer": "Stockpost",
                        "email": "ermakane@stockpost.com",
                        "city": "Chamizal",
                        "state": "NY"
                    },
                    "sort": [
                        14
                    ]
                },
                {
                    "_index": "bank",
                    "_type": "_doc",
                    "_id": "15",
                    "_score": null,
                    "_source": {
                        "account_number": 15,
                        "balance": 43456,
                        "firstname": "Bobbie",
                        "lastname": "Sexton",
                        "age": 21,
                        "gender": "M",
                        "address": "232 Sedgwick Place",
                        "employer": "Zytrex",
                        "email": "bobbiesexton@zytrex.com",
                        "city": "Hendersonville",
                        "state": "CA"
                    },
                    "sort": [
                        15
                    ]
                },
                {
                    "_index": "bank",
                    "_type": "_doc",
                    "_id": "16",
                    "_score": null,
                    "_source": {
                        "account_number": 16,
                        "balance": 35883,
                        "firstname": "Adrian",
                        "lastname": "Pitts",
                        "age": 34,
                        "gender": "F",
                        "address": "963 Fay Court",
                        "employer": "Combogene",
                        "email": "adrianpitts@combogene.com",
                        "city": "Remington",
                        "state": "SD"
                    },
                    "sort": [
                        16
                    ]
                },
                {
                    "_index": "bank",
                    "_type": "_doc",
                    "_id": "17",
                    "_score": null,
                    "_source": {
                        "account_number": 17,
                        "balance": 7831,
                        "firstname": "Bessie",
                        "lastname": "Orr",
                        "age": 31,
                        "gender": "F",
                        "address": "239 Hinsdale Street",
                        "employer": "Skyplex",
                        "email": "bessieorr@skyplex.com",
                        "city": "Graball",
                        "state": "FL"
                    },
                    "sort": [
                        17
                    ]
                },
                {
                    "_index": "bank",
                    "_type": "_doc",
                    "_id": "18",
                    "_score": null,
                    "_source": {
                        "account_number": 18,
                        "balance": 4180,
                        "firstname": "Dale",
                        "lastname": "Adams",
                        "age": 33,
                        "gender": "M",
                        "address": "467 Hutchinson Court",
                        "employer": "Boink",
                        "email": "daleadams@boink.com",
                        "city": "Orick",
                        "state": "MD"
                    },
                    "sort": [
                        18
                    ]
                },
                {
                    "_index": "bank",
                    "_type": "_doc",
                    "_id": "19",
                    "_score": null,
                    "_source": {
                        "account_number": 19,
                        "balance": 27894,
                        "firstname": "Schwartz",
                        "lastname": "Buchanan",
                        "age": 28,
                        "gender": "F",
                        "address": "449 Mersereau Court",
                        "employer": "Sybixtex",
                        "email": "schwartzbuchanan@sybixtex.com",
                        "city": "Greenwich",
                        "state": "KS"
                    },
                    "sort": [
                        19
                    ]
                }
            ]
        }
    }
    

    (2) 按照指定字段搜索

    GET /bank/_search
    {
      "query": { 
        "match": { "address": "mill lane" } 
      }
    }
    
    
    

    注意事项

    (1) term,match,match_pharse的区别

    ​ term 相当于等于,

    ​ match 模糊比配, 比如搜索词是 "我 爱" ,那么短语中,只要出现了,我或者爱,那么就会被认为比配上

    ​ match_pharse 短语匹配,比如 "我 爱",那么只有短语中,严格出现了"我 爱" 这个完整词,才会被匹配上

    相关文章

      网友评论

          本文标题:2020-06-26

          本文链接:https://www.haomeiwen.com/subject/bxhyfktx.html