博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
elasticsearch 深度搜索
阅读量:4230 次
发布时间:2019-05-26

本文共 35074 字,大约阅读时间需要 116 分钟。

结构化搜索

精确值查找

数据录入

[looking@master ~]$ curl -X POST "localhost:9200/my_store/products/_bulk?pretty" -H 'Content-Type: application/json' -d'> { "index": { "_id": 1 }}> { "price" : 10, "productID" : "XHDK-A-1293-#fJ3" }> { "index": { "_id": 2 }}> { "price" : 20, "productID" : "KDKE-B-9947-#kL5" }> { "index": { "_id": 3 }}> { "price" : 30, "productID" : "JODL-X-1937-#pV7" }> { "index": { "_id": 4 }}> { "price" : 30, "productID" : "QQPX-R-3956-#aD8" }> '{  "took" : 1688,  "errors" : false,  "items" : [    {      "index" : {        "_index" : "my_store",        "_type" : "products",        "_id" : "1",        "_version" : 1,        "result" : "created",        "_shards" : {          "total" : 2,          "successful" : 1,          "failed" : 0        },        "_seq_no" : 0,        "_primary_term" : 1,        "status" : 201      }    },    {      "index" : {        "_index" : "my_store",        "_type" : "products",        "_id" : "2",        "_version" : 1,        "result" : "created",        "_shards" : {          "total" : 2,          "successful" : 1,          "failed" : 0        },        "_seq_no" : 1,        "_primary_term" : 1,        "status" : 201      }    },    {      "index" : {        "_index" : "my_store",        "_type" : "products",        "_id" : "3",        "_version" : 1,        "result" : "created",        "_shards" : {          "total" : 2,          "successful" : 1,          "failed" : 0        },        "_seq_no" : 2,        "_primary_term" : 1,        "status" : 201      }    },    {      "index" : {        "_index" : "my_store",        "_type" : "products",        "_id" : "4",        "_version" : 1,        "result" : "created",        "_shards" : {          "total" : 2,          "successful" : 1,          "failed" : 0        },        "_seq_no" : 3,        "_primary_term" : 1,        "status" : 201      }    }  ]}

term 查询数字

[looking@master ~]$ curl -X GET "localhost:9200/my_store/products/_search?pretty" -H 'Content-Type: application/json' -d'> {>     "query" : {>         "constant_score" : { >             "filter" : {>                 "term" : { >                     "price" : 20>                 }>             }>         }>     }> }> '{  "took" : 275,  "timed_out" : false,  "_shards" : {    "total" : 1,    "successful" : 1,    "skipped" : 0,    "failed" : 0  },  "hits" : {    "total" : {      "value" : 1,      "relation" : "eq"    },    "max_score" : 1.0,    "hits" : [      {        "_index" : "my_store",        "_type" : "products",        "_id" : "2",        "_score" : 1.0,        "_source" : {          "price" : 20,          "productID" : "KDKE-B-9947-#kL5"        }      }    ]  }}[looking@master ~]$ curl -X GET "localhost:9200/my_store/products/_search?pretty" -H 'Content-Type: application/json' -d'> {>     "query" : {>         "term" : {>             "price" : 20>         }>     }> }> '{  "took" : 16,  "timed_out" : false,  "_shards" : {    "total" : 1,    "successful" : 1,    "skipped" : 0,    "failed" : 0  },  "hits" : {    "total" : {      "value" : 1,      "relation" : "eq"    },    "max_score" : 1.0,    "hits" : [      {        "_index" : "my_store",        "_type" : "products",        "_id" : "2",        "_score" : 1.0,        "_source" : {          "price" : 20,          "productID" : "KDKE-B-9947-#kL5"        }      }    ]  }}

term 查询文本 

[looking@master ~]$ curl -X GET "localhost:9200/my_store/products/_search?pretty" -H 'Content-Type: application/json' -d'> {>     "query" : {>         "constant_score" : {>             "filter" : {>                 "term" : {>                     "productID" : "XHDK-A-1293-#fJ3">                 }>             }>         }>     }> }> '{  "took" : 75,  "timed_out" : false,  "_shards" : {    "total" : 1,    "successful" : 1,    "skipped" : 0,    "failed" : 0  },  "hits" : {    "total" : {      "value" : 0,      "relation" : "eq"    },    "max_score" : null,    "hits" : [ ]  }}[looking@master ~]$ curl -X GET "localhost:9200/my_store/_analyze?pretty" -H 'Content-Type: application/json' -d'> {>   "field": "productID",>   "text": "XHDK-A-1293-#fJ3"> }> '{  "tokens" : [    {      "token" : "xhdk",      "start_offset" : 0,      "end_offset" : 4,      "type" : "
", "position" : 0 }, { "token" : "a", "start_offset" : 5, "end_offset" : 6, "type" : "
", "position" : 1 }, { "token" : "1293", "start_offset" : 7, "end_offset" : 11, "type" : "
", "position" : 2 }, { "token" : "fj3", "start_offset" : 13, "end_offset" : 16, "type" : "
", "position" : 3 } ]}

重新索引并设置 productID 字段为精确值(现在可以通过 productID 来进行查询了)。

[looking@master ~]$ curl -X PUT "localhost:9200/my_store?include_type_name=true&pretty" -H 'Content-Type: application/json' -d'> {>     "mappings" : {>         "products" : {>             "properties" : {>                 "productID" : {>                     "type" : "keyword">                 }>             }>         }>     }> }> '{  "acknowledged" : true,  "shards_acknowledged" : true,  "index" : "my_store"}[looking@master ~]$ curl -X POST "localhost:9200/my_store/products/_bulk?pretty" -H 'Content-Type: application/json' -d'> { "index": { "_id": 1 }}> { "price" : 10, "productID" : "XHDK-A-1293-#fJ3" }> { "index": { "_id": 2 }}> { "price" : 20, "productID" : "KDKE-B-9947-#kL5" }> { "index": { "_id": 3 }}> { "price" : 30, "productID" : "JODL-X-1937-#pV7" }> { "index": { "_id": 4 }}> { "price" : 30, "productID" : "QQPX-R-3956-#aD8" }> '{  "took" : 121,  "errors" : false,  "items" : [    {      "index" : {        "_index" : "my_store",        "_type" : "products",        "_id" : "1",        "_version" : 1,        "result" : "created",        "_shards" : {          "total" : 2,          "successful" : 1,          "failed" : 0        },        "_seq_no" : 0,        "_primary_term" : 1,        "status" : 201      }    },    {      "index" : {        "_index" : "my_store",        "_type" : "products",        "_id" : "2",        "_version" : 1,        "result" : "created",        "_shards" : {          "total" : 2,          "successful" : 1,          "failed" : 0        },        "_seq_no" : 1,        "_primary_term" : 1,        "status" : 201      }    },    {      "index" : {        "_index" : "my_store",        "_type" : "products",        "_id" : "3",        "_version" : 1,        "result" : "created",        "_shards" : {          "total" : 2,          "successful" : 1,          "failed" : 0        },        "_seq_no" : 2,        "_primary_term" : 1,        "status" : 201      }    },    {      "index" : {        "_index" : "my_store",        "_type" : "products",        "_id" : "4",        "_version" : 1,        "result" : "created",        "_shards" : {          "total" : 2,          "successful" : 1,          "failed" : 0        },        "_seq_no" : 3,        "_primary_term" : 1,        "status" : 201      }    }  ]}[looking@master ~]$ curl -X GET "localhost:9200/my_store/products/_search?pretty" -H 'Content-Type: application/json' -d'> {>     "query" : {>         "constant_score" : {>             "filter" : {>                 "term" : {>                     "productID" : "XHDK-A-1293-#fJ3">                 }>             }>         }>     }> }> '{  "took" : 65,  "timed_out" : false,  "_shards" : {    "total" : 1,    "successful" : 1,    "skipped" : 0,    "failed" : 0  },  "hits" : {    "total" : {      "value" : 1,      "relation" : "eq"    },    "max_score" : 1.0,    "hits" : [      {        "_index" : "my_store",        "_type" : "products",        "_id" : "1",        "_score" : 1.0,        "_source" : {          "price" : 10,          "productID" : "XHDK-A-1293-#fJ3"        }      }    ]  }}

组合过滤器

布尔过滤器

[looking@master ~]$ curl -X GET "localhost:9200/my_store/products/_search?pretty" -H 'Content-Type: application/json' -d'> {>    "query" : {>       "bool" : {>          "filter" : {>             "bool" : {>               "should" : [>                  { "term" : {"price" : 20}},>                  { "term" : {"productID" : "XHDK-A-1293-#fJ3"}}>               ],>               "must_not" : {>                  "term" : {"price" : 30}>               }>            }>          }>       }>    }> }> '{  "took" : 155,  "timed_out" : false,  "_shards" : {    "total" : 1,    "successful" : 1,    "skipped" : 0,    "failed" : 0  },  "hits" : {    "total" : {      "value" : 2,      "relation" : "eq"    },    "max_score" : 0.0,    "hits" : [      {        "_index" : "my_store",        "_type" : "products",        "_id" : "1",        "_score" : 0.0,        "_source" : {          "price" : 10,          "productID" : "XHDK-A-1293-#fJ3"        }      },      {        "_index" : "my_store",        "_type" : "products",        "_id" : "2",        "_score" : 0.0,        "_source" : {          "price" : 20,          "productID" : "KDKE-B-9947-#kL5"        }      }    ]  }}

嵌套布尔过滤器

[looking@master ~]$ curl -X GET "localhost:9200/my_store/products/_search?pretty" -H 'Content-Type: application/json' -d'> {>    "query" : {>       "bool" : {>          "filter" : {>             "bool" : {>               "should" : [>                 { "term" : {"productID" : "KDKE-B-9947-#kL5"}}, >                 { "bool" : { >                   "must" : [>                     { "term" : {"productID" : "JODL-X-1937-#pV7"}}, >                     { "term" : {"price" : 30}} >                   ]>                 }}>               ]>            }>          }>       }>    }> }> '{  "took" : 8,  "timed_out" : false,  "_shards" : {    "total" : 1,    "successful" : 1,    "skipped" : 0,    "failed" : 0  },  "hits" : {    "total" : {      "value" : 2,      "relation" : "eq"    },    "max_score" : 0.0,    "hits" : [      {        "_index" : "my_store",        "_type" : "products",        "_id" : "2",        "_score" : 0.0,        "_source" : {          "price" : 20,          "productID" : "KDKE-B-9947-#kL5"        }      },      {        "_index" : "my_store",        "_type" : "products",        "_id" : "3",        "_score" : 0.0,        "_source" : {          "price" : 30,          "productID" : "JODL-X-1937-#pV7"        }      }    ]  }}

多个精确值

[looking@master ~]$ curl -X GET "localhost:9200/my_store/products/_search?pretty" -H 'Content-Type: application/json' -d'> {>     "query" : {>         "constant_score" : {>             "filter" : {>                 "terms" : { >                     "price" : [20, 30]>                 }>             }>         }>     }> }> '{  "took" : 14,  "timed_out" : false,  "_shards" : {    "total" : 1,    "successful" : 1,    "skipped" : 0,    "failed" : 0  },  "hits" : {    "total" : {      "value" : 3,      "relation" : "eq"    },    "max_score" : 1.0,    "hits" : [      {        "_index" : "my_store",        "_type" : "products",        "_id" : "2",        "_score" : 1.0,        "_source" : {          "price" : 20,          "productID" : "KDKE-B-9947-#kL5"        }      },      {        "_index" : "my_store",        "_type" : "products",        "_id" : "3",        "_score" : 1.0,        "_source" : {          "price" : 30,          "productID" : "JODL-X-1937-#pV7"        }      },      {        "_index" : "my_store",        "_type" : "products",        "_id" : "4",        "_score" : 1.0,        "_source" : {          "price" : 30,          "productID" : "QQPX-R-3956-#aD8"        }      }    ]  }}

范围

数字范围

[looking@master ~]$ curl -X GET "localhost:9200/my_store/products/_search?pretty" -H 'Content-Type: application/json' -d'> {>     "query" : {>         "constant_score" : {>             "filter" : {>                 "range" : {>                     "price" : {>                         "gte" : 20,>                         "lt"  : 40>                     }>                 }>             }>         }>     }> }> '{  "took" : 79,  "timed_out" : false,  "_shards" : {    "total" : 1,    "successful" : 1,    "skipped" : 0,    "failed" : 0  },  "hits" : {    "total" : {      "value" : 3,      "relation" : "eq"    },    "max_score" : 1.0,    "hits" : [      {        "_index" : "my_store",        "_type" : "products",        "_id" : "2",        "_score" : 1.0,        "_source" : {          "price" : 20,          "productID" : "KDKE-B-9947-#kL5"        }      },      {        "_index" : "my_store",        "_type" : "products",        "_id" : "3",        "_score" : 1.0,        "_source" : {          "price" : 30,          "productID" : "JODL-X-1937-#pV7"        }      },      {        "_index" : "my_store",        "_type" : "products",        "_id" : "4",        "_score" : 1.0,        "_source" : {          "price" : 30,          "productID" : "QQPX-R-3956-#aD8"        }      }    ]  }}

日期范围

# 假设有 timestamp 字段的话可以这么写,不过这儿并没有curl -X GET "localhost:9200/my_store/products/_search?pretty" -H 'Content-Type: application/json' -d'{    "query" : {        "constant_score" : {            "filter" : {                "range" : {                    "timestamp" : {                        "gt" : "2014-01-01 00:00:00",                        "lt" : "2014-01-07 00:00:00"                    }                }            }        }    }}'curl -X GET "localhost:9200/my_store/products/_search?pretty" -H 'Content-Type: application/json' -d'{    "query" : {        "constant_score" : {            "filter" : {                "range" : {                    "timestamp" : {                        "gt" : "now-1h"                    }                }            }        }    }}'curl -X GET "localhost:9200/my_store/products/_search?pretty" -H 'Content-Type: application/json' -d'{    "query" : {        "constant_score" : {            "filter" : {                "range" : {                    "timestamp" : {                        "gt" : "2014-01-01 00:00:00",                        "lt" : "2014-01-01 00:00:00||+1M"                    }                }            }        }    }}'

字符串范围

[looking@master ~]$ curl -X GET "localhost:9200/my_store/products/_search?pretty" -H 'Content-Type: application/json' -d'> {>     "query" : {>         "constant_score" : {>             "filter" : {>                 "range" : {>                     "productID" : {>                         "gte" : "O",>                         "lt" :  "Z">                     }>                 }>             }>         }>     }> }> '{  "took" : 357,  "timed_out" : false,  "_shards" : {    "total" : 1,    "successful" : 1,    "skipped" : 0,    "failed" : 0  },  "hits" : {    "total" : {      "value" : 2,      "relation" : "eq"    },    "max_score" : 1.0,    "hits" : [      {        "_index" : "my_store",        "_type" : "products",        "_id" : "1",        "_score" : 1.0,        "_source" : {          "price" : 10,          "productID" : "XHDK-A-1293-#fJ3"        }      },      {        "_index" : "my_store",        "_type" : "products",        "_id" : "4",        "_score" : 1.0,        "_source" : {          "price" : 30,          "productID" : "QQPX-R-3956-#aD8"        }      }    ]  }}

处理 NULL 值

数据录入

curl -X POST "localhost:9200/my_index/posts/_bulk?pretty" -H 'Content-Type: application/json' -d'{ "index": { "_id": "1"              }}{ "tags" : ["search"]                }{ "index": { "_id": "2"              }}{ "tags" : ["search", "open_source"] }{ "index": { "_id": "3"              }}{ "other_field" : "some data"        }{ "index": { "_id": "4"              }}{ "tags" : null                      }{ "index": { "_id": "5"              }}{ "tags" : ["search", null]          }'{  "took" : 288,  "errors" : false,  "items" : [    {      "index" : {        "_index" : "my_index",        "_type" : "posts",        "_id" : "1",        "_version" : 1,        "result" : "created",        "_shards" : {          "total" : 2,          "successful" : 1,          "failed" : 0        },        "_seq_no" : 0,        "_primary_term" : 1,        "status" : 201      }    },    ...    {      "index" : {        "_index" : "my_index",        "_type" : "posts",        "_id" : "5",        "_version" : 1,        "result" : "created",        "_shards" : {          "total" : 2,          "successful" : 1,          "failed" : 0        },        "_seq_no" : 4,        "_primary_term" : 1,        "status" : 201      }    }  ]}

存在查询 

[looking@master ~]$ curl -X GET "localhost:9200/my_index/posts/_search?pretty" -H 'Content-Type: application/json' -d'> {>     "query" : {>         "constant_score" : {>             "filter" : {>                 "exists" : { "field" : "tags" }>             }>         }>     }> }> '{  "took" : 46,  "timed_out" : false,  "_shards" : {    "total" : 1,    "successful" : 1,    "skipped" : 0,    "failed" : 0  },  "hits" : {    "total" : {      "value" : 3,      "relation" : "eq"    },    "max_score" : 1.0,    "hits" : [      {        "_index" : "my_index",        "_type" : "posts",        "_id" : "1",        "_score" : 1.0,        "_source" : {          "tags" : [            "search"          ]        }      },      {        "_index" : "my_index",        "_type" : "posts",        "_id" : "2",        "_score" : 1.0,        "_source" : {          "tags" : [            "search",            "open_source"          ]        }      },      {        "_index" : "my_index",        "_type" : "posts",        "_id" : "5",        "_score" : 1.0,        "_source" : {          "tags" : [            "search",            null          ]        }      }    ]  }}

缺失查询

[looking@master ~]$ curl -X GET "localhost:9200/my_index/posts/_search?pretty" -H 'Content-Type: application/json' -d'> {>     "query" : {>         "bool" : {>             "must_not" : {>                 "exists" : { "field" : "tags" }>             }>         }>     }> }> '{  "took" : 28,  "timed_out" : false,  "_shards" : {    "total" : 1,    "successful" : 1,    "skipped" : 0,    "failed" : 0  },  "hits" : {    "total" : {      "value" : 2,      "relation" : "eq"    },    "max_score" : 0.0,    "hits" : [      {        "_index" : "my_index",        "_type" : "posts",        "_id" : "3",        "_score" : 0.0,        "_source" : {          "other_field" : "some data"        }      },      {        "_index" : "my_index",        "_type" : "posts",        "_id" : "4",        "_score" : 0.0,        "_source" : {          "tags" : null        }      }    ]  }}

全文搜索

匹配查询

数据录入

[looking@master ~]$ curl -X DELETE "localhost:9200/my_index?pretty"{  "acknowledged" : true}[looking@master ~]$ curl -X PUT "localhost:9200/my_index?pretty" -H 'Content-Type: application/json' -d'> { "settings": { "number_of_shards": 1 }}> '{  "acknowledged" : true,  "shards_acknowledged" : true,  "index" : "my_index"}[looking@master ~]$ curl -X POST "localhost:9200/my_index/my_type/_bulk?pretty" -H 'Content-Type: application/json' -d'> { "index": { "_id": 1 }}> { "title": "The quick brown fox" }> { "index": { "_id": 2 }}> { "title": "The quick brown fox jumps over the lazy dog" }> { "index": { "_id": 3 }}> { "title": "The quick brown fox jumps over the quick dog" }> { "index": { "_id": 4 }}> { "title": "Brown fox brown dog" }> '{  "took" : 73,  "errors" : false,  "items" : [    {      "index" : {        "_index" : "my_index",        "_type" : "my_type",        "_id" : "1",        "_version" : 1,        "result" : "created",        "_shards" : {          "total" : 2,          "successful" : 1,          "failed" : 0        },        "_seq_no" : 0,        "_primary_term" : 1,        "status" : 201      }    },    {      "index" : {        "_index" : "my_index",        "_type" : "my_type",        "_id" : "2",        "_version" : 1,        "result" : "created",        "_shards" : {          "total" : 2,          "successful" : 1,          "failed" : 0        },        "_seq_no" : 1,        "_primary_term" : 1,        "status" : 201      }    },    {      "index" : {        "_index" : "my_index",        "_type" : "my_type",        "_id" : "3",        "_version" : 1,        "result" : "created",        "_shards" : {          "total" : 2,          "successful" : 1,          "failed" : 0        },        "_seq_no" : 2,        "_primary_term" : 1,        "status" : 201      }    },    {      "index" : {        "_index" : "my_index",        "_type" : "my_type",        "_id" : "4",        "_version" : 1,        "result" : "created",        "_shards" : {          "total" : 2,          "successful" : 1,          "failed" : 0        },        "_seq_no" : 3,        "_primary_term" : 1,        "status" : 201      }    }  ]}

单词查询

[looking@master ~]$ curl -X GET "localhost:9200/my_index/my_type/_search?pretty" -H 'Content-Type: application/json' -d'> {>     "query": {>         "match": {>             "title": "QUICK!">         }>     }> }> '{  "took" : 76,  "timed_out" : false,  "_shards" : {    "total" : 1,    "successful" : 1,    "skipped" : 0,    "failed" : 0  },  "hits" : {    "total" : {      "value" : 3,      "relation" : "eq"    },    "max_score" : 0.44255546,    "hits" : [      {        "_index" : "my_index",        "_type" : "my_type",        "_id" : "3",        "_score" : 0.44255546,        "_source" : {          "title" : "The quick brown fox jumps over the quick dog"        }      },      {        "_index" : "my_index",        "_type" : "my_type",        "_id" : "1",        "_score" : 0.42327404,        "_source" : {          "title" : "The quick brown fox"        }      },      {        "_index" : "my_index",        "_type" : "my_type",        "_id" : "2",        "_score" : 0.30818442,        "_source" : {          "title" : "The quick brown fox jumps over the lazy dog"        }      }    ]  }}

多词查询

[looking@master ~]$ curl -X GET "localhost:9200/my_index/my_type/_search?pretty" -H 'Content-Type: application/json' -d'> {>     "query": {>         "match": {>             "title": "BROWN DOG!">         }>     }> }> '{  "took" : 75,  "timed_out" : false,  "_shards" : {    "total" : 1,    "successful" : 1,    "skipped" : 0,    "failed" : 0  },  "hits" : {    "total" : {      "value" : 4,      "relation" : "eq"    },    "max_score" : 0.58571666,    "hits" : [      {        "_index" : "my_index",        "_type" : "my_type",        "_id" : "4",        "_score" : 0.58571666,        "_source" : {          "title" : "Brown fox brown dog"        }      },      {        "_index" : "my_index",        "_type" : "my_type",        "_id" : "2",        "_score" : 0.399221,        "_source" : {          "title" : "The quick brown fox jumps over the lazy dog"        }      },      {        "_index" : "my_index",        "_type" : "my_type",        "_id" : "3",        "_score" : 0.399221,        "_source" : {          "title" : "The quick brown fox jumps over the quick dog"        }      },      {        "_index" : "my_index",        "_type" : "my_type",        "_id" : "1",        "_score" : 0.12503365,        "_source" : {          "title" : "The quick brown fox"        }      }    ]  }}

提高精度(and)

[looking@master ~]$ curl -X GET "localhost:9200/my_index/my_type/_search?pretty" -H 'Content-Type: application/json' -d'> {>     "query": {>         "match": {>             "title": {      >                 "query":    "BROWN DOG!",>                 "operator": "and">             }>         }>     }> }> '{  "took" : 27,  "timed_out" : false,  "_shards" : {    "total" : 1,    "successful" : 1,    "skipped" : 0,    "failed" : 0  },  "hits" : {    "total" : {      "value" : 3,      "relation" : "eq"    },    "max_score" : 0.58571666,    "hits" : [      {        "_index" : "my_index",        "_type" : "my_type",        "_id" : "4",        "_score" : 0.58571666,        "_source" : {          "title" : "Brown fox brown dog"        }      },      {        "_index" : "my_index",        "_type" : "my_type",        "_id" : "2",        "_score" : 0.399221,        "_source" : {          "title" : "The quick brown fox jumps over the lazy dog"        }      },      {        "_index" : "my_index",        "_type" : "my_type",        "_id" : "3",        "_score" : 0.399221,        "_source" : {          "title" : "The quick brown fox jumps over the quick dog"        }      }    ]  }}

控制精度

[looking@master ~]$ curl -X GET "localhost:9200/my_index/my_type/_search?pretty" -H 'Content-Type: application/json' -d'> {>   "query": {>     "match": {>       "title": {>         "query":  "quick brown dog",>         "minimum_should_match": "75%">       }>     }>   }> }> '{  "took" : 5,  "timed_out" : false,  "_shards" : {    "total" : 1,    "successful" : 1,    "skipped" : 0,    "failed" : 0  },  "hits" : {    "total" : {      "value" : 4,      "relation" : "eq"    },    "max_score" : 0.84177643,    "hits" : [      {        "_index" : "my_index",        "_type" : "my_type",        "_id" : "3",        "_score" : 0.84177643,        "_source" : {          "title" : "The quick brown fox jumps over the quick dog"        }      },      {        "_index" : "my_index",        "_type" : "my_type",        "_id" : "2",        "_score" : 0.7074054,        "_source" : {          "title" : "The quick brown fox jumps over the lazy dog"        }      },      {        "_index" : "my_index",        "_type" : "my_type",        "_id" : "4",        "_score" : 0.58571666,        "_source" : {          "title" : "Brown fox brown dog"        }      },      {        "_index" : "my_index",        "_type" : "my_type",        "_id" : "1",        "_score" : 0.54830766,        "_source" : {          "title" : "The quick brown fox"        }      }    ]  }}

组合查询

[looking@master ~]$ curl -X GET "localhost:9200/my_index/my_type/_search?pretty" -H 'Content-Type: application/json' -d'> {>   "query": {>     "bool": {>       "must":     { "match": { "title": "quick" }},>       "must_not": { "match": { "title": "lazy"  }},>       "should": [>                   { "match": { "title": "brown" }},>                   { "match": { "title": "dog"   }}>       ]>     }>   }> }> '{  "took" : 239,  "timed_out" : false,  "_shards" : {    "total" : 1,    "successful" : 1,    "skipped" : 0,    "failed" : 0  },  "hits" : {    "total" : {      "value" : 2,      "relation" : "eq"    },    "max_score" : 0.8417765,    "hits" : [      {        "_index" : "my_index",        "_type" : "my_type",        "_id" : "3",        "_score" : 0.8417765,        "_source" : {          "title" : "The quick brown fox jumps over the quick dog"        }      },      {        "_index" : "my_index",        "_type" : "my_type",        "_id" : "1",        "_score" : 0.54830766,        "_source" : {          "title" : "The quick brown fox"        }      }    ]  }}
[looking@master ~]$ curl -X GET "localhost:9200/my_index/my_type/_search?pretty" -H 'Content-Type: application/json' -d'> {>   "query": {>     "bool": {>       "should": [>         { "match": { "title": "brown" }},>         { "match": { "title": "fox"   }},>         { "match": { "title": "dog"   }}>       ],>       "minimum_should_match": 2 >     }>   }> }> '{  "took" : 4,  "timed_out" : false,  "_shards" : {    "total" : 1,    "successful" : 1,    "skipped" : 0,    "failed" : 0  },  "hits" : {    "total" : {      "value" : 4,      "relation" : "eq"    },    "max_score" : 0.71075034,    "hits" : [      {        "_index" : "my_index",        "_type" : "my_type",        "_id" : "4",        "_score" : 0.71075034,        "_source" : {          "title" : "Brown fox brown dog"        }      },      {        "_index" : "my_index",        "_type" : "my_type",        "_id" : "2",        "_score" : 0.49025756,        "_source" : {          "title" : "The quick brown fox jumps over the lazy dog"        }      },      {        "_index" : "my_index",        "_type" : "my_type",        "_id" : "3",        "_score" : 0.49025756,        "_source" : {          "title" : "The quick brown fox jumps over the quick dog"        }      },      {        "_index" : "my_index",        "_type" : "my_type",        "_id" : "1",        "_score" : 0.2500673,        "_source" : {          "title" : "The quick brown fox"        }      }    ]  }}

布尔匹配

curl -X GET "localhost:9200/my_index/my_type/_search?pretty" -H 'Content-Type: application/json' -d'{  "query": {    "bool": {      "should": [        { "match": { "title": "brown" }},        { "match": { "title": "fox"   }}      ]    }  }}'curl -X GET "localhost:9200/my_index/my_type/_search?pretty" -H 'Content-Type: application/json' -d'{  "query": {    "bool": {      "should": [        { "match": { "title": "brown fox" }}      ]    }  }}'

提升权重

[looking@master ~]$ curl -X GET "localhost:9200/_search?pretty" -H 'Content-Type: application/json' -d'> {>     "query": {>         "bool": {>             "must": {>                 "match": {>                     "content": { >                         "query":    "full text search",>                         "operator": "and">                     }>                 }>             },>             "should": [ >                 { "match": { "content": "Elasticsearch" }},>                 { "match": { "content": "Lucene"        }}>             ]>         }>     }> }> '{  "took" : 64,  "timed_out" : false,  "_shards" : {    "total" : 9,    "successful" : 9,    "skipped" : 0,    "failed" : 0  },  "hits" : {    "total" : {      "value" : 0,      "relation" : "eq"    },    "max_score" : null,    "hits" : [ ]  }}[looking@master ~]$ curl -X GET "localhost:9200/_search?pretty" -H 'Content-Type: application/json' -d'> {>     "query": {>         "bool": {>             "must": {>                 "match": {  >                     "content": {>                         "query":    "full text search",>                         "operator": "and">                     }>                 }>             },>             "should": [>                 { "match": {>                     "content": {>                         "query": "Elasticsearch",>                         "boost": 3 >                     }>                 }},>                 { "match": {>                     "content": {>                         "query": "Lucene",>                         "boost": 2 >                     }>                 }}>             ]>         }>     }> }> '{  "took" : 6,  "timed_out" : false,  "_shards" : {    "total" : 9,    "successful" : 9,    "skipped" : 0,    "failed" : 0  },  "hits" : {    "total" : {      "value" : 0,      "relation" : "eq"    },    "max_score" : null,    "hits" : [ ]  }}

控制分析

新增字段

[looking@master ~]$ curl -X PUT "localhost:9200/my_index/_mapping?pretty" -H 'Content-Type: application/json' -d'> {>     "properties": {>         "english_title": {>             "type":     "text",>             "analyzer": "english">         }>     }> }> '{  "acknowledged" : true}
[looking@master ~]$ curl -X GET "localhost:9200/my_index/_analyze?pretty" -H 'Content-Type: application/json' -d'> {>   "field": "my_type.title",   >   "text": "Foxes"> }> '{  "tokens" : [    {      "token" : "foxes",      "start_offset" : 0,      "end_offset" : 5,      "type" : "
", "position" : 0 } ]}

 

[looking@master ~]$ curl -X GET "localhost:9200/my_index/my_type/_validate/query?explain&pretty" -H 'Content-Type: application/json' -d'> {>     "query": {>         "bool": {>             "should": [>                 { "match": { "title":         "Foxes"}},>                 { "match": { "english_title": "Foxes"}}>             ]>         }>     }> }> '{  "_shards" : {    "total" : 1,    "successful" : 1,    "failed" : 0  },  "valid" : true,  "explanations" : [    {      "index" : "my_index",      "valid" : true,      "explanation" : "+(title:foxes english_title:fox) #*:*"    }  ]}

多字段搜索

多字符串查询

[looking@master ~]$ curl -X GET "localhost:9200/_search?pretty" -H 'Content-Type: application/json' -d'> {>   "query": {>     "bool": {>       "should": [>         { "match": { "title":  "War and Peace" }},>         { "match": { "author": "Leo Tolstoy"   }}>       ]>     }>   }> }> '{  "took" : 267,  "timed_out" : false,  "_shards" : {    "total" : 9,    "successful" : 9,    "skipped" : 0,    "failed" : 0  },  "hits" : {    "total" : {      "value" : 0,      "relation" : "eq"    },    "max_score" : null,    "hits" : [ ]  }}[looking@master ~]$ curl -X GET "localhost:9200/_search?pretty" -H 'Content-Type: application/json' -d'> {>   "query": {>     "bool": {>       "should": [>         { "match": { "title":  "War and Peace" }},>         { "match": { "author": "Leo Tolstoy"   }},>         { "bool":  {>           "should": [>             { "match": { "translator": "Constance Garnett" }},>             { "match": { "translator": "Louise Maude"      }}>           ]>         }}>       ]>     }>   }> }> '{  "took" : 10,  "timed_out" : false,  "_shards" : {    "total" : 9,    "successful" : 9,    "skipped" : 0,    "failed" : 0  },  "hits" : {    "total" : {      "value" : 0,      "relation" : "eq"    },    "max_score" : null,    "hits" : [ ]  }}[looking@master ~]$ curl -X GET "localhost:9200/_search?pretty" -H 'Content-Type: application/json' -d'> {>   "query": {>     "bool": {>       "should": [>         { "match": { >             "title":  {>               "query": "War and Peace",>               "boost": 2>         }}},>         { "match": { >             "author":  {>               "query": "Leo Tolstoy",>               "boost": 2>         }}},>         { "bool":  { >             "should": [>               { "match": { "translator": "Constance Garnett" }},>               { "match": { "translator": "Louise Maude"      }}>             ]>         }}>       ]>     }>   }> }> '{  "took" : 7,  "timed_out" : false,  "_shards" : {    "total" : 9,    "successful" : 9,    "skipped" : 0,    "failed" : 0  },  "hits" : {    "total" : {      "value" : 0,      "relation" : "eq"    },    "max_score" : null,    "hits" : [ ]  }}

最佳字段

[looking@master ~]$ curl -X GET "localhost:9200/_search?pretty" -H 'Content-Type: application/json' -d'> {>   "query": {>     "bool": {>       "should": [>         { "match": { "title":  "Brown fox" }},>         { "match": { "body": "Brown fox"   }}>       ]>     }>   }> }> '{  "took" : 535,  "timed_out" : false,  "_shards" : {    "total" : 9,    "successful" : 9,    "skipped" : 0,    "failed" : 0  },  "hits" : {    "total" : {      "value" : 4,      "relation" : "eq"    },    "max_score" : 1.6611805,    "hits" : [      {        "_index" : "my_index",        "_type" : "my_type",        "_id" : "4",        "_score" : 1.6611805,        "_source" : {          "title" : "Brown fox brown dog"        }      },      {        "_index" : "my_index",        "_type" : "my_type",        "_id" : "3",        "_score" : 0.9979023,        "_source" : {          "title" : "The quick brown fox jumps over the quick dog"        }      },      {        "_index" : "my_index",        "_type" : "my_type",        "_id" : "2",        "_score" : 0.7026868,        "_source" : {          "title" : "Keeping pets healthy",          "body" : "My quick brown fox eats rabbits on a regular basis."        }      },      {        "_index" : "my_index",        "_type" : "my_type",        "_id" : "1",        "_score" : 0.62098616,        "_source" : {          "title" : "Quick brown rabbits",          "body" : "Brown rabbits are commonly seen."        }      }    ]  }}

to be continued

转载地址:http://bjjqi.baihongyu.com/

你可能感兴趣的文章
ffs的另外一种实现方法
查看>>
strtol的用法
查看>>
工作队列的使用
查看>>
让vim显示空格,及tab字符 vim 多行注释
查看>>
利用mmc_test.c研究mmc模块
查看>>
tasklet、wait_queue、completion、work_queue用法总结
查看>>
int (*func(int)) (int *,int)
查看>>
在Ubuntu上下载、编译和安装Android最新内核源代码(Linux Kernel
查看>>
Linux内核同步机制API函数:宏:spin_lock_init ( )
查看>>
driver_register 理解
查看>>
copy_from_user && copy_to_user
查看>>
device_register
查看>>
Android上C++对象的自动回收机制分析
查看>>
从spin_lock到spin_lock_irqsave
查看>>
sdio 驱动
查看>>
vim 常用用法
查看>>
更好就足够了吗?| 驱动变革
查看>>
技术选型指南
查看>>
在一家技术公司做媒体
查看>>
项目管理的三个关键
查看>>