easy-es使用详解与源码解析

1.git clone后,easy-es-core中的pom中需要引入:

        
            org.apache.httpcomponents
            httpcore
            4.4.12
        

2.easy-es-sample 中提供了基本案例,可以用来解析源码。

3.easy-es-common中的pom里可以看到,它是基于elasticsearch-rest-high-level-client的。

如果不熟悉elasticsearch-rest-high-level-client,建议先熟悉一下。

1. DSL语句

1.1 DSL常见的查询分类

查询所有:match_all (一般也就是测试用用)

全文检索:利用分词器对用户输入的内容进行分词后进行匹配查询

match_query,multi_match_query

精确查询:根据精确词条值查找数据,主要字段类型为keyword,日期,数值,布尔等
ids,range,term

地理(geo)查询:根据经纬度查询。
geo_distance,geo_bounding_box

复合查询:将上述查询条件组合起来,合并查询条件

bool,function_score

1.2 全文检索

对用户输入的内容进行分词后进行匹配查询

GET /easyes_document/_search
{
  "query": {
    "match_all": {}
  }
}

GET /easyes_document/_search
{
  "query": {
    "match": {
      "title": "老王"
    }
  }
}

GET /easyes_document/_search
{
  "query": {
    "multi_match": {
      "query": "老王",
      "fields": ["content","creator"]
    }
  }
}

1.3 精确检索

根据精确词条值查找数据,主要字段类型为keyword,日期,数值,布尔等,不会对用户输入的内容进行分词

term:根据词条精确值查询
range:根据值的范围查询

#term 的使用

GET /easyes_document/_search
{
  "query": {
    "term": {
      "_id": {
        "value": "3"
      }
    }
  }
}

GET /easyes_document/_search
{
  "query": {
    "range": {
      "star_num": {
        "gte": 20
      }
    }
  }
}

GET /easyes_document/_search
{
  "query": {
    "range": {
      "star_num": {
        "lte": 5
      }
    }
  }
}

GET /easyes_document/_search
{
  "query": {
    "range": {
      "gmt_create": {
        "lte": "2023-06-18 07:38:43"
      }
    }
  }
}

1.4地理坐标查询

如果没有提示需要硬写:

GET /easyes_document/_search
{
  "query": {
    "geo_distance": {
      "location": "40.13,116.63",
      "distance": "2km"
    }
  }
}

1.5 function score query改变权重影响得分

easy-es使用详解与源码解析_第1张图片

 term默认对keyword中文无效,需要field.keyword才可以。

#function score query
GET /easyes_document/_search
{
  "query": {
    "function_score": {
      "query": {
        "match": {
          "title": "老王"
        } 
      },
      "functions": [
        {
          "filter": { 
            "term": {
              "sub_title.keyword": "小毛子"
            }
          },
          "weight":100
        }
      ]
    }
  }
}

你可能感兴趣的:(elasticsearch,数学建模,java)