ElasticSearch常用搜索语法

随着文档型数据库用得越来越多,ES使用也成了程序猿的常备技能,下面整理了工作中用到的ES搜索语法

基本搜索

  • 模糊搜索、搜索相关的值,对这个查询词不进行分词,必须完全匹配查询词才可以作为结果显示
{
    "query" : {
        "match_phrase" : {
            "字段" : "搜索值"
        }
    }
}
  • 间隔1个单词以内也算是匹配的结果
{
  "query": {
    "match_phrase": {
      "product_name" : {
          "query" : "PHILIPS HX6730",
          "slop" : 1
      }
    }
  }
}
  • 类似 Google 搜索框,关键字输入推荐,max_expansions用来限定最多匹配多少个term,优化性能
{
  "query": {
    "match_phrase_prefix": {
      "product_name": "PHILIPS HX",
      "slop": 5,
      "max_expansions": 20
    }
  }
}
  • 对查询的词进行分词,再进行匹配,match_phrase性能没有match好
{
  "query": {
    "match": {"title": "串"}
  },
  "from": 10,
  "size": 10
}

模糊搜索

  • 指定字段模糊搜索,从 title 和 tag 属性中去找
{
  "query": {
    "multi_match": {
      "query": "串串",
      "fields": [ "title", "tag"]
    }
  }
}
  • 指定字段模糊搜索,不分词匹配
{
  "query":{
    "multi_match":{
        "query":"aa",
        "type" : "phrase_prefix",
        "fields":["content_text"]
    }
  }
}

50%命中其中两个词就返回

{
  "query": {
    "match": {
      "product_name": {
        "query": "java 程序员 书 推荐",
        "minimum_should_match": "50%"
      }
    }
  }
}

稍复杂的搜索

  • bool使用terms,完全匹配
{"query": {
    "bool": {
      "must": [
       {"terms":{"node_standard_code":[1001,1002,1004,1007]}}
      ]
    }
 }
}
  • 大于或小于,query 和 filter 一起使用的话,filter 会先执行,filter,只查询出搜索条件的数据,不计算相关度分数,query,查询出搜索条件的数据,并计算相关度分数,按照分数进行倒序排序,filter(性能更好,无排序),无需计算相关度分数,也就无需排序,内置的自动缓存最常使用查询结果的数据
{
  "query": {
    "bool": {
      "must": {
        "match": {
          "product_name": "toothbrush"
        }
      },
      "filter": {
        "range": {
          "price": {
            "gt": 400,
            "lt": 700
          }
        }
      }
    }
  }
}
  • terms 用法,类似于数据库的 in
{
  "query": {
    "constant_score": {
      "filter": {
        "terms": {
          "product_name": [
            "toothbrush",
            "shell"
          ]
        }
      }
    }
  }
}
  • tags IS NULL
{
    "query" : {
        "constant_score" : {
            "filter": {
                "missing" : { "field" : "tags" }
            }
        }
    }
}
  • tags IS NOT NULL
{
    "query" : {
        "constant_score" : {
            "filter" : {
                "exists" : { "field" : "tags" }
            }
        }
    }
}
  • 前缀查询tag LIKE ‘aa%’
{
    "query": {
        "prefix": {
            "postcode": "W1"
        }
    }
}
  • 全匹配查询:LIKE %word%
{
    "query": {
        "wildcard": {
            "affair_list.affairs_name.keyword": {
                "value": "*%*"
            }
        }
    }
}
  • 特殊字符问题
{
    "query": {
        "wildcard": {
            "affair_list.affairs_name.keyword": {
                "value": "*\\**"
            }
        }
    }
}
  • 模拟数据库的LOCATE函数
{
  "query": {
    "bool": {
      "filter": {
        "script": {
           "script": "'10010001'.startsWith(doc['code.keyword'].value)"
        }
      }
    }
  }
}
  • bool查询,must:是类似and,must_not:不等于,should:类似or
{
  "query": {
    "bool": {
    "fileter":[
        {"range":{"date.keyword":{"gt":"20170101","lt":"20170201"}}}
    ],
    "must":[
      {
      "bool":{
        "should": [
          {"term": {"A.keyword": "0000000000"}},
          {"term": {"B.keyword": "0000000001"}}
        ]
       }
      }
    ]
    }
  }
}
  • 实现数据库的OR条件
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "product_name": "java"
          }
        },
        {
          "match": {
            "product_name": "程序员"
          }
        },
        {
          "match": {
            "product_name": "书"
          }
        },
        {
          "match": {
            "product_name": "推荐"
          }
        }
      ],
      "minimum_should_match": 3
    }
  }
}
  • 脚本查询,文档内字段对比
{
  "query":{
    "script": {
        "script": {
            "inline": "doc['rr_mark_id'].value - doc['mr_mark_id'].value == 0",
            "lang": "painless"
        }
    }
  }
}
  • 删除操作-删除所有数据
    -XPOST affairs_accept_cache_storage/_delete_by_query
{"query": {
    "bool": {
      "filter": {
        "term": {"status": "1"}
      }
    }
 }
}

全网最新在线观看

ElasticSearch常用搜索语法_第1张图片

你可能感兴趣的:(编程,elasticsearch,搜索引擎)