动态

详情 返回 返回

elasticsearch查詢練習題19個-part1 - 动态 详情

參考鏈接

參考鏈接中的版本有些已經不適用,但是題目可以用;我這裏使用的版本是7.17.29

0.數據準備:

POST bookdb/_bulk
{"index":{"_id":1}}
{"title":"Elasticsearch: The Definitive Guide","authors":["clinton gormley","zachary tong"],"summary":"A distibuted real-time search and analytics engine","publish_date":"2015-02-07","num_reviews":20,"publisher":"oreilly"}
{"index":{"_id":2}}
{"title":"Taming Text: How to Find, Organize, and Manipulate It","authors":["grant ingersoll","thomas morton","drew farris"],"summary":"organize text using approaches such as full-text search, proper name recognition, clustering, tagging, information extraction, and summarization","publish_date":"2013-01-24","num_reviews":12,"publisher":"manning"}
{"index":{"_id":3}}
{"title":"Elasticsearch in Action","authors":["radu gheorge","matthew lee hinman","roy russo"],"summary":"build scalable search applications using Elasticsearch without having to do complex low-level programming or understand advanced data science algorithms","publish_date":"2015-12-03","num_reviews":18,"publisher":"manning"}
{"index":{"_id":4}}
{"title":"Solr in Action","authors":["trey grainger","timothy potter"],"summary":"Comprehensive guide to implementing a scalable search engine using Apache Solr","publish_date":"2014-04-05","num_reviews":23,"publisher":"manning"}

1.match:基本查詢

title列包含"in Action"的
GET bookdb/_search
{
  "query": {
    "match": {
      "title": "in action"
    }
  }
}

結果:

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.6323127,
    "hits" : [
      {
        "_index" : "bookdb",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 1.6323127,
        "_source" : {
          "title" : "Elasticsearch in Action",
          "authors" : [
            "radu gheorge",
            "matthew lee hinman",
            "roy russo"
          ],
          "summary" : "build scalable search applications using Elasticsearch without having to do complex low-level programming or understand advanced data science algorithms",
          "publish_date" : "2015-12-03",
          "num_reviews" : 18,
          "publisher" : "manning"
        }
      },
      {
        "_index" : "bookdb",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 1.6323127,
        "_source" : {
          "title" : "Solr in Action",
          "authors" : [
            "trey grainger",
            "timothy potter"
          ],
          "summary" : "Comprehensive guide to implementing a scalable search engine using Apache Solr",
          "publish_date" : "2014-04-05",
          "num_reviews" : 23,
          "publisher" : "manning"
        }
      }
    ]
  }
}

2.多字段(multi_match)查詢

multi_match:查詢任一字段包含 Guide 的記錄

2.1 所有字段(不指定列)

GET bookdb/_search
{
  "query": {
    "multi_match": {
      "query": "Guide"
    }
  }
}

結果:

{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.3278645,
    "hits" : [
      {
        "_index" : "bookdb",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 1.3278645,
        "_source" : {
          "title" : "Solr in Action",
          "authors" : [
            "trey grainger",
            "timothy potter"
          ],
          "summary" : "Comprehensive guide to implementing a scalable search engine using Apache Solr",
          "publish_date" : "2014-04-05",
          "num_reviews" : 23,
          "publisher" : "manning"
        }
      },
      {
        "_index" : "bookdb",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.2871116,
        "_source" : {
          "title" : "Elasticsearch: The Definitive Guide",
          "authors" : [
            "clinton gormley",
            "zachary tong"
          ],
          "summary" : "A distibuted real-time search and analytics engine",
          "publish_date" : "2015-02-07",
          "num_reviews" : 20,
          "publisher" : "oreilly"
        }
      }
    ]
  }
}

2.2 所有字段("fields":[])

不傳和沒有都查所有字段
GET bookdb/_search
{
  "query": {
    "multi_match": {
      "query": "guide",
      "fields": []
    }
  }
}

結果同上!

3.列上score推升:Boosting(multi_match)

GET bookdb/_search
{
  "query": {
    "multi_match": {
      "query": "elasticsearch guide",
      "fields": ["title", "summary^3"]
    }
  }
}

結果

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 3.9835935,
    "hits" : [
      {
        "_index" : "bookdb",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 3.9835935,
        "_source" : {
          "title" : "Solr in Action",
          "authors" : [
            "trey grainger",
            "timothy potter"
          ],
          "summary" : "Comprehensive guide to implementing a scalable search engine using Apache Solr",
          "publish_date" : "2014-04-05",
          "num_reviews" : 23,
          "publisher" : "manning"
        }
      },
      {
        "_index" : "bookdb",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 3.1001682,
        "_source" : {
          "title" : "Elasticsearch in Action",
          "authors" : [
            "radu gheorge",
            "matthew lee hinman",
            "roy russo"
          ],
          "summary" : "build scalable search applications using Elasticsearch without having to do complex low-level programming or understand advanced data science algorithms",
          "publish_date" : "2015-12-03",
          "num_reviews" : 18,
          "publisher" : "manning"
        }
      },
      {
        "_index" : "bookdb",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 2.0281231,
        "_source" : {
          "title" : "Elasticsearch: The Definitive Guide",
          "authors" : [
            "clinton gormley",
            "zachary tong"
          ],
          "summary" : "A distibuted real-time search and analytics engine",
          "publish_date" : "2015-02-07",
          "num_reviews" : 20,
          "publisher" : "oreilly"
        }
      }
    ]
  }
}
可以看到summary^3之後排序完全變了:_score可以看出端倪

4. Bool 查詢

書名包含 ElasticSearch 或 Solr,且作者是 Clinton Gormley 不是 Radu Gheorge
GET bookdb/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "title": "Elasticsearch"
          }
        },
        {
          "match": {
            "title": "Solr"
          }
        }
      ],
      "minimum_should_match": 1, 
      "must": [
        {
          "match": {
            "authors": "Clinton Gormley"
          }
        }
      ],
      "must_not": [
        {
          "match": {
            "authors": "Radu Gheorge"
          }
        }
      ]
    }
  }
}

查詢結果

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 3.4088073,
    "hits" : [
      {
        "_index" : "bookdb",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 3.4088073,
        "_source" : {
          "title" : "Elasticsearch: The Definitive Guide",
          "authors" : [
            "clinton gormley",
            "zachary tong"
          ],
          "summary" : "A distibuted real-time search and analytics engine",
          "publish_date" : "2015-02-07",
          "num_reviews" : 20,
          "publisher" : "oreilly"
        }
      }
    ]
  }
}

5. 模糊(Fuzzy)查詢:

"clinton gormley" 近似:clniton gormley
模糊標準:編輯距離標準:下面任一(or)
  • 改變一個字符(box→fox)
  • 移除一個字符(black→lack)
  • 插入一個字符(sic→sick)
  • 調換兩個相鄰字符(act→cat)
GET bookdb/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "fuzzy": {
            "authors": {
              "value": "clniton"
            }
          }
        }
      ],
      "minimum_should_match": 1, 
      "must": [
        {
          "match": {
            "authors": "gormley"
          }
        }
      ]
    }
  }
}

查詢結果:

{
  "took" : 6,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 2.4772387,
    "hits" : [
      {
        "_index" : "bookdb",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 2.4772387,
        "_source" : {
          "title" : "Elasticsearch: The Definitive Guide",
          "authors" : [
            "clinton gormley",
            "zachary tong"
          ],
          "summary" : "A distibuted real-time search and analytics engine",
          "publish_date" : "2015-02-07",
          "num_reviews" : 20,
          "publisher" : "oreilly"
        }
      }
    ]
  }
}
這裏因為我要查的第二個詞是準的, 所以用了bool:should+must

6.通配符(Wildcard)查詢

  • ? 匹配任1字符
    • 匹配零個或多個字符。
GET bookdb/_search
{
  "query": {
    "wildcard": {
      "authors": {
        "value": "z*"
      }
    }
  },
  "_source": ["authors"], 
  "highlight": {
    "fields": {
      "authors": {}
    }
  }
}

結果:

{
  "took" : 5,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "bookdb",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "authors" : [
            "clinton gormley",
            "zachary tong"
          ]
        },
        "highlight" : {
          "authors" : [
            "<em>zachary</em> tong"
          ]
        }
      }
    ]
  }
}

7. 正則(Regexp)查詢

比通配符查詢 更復雜的模式進行查詢
GET bookdb/_search
{
  "query": {
    "regexp": {
      "authors": {
        "value": "z[a-z]*y"
      }
    }
  },
  "_source": ["authors"], 
  "highlight": {
    "fields": {
      "authors": {}
    }
  }
}

結果:

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "bookdb",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "authors" : [
            "clinton gormley",
            "zachary tong"
          ]
        },
        "highlight" : {
          "authors" : [
            "<em>zachary</em> tong"
          ]
        }
      }
    ]
  }
}

8. 短語匹配(match_phrase)查詢

8.1 match_phrase要求內容+順序一致

GET bookdb/_search
{
  "_source": ["summary"], 
  "query": {
    "match_phrase": {
      "summary": {
        "query": "search engine",
        "slop": 1
      }
    }
  }
}

結果:

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.88067603,
    "hits" : [
      {
        "_index" : "bookdb",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 0.88067603,
        "_source" : {
          "summary" : "Comprehensive guide to implementing a scalable search engine using Apache Solr"
        }
      }
    ]
  }
}

8.2 match_phrase之slop(詞間容插數)

slop 詞項間隔容忍度: slop:1->查詢詞中間容忍的插入詞的個數,如 search using->search engine using/search applications using
GET bookdb/_search
{
  "_source": ["summary"], 
  "query": {
    "match_phrase": {
      "summary": {
        "query": "search using",
        "slop": 1
      }
    }
  }
}

結果,匹配兩條:

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.34001905,
    "hits" : [
      {
        "_index" : "bookdb",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 0.34001905,
        "_source" : {
          "summary" : "Comprehensive guide to implementing a scalable search engine using Apache Solr"
        }
      },
      {
        "_index" : "bookdb",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 0.24634027,
        "_source" : {
          "summary" : "build scalable search applications using Elasticsearch without having to do complex low-level programming or understand advanced data science algorithms"
        }
      }
    ]
  }
}
user avatar cafebabe 头像 weirdo_67190d70053aa 头像 _624e4f5182eaf 头像 cherish_5ad82c136df47 头像
点赞 4 用户, 点赞了这篇动态!
点赞

Add a new 评论

Some HTML is okay.