(3)ElasticSearch--全文检索

一、创建数据

1.创建索引
//PUT http://10.1.84.116:9200/school
{
    "settings": {
        "index": {
            "number_of_shards": "1",
            "number_of_replicas": "0"
        }
    },
    "mappings": {
        "person": {
            "properties": {
                "name": {
                    "type": "text"
                },
                "age": {
                    "type": "integer"
                },
                "mail": {
                    "type": "keyword"
                },
                "hobby": {
                    "type": "text",
                    "analyzer": "ik_max_word"
                }
            }
        }
    }
}
2.批量创建数据
{"index":{"_index":"school","_type":"person"}}
{"name":"张三","age": 20,"mail": "[email protected]","hobby":"羽毛球、乒乓球、足球"}
{"index":{"_index":"school","_type":"person"}}
{"name":"李四","age": 21,"mail": "[email protected]","hobby":"羽毛球、乒乓球、足球、篮球"}
{"index":{"_index":"school","_type":"person"}}
{"name":"王五","age": 22,"mail": "[email protected]","hobby":"羽毛球、篮球、游泳、听音乐"}
{"index":{"_index":"school","_type":"person"}}
{"name":"赵六","age": 23,"mail": "[email protected]","hobby":"跑步、游泳、篮球"}
{"index":{"_index":"school","_type":"person"}}
{"name":"孙七","age": 24,"mail": "[email protected]","hobby":"听音乐、看电影、羽毛球"}

(3)ElasticSearch--全文检索_第1张图片

3.单词搜索

搜索参数

//GET /school/person/_search
{
    "query": {
        "match": {
            "hobby": "音乐"
        }
    },
    "highlight": {
        "fields": {
            "hobby": {}
        }
    }
}

搜索结果

{
    "took": 118,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 2,
        "max_score": 0.81652206,
        "hits": [
            {
                "_index": "school",
                "_type": "person",
                "_id": "k4Ndn3MB74wApIfXYeWI",
                "_score": 0.81652206,
                "_source": {
                    "name": "王五",
                    "age": 22,
                    "mail": "[email protected]",
                    "hobby": "羽毛球、篮球、游泳、听音乐"
                },
                "highlight": {
                    "hobby": [
                        "羽毛球、篮球、游泳、听音乐"
                    ]
                }
            },
            {
                "_index": "school",
                "_type": "person",
                "_id": "mINhn3MB74wApIfXJeUx",
                "_score": 0.81652206,
                "_source": {
                    "name": "孙七",
                    "age": 24,
                    "mail": "[email protected]",
                    "hobby": "听音乐、看电影、羽毛球"
                },
                "highlight": {
                    "hobby": [
                        "听音乐、看电影、羽毛球"
                    ]
                }
            }
        ]
    }
}
4.多词搜索(或关系)

爱好包括音乐或游泳
搜索参数

//GET /school/person/_search
{
    "query": {
        "match": {
            "hobby": "音乐 游泳"
        } 
    },
    "highlight": {
        "fields": {
            "hobby": {}
        }
    }
}

搜索结果

{
    "took": 11,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 3,
        "max_score": 1.6330441,
        "hits": [
            {
                "_index": "school",
                "_type": "person",
                "_id": "k4Ndn3MB74wApIfXYeWI",
                "_score": 1.6330441,
                "_source": {
                    "name": "王五",
                    "age": 22,
                    "mail": "[email protected]",
                    "hobby": "羽毛球、篮球、游泳、听音乐"
                },
                "highlight": {
                    "hobby": [
                        "羽毛球、篮球、游泳、听音乐"
                    ]
                }
            },
            {
                "_index": "school",
                "_type": "person",
                "_id": "lINdn3MB74wApIfXcOWi",
                "_score": 1.1349231,
                "_source": {
                    "name": "赵六",
                    "age": 23,
                    "mail": "[email protected]",
                    "hobby": "跑步、游泳、篮球"
                },
                "highlight": {
                    "hobby": [
                        "跑步、游泳、篮球"
                    ]
                }
            },
            {
                "_index": "school",
                "_type": "person",
                "_id": "mINhn3MB74wApIfXJeUx",
                "_score": 0.81652206,
                "_source": {
                    "name": "孙七",
                    "age": 24,
                    "mail": "[email protected]",
                    "hobby": "听音乐、看电影、羽毛球"
                },
                "highlight": {
                    "hobby": [
                        "听音乐、看电影、羽毛球"
                    ]
                }
            }
        ]
    }
}
5.多词搜索(与关系)

爱好既包括音乐也游泳
搜索参数

//GET /school/person/_search
{
    "query": {
        "match": {
            "hobby": {
                "query": "音乐 游泳",
                "operator": "and"
            }
        }
    },
    "highlight": {
        "fields": {
            "hobby": {}
        }
    }
}

搜索结果

{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 1.6330441,
        "hits": [
            {
                "_index": "school",
                "_type": "person",
                "_id": "k4Ndn3MB74wApIfXYeWI",
                "_score": 1.6330441,
                "_source": {
                    "name": "王五",
                    "age": 22,
                    "mail": "[email protected]",
                    "hobby": "羽毛球、篮球、游泳、听音乐"
                },
                "highlight": {
                    "hobby": [
                        "羽毛球、篮球、游泳、听音乐"
                    ]
                }
            }
        ]
    }
}
6.多词搜索(相似度)

搜索参数

//GET /school/person/_search
{
    "query": {
        "match": {
            "hobby": {
                "query": "游泳 羽毛球",
                "minimum_should_match": "80%"
            }
        }
    },
    "highlight": {
        "fields": {
            "hobby": {}
        }
    }
}

搜索结果

{
    "took": 10,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 4,
        "max_score": 1.621458,
        "hits": [
            {
                "_index": "school",
                "_type": "person",
                "_id": "k4Ndn3MB74wApIfXYeWI",
                "_score": 1.621458,
                "_source": {
                    "name": "王五",
                    "age": 22,
                    "mail": "[email protected]",
                    "hobby": "羽毛球、篮球、游泳、听音乐"
                },
                "highlight": {
                    "hobby": [
                        "羽毛、篮球、游泳、听音乐"
                    ]
                }
            },
            {
                "_index": "school",
                "_type": "person",
                "_id": "kYNdn3MB74wApIfXNeXo",
                "_score": 0.9608413,
                "_source": {
                    "name": "张三",
                    "age": 20,
                    "mail": "[email protected]",
                    "hobby": "羽毛球、乒乓球、足球"
                },
                "highlight": {
                    "hobby": [
                        "羽毛、乒乓、足球"
                    ]
                }
            },
            {
                "_index": "school",
                "_type": "person",
                "_id": "koNdn3MB74wApIfXTuVV",
                "_score": 0.91348255,
                "_source": {
                    "name": "李四",
                    "age": 21,
                    "mail": "[email protected]",
                    "hobby": "羽毛球、乒乓球、足球、篮球"
                },
                "highlight": {
                    "hobby": [
                        "羽毛、乒乓、足球、篮球"
                    ]
                }
            },
            {
                "_index": "school",
                "_type": "person",
                "_id": "mINhn3MB74wApIfXJeUx",
                "_score": 0.80493605,
                "_source": {
                    "name": "孙七",
                    "age": 24,
                    "mail": "[email protected]",
                    "hobby": "听音乐、看电影、羽毛球"
                },
                "highlight": {
                    "hobby": [
                        "听音乐、看电影、羽毛"
                    ]
                }
            }
        ]
    }
}
7.组合搜索

搜索结果中必须包含篮球,不能包含音乐,如果包含了游泳,那么它的相似度更高。
搜索参数

//GET /school/person/_search
{
    "query": {
        "bool": {
            "must": {
                "match": {
                    "hobby": "篮球"
                }
            },
            "must_not": {
                "match": {
                    "hobby": "音乐"
                }
            },
            "should": [
                {
                    "match": {
                        "hobby": "游泳"
                    }
                }
            ]
        }
    },
    "highlight": {
        "fields": {
            "hobby": {}
        }
    }
}

搜索结果

{
    "took": 7,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 2,
        "max_score": 1.8336569,
        "hits": [
            {
                "_index": "school",
                "_type": "person",
                "_id": "lINdn3MB74wApIfXcOWi",
                "_score": 1.8336569,
                "_source": {
                    "name": "赵六",
                    "age": 23,
                    "mail": "[email protected]",
                    "hobby": "跑步、游泳、篮球"
                },
                "highlight": {
                    "hobby": [
                        "跑步、游泳篮球"
                    ]
                }
            },
            {
                "_index": "school",
                "_type": "person",
                "_id": "koNdn3MB74wApIfXTuVV",
                "_score": 0.50270504,
                "_source": {
                    "name": "李四",
                    "age": 21,
                    "mail": "[email protected]",
                    "hobby": "羽毛球、乒乓球、足球、篮球"
                },
                "highlight": {
                    "hobby": [
                        "羽毛球、乒乓球、足球、篮球"
                    ]
                }
            }
        ]
    }
}
8.权重

有些时候,我们可能需要对某些词增加权重来影响该条数据的得分。如下:搜索关键字为“游泳篮球”,如果结果中包含了“音乐”权重为10,包含了“跑步”权重为2
搜索参数

//GET /school/person/_search
{
    "query": {
        "bool": {
            "must": {
                "match": {
                    "hobby": {
                        "query": "游泳篮球",
                        "operator": "and"
                    }
                }
            },
            "should": [
                {
                    "match": {
                        "hobby": {
                            "query": "音乐",
                            "boost": 10
                        }
                    }
                },
                {
                    "match": {
                        "hobby": {
                            "query": "跑步",
                            "boost": 2
                        }
                    }
                }
            ]
        }
    },
    "highlight": {
        "fields": {
            "hobby": {}
        }
    }
}

搜索结果

{
    "took": 5,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 2,
        "max_score": 9.484448,
        "hits": [
            {
                "_index": "school",
                "_type": "person",
                "_id": "k4Ndn3MB74wApIfXYeWI",
                "_score": 9.484448,
                "_source": {
                    "name": "王五",
                    "age": 22,
                    "mail": "[email protected]",
                    "hobby": "羽毛球、篮球、游泳、听音乐"
                },
                "highlight": {
                    "hobby": [
                        "羽毛球、篮球游泳、听音乐"
                    ]
                }
            },
            {
                "_index": "school",
                "_type": "person",
                "_id": "lINdn3MB74wApIfXcOWi",
                "_score": 5.4279313,
                "_source": {
                    "name": "赵六",
                    "age": 23,
                    "mail": "[email protected]",
                    "hobby": "跑步、游泳、篮球"
                },
                "highlight": {
                    "hobby": [
                        "跑步游泳篮球"
                    ]
                }
            }
        ]
    }
}

你可能感兴趣的:(#,Elasticsearch,es,全文检索)