Elastic Search 的搜索

别说谁变了你拦得住时间么 提交于 2020-04-06 12:37:14

批量导入数据

ES 提供了一个叫 bulk 的 API 来进行批量操作

在ES安装目录下新建一个文件,文件名可以自定义,这里是player

文件内容如下:

{"index":{"_index":"nba","_type":"_doc","_id":"1"}}
{"countryEn":"United States","teamName":"老鹰","birthDay":831182400000,"country":"美国","teamCityEn":"Atlanta","code":"jaylen_adams","displayAffiliation":"United States","displayName":"杰伦 亚当斯","schoolType":"College","teamConference":"东部","teamConferenceEn":"Eastern","weight":"86.2 公斤","teamCity":"亚特兰大","playYear":1,"jerseyNo":"10","teamNameEn":"Hawks","draft":2018,"displayNameEn":"Jaylen Adams","heightValue":1.88,"birthDayStr":"1996-05-04","position":"后卫","age":23,"playerId":"1629121"}
{"index":{"_index":"nba","_type":"_doc","_id":"2"}}
{"countryEn":"New Zealand","teamName":"雷霆","birthDay":743140800000,"country":"新西兰","teamCityEn":"Oklahoma City","code":"steven_adams","displayAffiliation":"Pittsburgh/New Zealand","displayName":"斯蒂文 亚当斯","schoolType":"College","teamConference":"西部","teamConferenceEn":"Western","weight":"120.2 公斤","teamCity":"俄克拉荷马城","playYear":6,"jerseyNo":"12","teamNameEn":"Thunder","draft":2013,"displayNameEn":"Steven Adams","heightValue":2.13,"birthDayStr":"1993-07-20","position":"中锋","age":26,"playerId":"203500"}

注意:最后需要空一行

执行以下命令,可以将文件里的数据批量导入

curl -X POST "localhost:9200/_bulk" -H "Content-Type: application/json" --data-binary @player

ES 之 term 的多种查询

单词级别查询:这些查询通常用于结构化的数据,比如:number, date, keyword 等,而不是对 text。也就是说,全文本查询之前要先对文本内容进行分词,而单词级别的查询直接在相应字段的反向索引中精确查找,单词级别的查询一般用于数值、日期等类型的字段上。

准备工作

  • 删除nba索引
  • 新增nba索引
  • POST:localhost:9200/nba/_mapping
    
    {
        "properties":{
            "birthDay":{
                "type":"date"
            },
            "birthDayStr":{
                "type":"keyword"
            },
            "age":{
                "type":"integer"
            },
            "code":{
                "type":"text"
            },
            "country":{
                "type":"text"
            },
            "countryEn":{
                "type":"text"
            },
            "displayAffiliation":{
                "type":"text"
            },
            "displayName":{
                "type":"text"
            },
            "displayNameEn":{
                "type":"text"
            },
            "draft":{
                "type":"long"
            },
            "heightValue":{
                "type":"float"
            },
            "jerseyNo":{
                "type":"text"
            },
            "playYear":{
                "type":"long"
            },
            "playerId":{
                "type":"keyword"
            },
            "position":{
                "type":"text"
            },
            "schoolType":{
                "type":"text"
            },
            "teamCity":{
                "type":"text"
            },
            "teamCityEn":{
                "type":"text"
            },
            "teamConference":{
                "type":"keyword"
            },
            "teamConferenceEn":{
                "type":"keyword"
            },
            "teamName":{
                "type":"keyword"
            },
            "teamNameEn":{
                "type":"keyword"
            },
            "weight":{
                "type":"text"
            }
        }
    }
  • 批量导入数据(player文件)

Term query

精准匹配查询(查找号码为23的球员)

POST:localhost:9200/nba/_search
{
    "query": {
        "term": {
            "jerseyNo": "23"
        }
    }
}

Exsit Query

在特定的字段中查找非空值的文档(查找队名非空的球员)

POST:localhost:9200/nba/_search
{
    "query": {
        "exists": {
            "field": "teamNameEn"
        }
    }
}

Prefix Query

查找包含带有指定前缀 term 的文档(查找队名以Rock开头的球员)

POST:localhost:9200/nba/_search
{
    "query": {
        "prefix": {
            "teamNameEn": "Rock"
        }
    }
}

Wildcard Query

支持通配符查询,*表示任意字符,?表示任意单个字符(查找火箭队的球员)

POST:localhost:9200/nba/_search
{
    "query": {
        "wildcard": {
            "teamNameEn": "Ro*s"
        }
    }
}

Regexp Query

正则表达式查询(查找火箭队的球员)

POST:localhost:9200/nba/_search
{
    "query": {
        "regexp": {
            "teamNameEn": "Ro.*s"
        }
    }
}

Ids Query

id 查询(查找id为1和2的球员)

POST:localhost:9200/nba/_search
{
    "query": {
        "ids": {
            "values": [1,2]
        }
    }
}

ES 的范围查询

查找指定字段在指定范围内包含值(日期、数字或字符串)的文档。

查找在nba打了2年到10年以内的球员
POST:localhost:9200/nba/_search
{
    "query": {
        "range": {
            "playYear": {
                "gte": 2,
                "lte": 10
            }
        }
    }
}


查找1980年到1999年出生的球员
POST:localhost:9200/nba/_search
{
    "query": {
        "range": {
            "birthDay": {
                "gte": "01/01/1999",
                "lte": "2022",
                "format": "dd/MM/yyyy||yyyy"
            }
        }
    }
}

ES 的布尔查询

  • must:必须出现在匹配文档中
  • filter:必须出现在文档中,但是不打分
  • must_not:不能出现在文档中
  • should:应该出现在文档中

must

查找名字叫做 James 的球员

POST:localhost:9200/nba/_search
{
    "query": {
        "bool": {
            "must": [
                {
                    "match": {
                        "displayNameEn": "james"
                    }
                }
            ]
        }
    }
}

filter

效果同 must,但是不打分(查找名字叫做 James 的球员)

must_not

查找名字叫做 James 的西部球员

POST:localhost:9200/nba/_search
{
    "query": {
        "bool": {
            "must": [
                {
                    "match": {
                        "displayNameEn": "james"
                    }
                }
            ],
            "must_not": [
                {
                    "term": {
                        "teamConferenceEn": {
                            "value": "Eastern"
                        }
                    }
                }
            ]
        }
    }
}

should

即使匹配不到也返回,只是评分不同

查找名字叫做James的打球时间应该在11到20年西部球员

POST:localhost:9200/nba/_search
{
    "query": {
        "bool": {
            "must": [
                {
                    "match": {
                        "displayNameEn": "james"
                    }
                }
            ],
            "must_not": [
                {
                    "term": {
                        "teamConferenceEn": {
                            "value": "Eastern"
                        }
                    }
                }
            ],
            "should": [
                {
                    "range": {
                        "playYear": {
                            "gte": 11,
                            "lte": 20
                        }
                    }
                }
            ]
        }
    }
}

如果 minimum_should_match=1,则变成要查出名字叫做 James 的打球时间在11到20年西部球员

POST:localhost:9200/nba/_search
{
    "query": {
        "bool": {
            "must": [
                {
                    "match": {
                        "displayNameEn": "james"
                    }
                }
            ],
            "must_not": [
                {
                    "term": {
                        "teamConferenceEn": {
                            "value": "Eastern"
                        }
                    }
                }
            ],
            "should": [
                {
                    "range": {
                        "playYear": {
                            "gte": 11,
                            "lte": 20
                        }
                    }
                }
            ],
            "minimum_should_match": 1
        }
    }
}

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!