zoukankan      html  css  js  c++  java
  • Elasticsearch 全文搜索

    1,匹配查询(match)

    • match查询主要的应用场景是进行全文搜索;
    // 1,初始化数据
    DELETE /my_index 
    
    PUT /my_index
    { "settings": { "number_of_shards": 1 }} 
    
    POST /my_index/my_type/_bulk
    { "index": { "_id": 1 }}
    { "title": "The quick brown fox" }
    { "index": { "_id": 2 }}
    { "title": "The quick brown fox jumps over the lazy dog" }
    { "index": { "_id": 3 }}
    { "title": "The quick brown fox jumps over the quick dog" }
    { "index": { "_id": 4 }}
    { "title": "Brown fox brown dog" }
    
    
    // 2,match 单个词查询
    GET /my_index/my_type/_search
    {
      "query":{
        "match":{
          "title":"QUICK!"
        }
      }
    }
    
    
    // 3,match 多词查询
    GET /my_index/my_type/_search
    {
      "query":{
        "match":{
          "title":"BROWN DOG!"
        }
      }
    }
    
    // 3.1 operator 操作符,默认值为or
    GET /my_index/my_type/_search
    {
      "query":{
        "match":{
          "title":{
            "query":"BROWN DOG!",
            "operator":"and"
            
          }
        }
      }
    }
    
    // 3.2 minimum_should_match 最小匹配参数
    GET /my_index/my_type/_search
    {
      "query":{
        "match":{
          "title":{
            "query":"quick brown dog",
            "minimum_should_match": "75%"
            
          }
        }
      }
    }
    

    2,组合查询

    // 组合查询
    GET /my_index/my_type/_search
    {
      "query":{
        "bool":{
          "must":{"match":{"title":"quick"}},
          "must_not":{"match":{"title":"lazy"}},
          "should":[
            {"match":{"title":"brown"}},
            {"match":{"title":"dog"}}
            ]
        }
      }
    }
    
    // 备注:should语句,一个文档不必包含“brown”或“dog”这两个词项,但如果一旦包含,它的相关性会提高。
    
    // 控制精度(minimum_should_match)
    GET /my_index/my_type/_search
    {
      "query":{
        "bool":{
          "should":[
            {"match":{"title":"brown"}},
            {"match":{"title":"fox"}},
            {"match":{"title":"dog"}}
            ],
            "minimum_should_match": 2
        }
      }
    }
    

    3,查询语句提升权重

    // boost 控制查询语句的相对权重,默认值为1,大于1会提升一个语句的相对权重
    GET /_search
    {
        "query": {
            "bool": {
                "must": {
                    "match": {  
                        "content": {
                            "query":    "full text search",
                            "operator": "and"
                        }
                    }
                },
                "should": [
                    { "match": {
                        "content": {
                            "query": "Elasticsearch",
                            "boost": 3 
                        }
                    }},
                    { "match": {
                        "content": {
                            "query": "Lucene",
                            "boost": 2 
                        }
                    }}
                ]
            }
        }
    }
    

    4,控制分析

    // 1,新增字段,并配置分析器
    PUT /my_index/_mapping/my_type
    {
        "my_type": {
            "properties": {
                "english_title": {
                    "type":     "text",
                    "analyzer": "english"
                }
            }
        }
    }
    
    
    // 2,validate-query API 分析查询过程
    GET /my_index/my_type/_validate/query?explain
    {
      "query":{
        "bool":{
          "should":[
            {"match":{"title":"Foxes"}},
            {"match":{"english_title": "Foxes"}}
            ]
        }
      }
    }
    

    参考资料:
    -FORBIDDEN 12 index read-only allow delete (api)
    -全文检索

  • 相关阅读:
    poj 2584 T-Shirt Gumbo (二分匹配)
    hdu 1757 A Simple Math Problem (乘法矩阵)
    矩阵之矩阵乘法(转载)
    poj 2239 Selecting Courses (二分匹配)
    hdu 3661 Assignments (贪心)
    hdu 1348 Wall (凸包)
    poj 2060 Taxi Cab Scheme (二分匹配)
    hdu 2202 最大三角形 (凸包)
    hdu 1577 WisKey的眼神 (数学几何)
    poj 1719 Shooting Contest (二分匹配)
  • 原文地址:https://www.cnblogs.com/linkworld/p/12030686.html
Copyright © 2011-2022 走看看