elasticsearch玩转大数据大数据 爬虫Python AI Sql

十五、Elasticsearch使用most-fields策略进

2017-07-16  本文已影响41人  编程界的小学生

1、cross-fields解释
crosss-fields搜索,一个唯一标志,跨了多个field。比如一个人,标识是姓名;一个建筑,他的标识是地址。姓名可以散落在多个 field中,比如first-name和last-name;地址可以散落在country,province和city中。

跨多个field搜索一个标识,比如搜索一个人名或地址就是cross-fields搜索

初步来讲,如果要实现,可能用most-fields比较合适,因为best-fields是优先搜索单个field最匹配的结果。cross-fields本身就不是单field,而是多field搜索。

2、数据准备

POST /forum/article/_bulk
{ "update": { "_id": "1"} }
{ "doc" : {"author_first_name" : "Peter", "author_last_name" : "Smith"} }
{ "update": { "_id": "2"} }
{ "doc" : {"author_first_name" : "Smith", "author_last_name" : "Williams"} }
{ "update": { "_id": "3"} }
{ "doc" : {"author_first_name" : "Jack", "author_last_name" : "Ma"} }
{ "update": { "_id": "4"} }
{ "doc" : {"author_first_name" : "Robbin", "author_last_name" : "Li"} }
{ "update": { "_id": "5"} }
{ "doc" : {"author_first_name" : "Tonny", "author_last_name" : "Peter Smith"} }

3、实战

GET /forum/article/_search
{
  "query": {
    "multi_match": {
      "query": "Peter Smith",
      "type": "most_fields", 
      "fields": ["author_first_name", "author_last_name"]
    }
  }
}

结果

{
  "took": 7,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 0.6931472,
    "hits": [
      {
        "_index": "forum",
        "_type": "article",
        "_id": "2",
        "_score": 0.6931472,
        "_source": {
          "articleID": "KDKE-B-9947-#kL5",
          "userID": 1,
          "hidden": false,
          "postDate": "2017-01-02",
          "tag": [
            "java"
          ],
          "tag_cnt": 1,
          "view_cnt": 50,
          "title": "this is java blog",
          "content": "i think java is the best programming language",
          "sub_title": "learned a lot of course",
          "author_first_name": "Smith",
          "author_last_name": "Williams"
        }
      },
      {
        "_index": "forum",
        "_type": "article",
        "_id": "1",
        "_score": 0.5753642,
        "_source": {
          "articleID": "XHDK-A-1293-#fJ3",
          "userID": 1,
          "hidden": false,
          "postDate": "2017-01-01",
          "tag": [
            "java",
            "hadoop"
          ],
          "tag_cnt": 2,
          "view_cnt": 30,
          "title": "this is java and elasticsearch blog",
          "content": "i like to write best elasticsearch article",
          "sub_title": "learning more courses",
          "author_first_name": "Peter",
          "author_last_name": "Smith"
        }
      },
      {
        "_index": "forum",
        "_type": "article",
        "_id": "5",
        "_score": 0.51623213,
        "_source": {
          "articleID": "DHJK-B-1395-#Ky5",
          "userID": 3,
          "hidden": false,
          "postDate": "2017-03-01",
          "tag": [
            "elasticsearch"
          ],
          "tag_cnt": 1,
          "view_cnt": 10,
          "title": "this is spark blog",
          "content": "spark is best big data solution based on scala ,an programming language similar to java",
          "sub_title": "haha, hello world",
          "author_first_name": "Tonny",
          "author_last_name": "Peter Smith"
        }
      }
    ]
  }
}

结果score解释
按照我们理想的结果来看,应该是现在结果的倒序排序才合理。
为什么会这样呢?
因为Perter Smith,匹配author_first_name,匹配到了Smith。他在所有doc中author_first_name只出现了一次Smith,所以根据IDF算法,他的分数较高。
Peter Smith这个人,doc1中,Smith在author_last_name中,但是author_last_name出现了两次Smith,所以导致doc1的IDF分数较低。

4、问题(弊端):
(1)只是找到尽可能多的field匹配doc,而不是某个field完全匹配的doc

(2)most-fields,没办法用minimum_should_match去掉长尾数据(就是匹配的特别少的结果)。

(3)TF/IDF算法,比如Peter Smith和Smith Williams,搜索Peter Smith的时候,由于first_name中很少有Smith的,所以query在所有document中的频率很低,得到的分数很高,可能Smith Williams反而会排在Peter Smith前面

若有兴趣,欢迎来加入群,【Java初学者学习交流群】:458430385,此群有Java开发人员、UI设计人员和前端工程师。有问必答,共同探讨学习,一起进步!
欢迎关注我的微信公众号【Java码农社区】,会定时推送各种干货:


qrcode_for_gh_577b64e73701_258.jpg
上一篇下一篇

猜你喜欢

热点阅读