美文网首页
ElasticSearch搜索例子

ElasticSearch搜索例子

作者: Leo_23 | 来源:发表于2023-08-07 21:30 被阅读0次

    ElasticSearch版本7.17.3, hotel 搜索例子

    
    GET _search
    {
      "query": {
        "match_all": {}
      }
    }
    
    GET /
    
    
    POST /_analyze
    {
      "text": "黑马程序员学习java的太棒了,奥利给!",
      "analyzer": "ik_smart"
    }
    
    
    POST /_analyze
    {
      "text": "黑马程序员学习java太棒了",
      "analyzer": "ik_max_word"
    }
    
    PUT /heima
    {
      "mappings": {
        "properties": {
          "info": {
            "type": "text",
            "analyzer": "ik_smart"
          },
          "email": {
            "type": "keyword",
            "index": false
          },
          "name": {
            "type": "object",
            "properties": {
              "firstName": {
                "type": "keyword",
                "index": true
              },
              "lastName": {
                "type": "keyword"
              }
            }
          }
        }
      }
    }
    
    GET heima
    
    
    PUT /heima/_mapping
    {
      "properties": {
        "age": {
          "type": "integer"
        }
      }
    }
    
    POST /heima/_doc/1
    {
      "info": "黑马程序员讲师Java",
      "email": "1@1.com",
      "name": {
        "firtName": "李",
        "lastName": "建舜"
      },
      "age": 18
    }
    
    GET /heima/_doc/1
    
    PUT /heima/_doc/3
    {
      "info": "黑马程序员Python讲师",
      "email": "1@1.com",
      "name": {
        "firtName": "李",
        "lastName": "建舜"
      },
      "age": 18
    }
    
    POST /heima/_update/1
    {
      "doc": {
         "info" : "黑马程序员Java讲师"
      }
    }
    
    PUT /hotel
    {
      "mappings": {
        "properties": {
          "id":{
            "type": "keyword"
          },
          "name":{
            "type": "text",
            "analyzer": "ik_max_word",
            "copy_to": "all"
          },
          "address": {
            "type": "keyword",
            "index": false
          },
          "price":{
            "type": "integer"
          },
          "score":{
            "type": "integer"
          },
          "brand":{
            "type": "keyword",
            "copy_to": "all"
          },
          "city":{
            "type": "keyword"
          },
          "starName":{
            "type": "keyword"
          },
          "business":{
            "type": "keyword",
            "copy_to": "all"
          },
          "location": {
            "type": "geo_point"
          },
          "pic": {
            "type": "keyword",
            "index": false
          },
          "all": {
            "type": "text",
            "analyzer": "ik_max_word"
          }
        }
      }
    }
    
    
    DELETE /hotel
    
    GET /hotel/_settings
    
    GET /hotel/_search
    
    
    
    
    GET /hotel/_search
    {
      "query": {
        "match_all": {}
      }
    }
    
    
    GET /hotel/_search
    {
      "query": {
        "match": {
          "all": "外滩如家"
        }
      }
    }
    
    
    GET /hotel/_search
    {
      "query": {
        "multi_match": {
          "query": "外滩如家",
          "fields": ["brand", "name", "business"]
        }
      }
    }
    
    GET /hotel/_search
    {
      "query": {
        "term": {
          "city": {
            "value": "深圳"
          }
        }
      }
    }
    
    GET /hotel/_search
    {
      "query": {
        "range": {
          "price": {
            "gte": 100,
            "lte": 300
          }
        }
      }
    }
    
    GET /hotel/_search
    {
      "query": {
        "geo_distance": {
          "distance": "3km",
          "location": "31.21, 121.5"
        }
      }
    }
    
    
    
    GET /hotel/_search
    {
      "query": {
        "function_score": {
          "query": {
            "match": {
              "all": "外滩"
            }
          },
          "functions": [
            {
              "filter": {
                "term": {
                  "brand": "如家"
                }
              },
              "weight": 10
            }
          ],
          "boost_mode": "sum"
        }
      }
    }
    
    
    GET /hotel/_search
    {
      "query": {
        "bool": {
          "must": [
            {
              "term": {
                "city": {
                  "value": "上海"
                }
              }
            }
          ],
          "should": [
            {
              "term": {
                "brand": {
                  "value": "皇冠假日"
                }
              }
            },
            {
              "term": {
                "brand": {
                  "value": "华美达"
                }
              }
            }
          ],
          "must_not": [
            {
              "range": {
                "price": {
                  "lte": 500
                }
              }
            }
          ],
          "filter": [
            {
              "range": {
                "score": {
                  "gte": 45
                }
              }
            }
          ]
        }
      }
    }
    
    GET /hotel/_search
    {
      "query": {
        "match_all": {}
      },
      "sort": [
        {
          "score": {
            "order": "desc"
          },
          "price": {
            "order": "asc"
          }
        }
      ]
    }
    
    
    GET /hotel/_search
    {
      "query": {
        "match_all": {}
      },
      "sort": [
        {
          "_geo_distance": {
            "location": {
              "lat": 31.034661,
              "lon": 121.612282
            },
            "order": "asc",
            "unit": "km"
          }
        }
      ]
    }
    
    # 分页
    GET /hotel/_search
    {
      "query": {
        "match_all": {}
      },
      "sort": [
        {
          "price": {
            "order": "asc"
          }
        }
      ],
      "from": 10,
      "size": 20
    }
    
    # 高亮,搜索结果处理整体语法
    GET /hotel/_search
    {
      "query": {
        "match": {
          "name": "如家"
        }
      },
      "from": 0, 
      "size": 20, 
      "sort": [
        {
          "price": {
            "order": "asc"
          },
          "_geo_distance": {
            "location": {
              "lat": 31.034661,
              "lon": 121.612282
            },
            "order": "asc",
            "unit": "km"
          }
        }
      ], 
      "highlight": {
        "fields": {
          "name": {
            "require_field_match": "false", 
            "pre_tags": "<em>",
            "post_tags": "</em>"
          }
        }
      }
    }
    
    
    # 聚合Bucket
    GET /hotel/_search
    {
      "size": 0,
      "aggs": {
        "brandAgg": {
          "terms": {
            "field": "brand",
            "order": {
              "_count": "asc"
            }, 
            "size": 20
          }
        }
      }
    }
    
    GET /hotel/_search
    {
      "query": {
        "range": {
          "price": {
            "lte": 200
          }
        }
      }, 
      "size": 0,
      "aggs": {
        "brandAgg": {
          "terms": {
            "field": "brand",
            "order": {
              "_count": "asc"
            }, 
            "size": 20
          }
        }
      }
    }
    
    # 聚合Metrics
    GET /hotel/_search
    {
      "size": 0,
      "aggs": {
        "brandAgg": {
          "terms": {
            "field": "brand",
            "size": 20,
            "order": {
              "scoreAgg.avg": "desc"
            }
          },
          "aggs": {
            "scoreAgg": {
              "stats": {
                "field": "score"
              }
            }
          }
        }
      }
    }
    
    
    
    

    相关文章

      网友评论

          本文标题:ElasticSearch搜索例子

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