Elasticsearch 学习笔记 高级查询

来源:互联网 发布:新中新dkq a16d软件 编辑:程序博客网 时间:2024/05/21 15:02

以下查询

POST 127.0.0.1:9200/book/_search

1、多条件查询

{"query":{"multi_match":{"query":"chenjie","fields":["auther","title"]}}}


结果:

{    "took": 11,    "timed_out": false,    "_shards": {        "total": 3,        "successful": 3,        "skipped": 0,        "failed": 0    },    "hits": {        "total": 1,        "max_score": 1.2029922,        "hits": [            {                "_index": "book",                "_type": "novel",                "_id": "AV8jW8WW4oF9V8mhiGCp",                "_score": 1.2029922,                "_source": {                    "word_count": "4000",                    "author": "chenjie",                    "title": "chenjie传",                    "publish_date": "1994-09-27"                }            }        ]    }}


2、使用AND OR

{"query":{"query_string":{"query":"(chenjie AND 入门) OR 活着 "}}}


结果:

{    "took": 10,    "timed_out": false,    "_shards": {        "total": 3,        "successful": 3,        "skipped": 0,        "failed": 0    },    "hits": {        "total": 2,        "max_score": 1.9439287,        "hits": [            {                "_index": "book",                "_type": "novel",                "_id": "1",                "_score": 1.9439287,                "_source": {                    "doc": {                        "title": "活着"                    },                    "title": "活着",                    "word_count": 9000,                    "author": "余华",                    "publish_date": "2017-10-16"                }            },            {                "_index": "book",                "_type": "novel",                "_id": "AV8i0A5k4oF9V8mhiGCh",                "_score": 1.7533967,                "_source": {                    "word_count": "1000",                    "author": "chenjie",                    "title": "Java从入门到放弃",                    "publish_date": "1995-08-19"                }            }        ]    }}

3、指定查询的范围

{"query":{"query_string":{"query":"(chenjie AND 入门) OR 活着 ","fields":["title","author"]}}}

结果:

{    "took": 8,    "timed_out": false,    "_shards": {        "total": 3,        "successful": 3,        "skipped": 0,        "failed": 0    },    "hits": {        "total": 2,        "max_score": 1.8134993,        "hits": [            {                "_index": "book",                "_type": "novel",                "_id": "AV8i0A5k4oF9V8mhiGCh",                "_score": 1.8134993,                "_source": {                    "word_count": "1000",                    "author": "chenjie",                    "title": "Java从入门到放弃",                    "publish_date": "1995-08-19"                }            },            {                "_index": "book",                "_type": "novel",                "_id": "1",                "_score": 1.6831388,                "_source": {                    "doc": {                        "title": "活着"                    },                    "title": "活着",                    "word_count": 9000,                    "author": "余华",                    "publish_date": "2017-10-16"                }            }        ]    }}


4、查询指定的列为给定条件的

{"query":{"term":{"author":"chenjie"}}}

结果:

{    "took": 6,    "timed_out": false,    "_shards": {        "total": 3,        "successful": 3,        "skipped": 0,        "failed": 0    },    "hits": {        "total": 3,        "max_score": 0.6931472,        "hits": [            {                "_index": "book",                "_type": "novel",                "_id": "AV8i0A5k4oF9V8mhiGCh",                "_score": 0.6931472,                "_source": {                    "word_count": "1000",                    "author": "chenjie",                    "title": "Java从入门到放弃",                    "publish_date": "1995-08-19"                }            },            {                "_index": "book",                "_type": "novel",                "_id": "AV8i0JvQ4oF9V8mhiGCi",                "_score": 0.47000363,                "_source": {                    "word_count": "2000",                    "author": "chenjie",                    "title": "MySql从删库到跑路",                    "publish_date": "1994-09-27"                }            },            {                "_index": "book",                "_type": "novel",                "_id": "AV8jW8WW4oF9V8mhiGCp",                "_score": 0.47000363,                "_source": {                    "word_count": "4000",                    "author": "chenjie",                    "title": "chenjie传",                    "publish_date": "1994-09-27"                }            }        ]    }}

5、范围,日期

{"query":{"range":{"publish_date":{"gt":"1990-01-01","lte":"now"}}}}

结果:

{    "took": 303,    "timed_out": false,    "_shards": {        "total": 3,        "successful": 3,        "skipped": 0,        "failed": 0    },    "hits": {        "total": 5,        "max_score": 1,        "hits": [            {                "_index": "book",                "_type": "novel",                "_id": "AV8i0JvQ4oF9V8mhiGCi",                "_score": 1,                "_source": {                    "word_count": "2000",                    "author": "chenjie",                    "title": "MySql从删库到跑路",                    "publish_date": "1994-09-27"                }            },            {                "_index": "book",                "_type": "novel",                "_id": "AV8i0UpQ4oF9V8mhiGCj",                "_score": 1,                "_source": {                    "word_count": "8000",                    "author": "路遥",                    "title": "平凡的世界",                    "publish_date": "2000-01-01"                }            },            {                "_index": "book",                "_type": "novel",                "_id": "AV8jW8WW4oF9V8mhiGCp",                "_score": 1,                "_source": {                    "word_count": "4000",                    "author": "chenjie",                    "title": "chenjie传",                    "publish_date": "1994-09-27"                }            },            {                "_index": "book",                "_type": "novel",                "_id": "AV8i0A5k4oF9V8mhiGCh",                "_score": 1,                "_source": {                    "word_count": "1000",                    "author": "chenjie",                    "title": "Java从入门到放弃",                    "publish_date": "1995-08-19"                }            },            {                "_index": "book",                "_type": "novel",                "_id": "1",                "_score": 1,                "_source": {                    "doc": {                        "title": "活着"                    },                    "title": "活着",                    "word_count": 9000,                    "author": "余华",                    "publish_date": "2017-10-16"                }            }        ]    }}


6、filter指定列

{"query":{"bool":{"filter":{"term":{"word_count": 1000}}}}}

结果:

{    "took": 16,    "timed_out": false,    "_shards": {        "total": 3,        "successful": 3,        "skipped": 0,        "failed": 0    },    "hits": {        "total": 1,        "max_score": 0,        "hits": [            {                "_index": "book",                "_type": "novel",                "_id": "AV8i0A5k4oF9V8mhiGCh",                "_score": 0,                "_source": {                    "word_count": "1000",                    "author": "chenjie",                    "title": "Java从入门到放弃",                    "publish_date": "1995-08-19"                }            }        ]    }}


7、固定分数

{"query":{"constant_score":{"filter":{"match":{"author":"chenjie"}}}}}


结果:

{    "took": 30,    "timed_out": false,    "_shards": {        "total": 3,        "successful": 3,        "skipped": 0,        "failed": 0    },    "hits": {        "total": 3,        "max_score": 1,        "hits": [            {                "_index": "book",                "_type": "novel",                "_id": "AV8i0JvQ4oF9V8mhiGCi",                "_score": 1,                "_source": {                    "word_count": "2000",                    "author": "chenjie",                    "title": "MySql从删库到跑路",                    "publish_date": "1994-09-27"                }            },            {                "_index": "book",                "_type": "novel",                "_id": "AV8jW8WW4oF9V8mhiGCp",                "_score": 1,                "_source": {                    "word_count": "4000",                    "author": "chenjie",                    "title": "chenjie传",                    "publish_date": "1994-09-27"                }            },            {                "_index": "book",                "_type": "novel",                "_id": "AV8i0A5k4oF9V8mhiGCh",                "_score": 1,                "_source": {                    "word_count": "1000",                    "author": "chenjie",                    "title": "Java从入门到放弃",                    "publish_date": "1995-08-19"                }            }        ]    }}

固定分数给定分数:

{"query":{"constant_score":{"filter":{"match":{"author":"chenjie"}},"boost":2}}}




8、使用should关键字,或逻辑

{"query":{"bool":{"should":[{"match":{"author":"chenjie"}},{"match":{"title":"java"}}]}}}


结果:

{    "took": 9,    "timed_out": false,    "_shards": {        "total": 3,        "successful": 3,        "skipped": 0,        "failed": 0    },    "hits": {        "total": 3,        "max_score": 1.2533233,        "hits": [            {                "_index": "book",                "_type": "novel",                "_id": "AV8i0A5k4oF9V8mhiGCh",                "_score": 1.2533233,                "_source": {                    "word_count": "1000",                    "author": "chenjie",                    "title": "Java从入门到放弃",                    "publish_date": "1995-08-19"                }            },            {                "_index": "book",                "_type": "novel",                "_id": "AV8i0JvQ4oF9V8mhiGCi",                "_score": 0.47000363,                "_source": {                    "word_count": "2000",                    "author": "chenjie",                    "title": "MySql从删库到跑路",                    "publish_date": "1994-09-27"                }            },            {                "_index": "book",                "_type": "novel",                "_id": "AV8jW8WW4oF9V8mhiGCp",                "_score": 0.47000363,                "_source": {                    "word_count": "4000",                    "author": "chenjie",                    "title": "chenjie传",                    "publish_date": "1994-09-27"                }            }        ]    }}


9、使用must完成与逻辑

{"query":{"bool":{"must":[{"match":{"author":"chenjie"}},{"match":{"title":"java"}}]}}}

结果:

{    "took": 14,    "timed_out": false,    "_shards": {        "total": 3,        "successful": 3,        "skipped": 0,        "failed": 0    },    "hits": {        "total": 1,        "max_score": 1.2533233,        "hits": [            {                "_index": "book",                "_type": "novel",                "_id": "AV8i0A5k4oF9V8mhiGCh",                "_score": 1.2533233,                "_source": {                    "word_count": "1000",                    "author": "chenjie",                    "title": "Java从入门到放弃",                    "publish_date": "1995-08-19"                }            }        ]    }}


9、must + filter

{"query":{"bool":{"must":[{"match":{"author":"chenjie"}},{"match":{"title":"java"}}],"filter":{"term":{"word_count":1000}}}}}

结果:

{    "took": 6,    "timed_out": false,    "_shards": {        "total": 3,        "successful": 3,        "skipped": 0,        "failed": 0    },    "hits": {        "total": 1,        "max_score": 1.2533233,        "hits": [            {                "_index": "book",                "_type": "novel",                "_id": "AV8i0A5k4oF9V8mhiGCh",                "_score": 1.2533233,                "_source": {                    "word_count": "1000",                    "author": "chenjie",                    "title": "Java从入门到放弃",                    "publish_date": "1995-08-19"                }            }        ]    }}

10、must not

{"query":{"bool":{"must_not":{"term":{"author":"chenjie"}}}}}


结果:

{    "took": 9,    "timed_out": false,    "_shards": {        "total": 3,        "successful": 3,        "skipped": 0,        "failed": 0    },    "hits": {        "total": 2,        "max_score": 1,        "hits": [            {                "_index": "book",                "_type": "novel",                "_id": "AV8i0UpQ4oF9V8mhiGCj",                "_score": 1,                "_source": {                    "word_count": "8000",                    "author": "路遥",                    "title": "平凡的世界",                    "publish_date": "2000-01-01"                }            },            {                "_index": "book",                "_type": "novel",                "_id": "1",                "_score": 1,                "_source": {                    "doc": {                        "title": "活着"                    },                    "title": "活着",                    "word_count": 9000,                    "author": "余华",                    "publish_date": "2017-10-16"                }            }        ]    }}



原创粉丝点击