ES

常用操作命令:

4、ES相关
和传统数据库对比关系
Relational DB -> Databases -> Tables -> Rows -> Columns
Elasticsearch -> Indices -> Types -> Documents -> Fields

es下相关 默认端口是9200 提供java应用连接端口是9300
curl -X GET 'http://localhost:8080'

查看所有插件:
curl -X GET 'http://localhost:8080/_cat/plugins?v'

查看索引情况
curl -X GET 'http://localhost:8080/_cat/indices?v'

查看ES健康
curl -X GET 'http://localhost:8080/_cat/health?v'

查看映射
curl -X GET 'http://localhost:8080/_mapping?pretty=true'

新建索引index操作 put请求(index名必须全部小写,返回包括acknowledged字段表示操作成功)
curl -X PUT 'http://localhost:8080/user'

删除索引操作 delete请求(返回{"acknowledged":true}表示成功)
curl -X DELETE 'http://localhost:8080/user'

查询索引map
curl -X GET 'http://localhost:8080/user'

查看当前的磁盘占用率:
curl -X GET 'http://localhost:8080/_cat/allocation?v'

 

 

 

ES常用搜索类型及使用场景:

 

ElasticSearch常用搜索类型以及使用场景

最近在使用Elasticsearch构建通讯录搜索系统,分享下ElasticSearch常用搜索类型以及使用场景心得

1、match

最简单的一个match例子:

查询和"我的宝马多少马力"这个查询语句匹配的文档。

{
  "query": {
    "match": {
        "content" : {
            "query" : "我的宝马多少马力"
        }
    }
  }
}

上面的查询匹配就会进行分词,比如"宝马多少马力"会被分词为"宝马 多少 马力", 所有有关"宝马 多少 马力", 那么所有包含这三个词中的一个或多个的文档就会被搜索出来。
并且根据lucene的评分机制(TF/IDF)来进行评分。

2、match_phrase

比如上面一个例子,一个文档"我的保时捷马力不错"也会被搜索出来,那么想要精确匹配所有同时包含"宝马 多少 马力"的文档怎么做?就要使用 match_phrase 了

{
  "query": {
    "match_phrase": {
        "content" : {
            "query" : "我的宝马多少马力"
        }
    }
  }
}

完全匹配可能比较严,我们会希望有个可调节因子,少匹配一个也满足,那就需要使用到slop。

{
  "query": {
    "match_phrase": {
        "content" : {
            "query" : "我的宝马多少马力",
            "slop" : 1
        }
    }
  }
}

 

3、multi_match

如果我们希望两个字段进行匹配,其中一个字段有这个文档就满足的话,使用multi_match

{
  "query": {
    "multi_match": {
        "query" : "我的宝马多少马力",
        "fields" : ["title", "content"]
    }
  }
}

但是multi_match就涉及到匹配评分的问题了。

3-1 multi_match->best_fields

我们希望完全匹配的文档占的评分比较高,则需要使用best_fields

{
  "query": {
    "multi_match": {
      "query": "我的宝马发动机多少",
      "type": "best_fields",
      "fields": [
        "tag",
        "content"
      ],
      "tie_breaker": 0.3
    }
  }
}

意思就是完全匹配"宝马 发动机"的文档评分会比较靠前,如果只匹配宝马的文档评分乘以0.3的系数

3-2 multi_match->most_fields

我们希望越多字段匹配的文档评分越高,就要使用most_fields

{
  "query": {
    "multi_match": {
      "query": "我的宝马发动机多少",
      "type": "most_fields",
      "fields": [
        "tag",
        "content"
      ]
    }
  }
}

 

3-2 multi_match->cross_fields

我们会希望这个词条的分词词汇是分配到不同字段中的,那么就使用cross_fields

{
  "query": {
    "multi_match": {
      "query": "我的宝马发动机多少",
      "type": "cross_fields",
      "fields": [
        "tag",
        "content"
      ]
    }
  }
}

 

4、term

term是代表完全匹配,即不进行分词器分析,文档中必须包含整个搜索的词汇

{
  "query": {
    "term": {
      "content": "汽车保养"
    }
  }
}

查出的所有文档都包含"汽车保养"这个词组的词汇。

使用term要确定的是这个字段是否“被分析”(analyzed),默认的字符串是被分析的。

总结

 本次主要介绍DSL语句常用查询,下次再介绍下如何使用springboot来快速搭建基于elasticsearch搜索系统的开发。

本主题由 安运良 于 2019-05-27 15:14:47 编辑
 
 
 
 
 

 

posted @ 2019-09-30 16:26  有魚<·))><  阅读(238)  评论(0编辑  收藏  举报