ElasticSearch核心语法使用-ES内功修炼第四篇

574 阅读3分钟

“这是我参与8月更文挑战的第6天,活动详情查看:8月更文挑战

上一篇介绍了ElasticSearch核心基础语法第三篇,本节介绍ElasticSearch核心语法第四篇。

一、控制搜索精准度

有时候需要对某词进行多条件、比例搜索时,可以进行如下查询。

1. operator

ElasticSearch默认对某字段进行搜索时,operator是or。

文档数据如下:

PUT /moe_article/_doc/1
{   
  "title": "java hello",
  "content": "java elasticsearch",
  "tags": ["java","world"]
}

1.1 or

content字段里边并没有“hadoop”,但也能搜到结果。

GET /moe_article/_search
{
  "query": {
    "match": {
      "content": "java world"
    }
  }
}

# 等价于如下写法
GET /moe_article/_search
{
  "query": {
    "match": {
      "content": {
        "query": "java world",
        "operator": "or"
      }
    }
  }
}

image.png

1.2 and

match查询content字段内容分词后必须有“java”且“hello”,才满足条件。所以查询为空。

GET /moe_article/_search
{
  "query": {
    "match": {
      "content": {
        "query": "java elasticsearch",
        "operator": "and"
      }
    }
  }
}

image.png

2. minimum_should_match

如果需要对搜索的词满足一定比例或者个数时,则可以用minimum_should_match进行搜索。

比如根据remark字段搜索时,输入“hello world”时,每个词占比差不多33%,如果搜索结果里至少满足2个词时,则minimum_should_match应该设置为超过66%,如果指定为30%,那搜索结果里只要满足一个词匹配即可。

文档数据如下:

PUT /moe_article/_doc/2
{   
  "title": "java架构师必须掌握哪些知识?",
  "content": "设计模式、数据结构与算法,等等。",
  "tags": ["java","架构师"],
  "remark":"java hello world !"
}

搜索如下:

用户输入了三个词,分别是hello、bbb、java进行搜索,每个词占比33%,所以至少2个词匹配。

GET /moe_article/_search
{
  "query": {
    "match": {
      "remark": {
        "query": "hello java aaa",
        "minimum_should_match": "70%"
      }
    }
  }
}

image.png

至少一个词匹配(低于66%),设置如下即可:

GET /moe_article/_search
{
  "query": {
    "match": {
      "remark": {
        "query": "bbb java aaa",
        "minimum_should_match": "50%"
      }
    }
  }
}

image.png

也可以写数字。

3. should + bool

使用should + bool搜索的话,也可以控制搜索条件的匹配度。

should:只要满足一个条件即可。

GET /moe_article/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "remark": "java"
          }
        },
        {
          "match": {
            "remark": "hello"
          }
        },
        {
          "match": {
            "remark": "abc"
          }
        }
      ],
      "minimum_should_match": 2
    }
  }
}

二、match转化

在ElasticSearch进行match搜索时,ES底层通常会对搜索条件进行转换,来实现最终的效果。

1. 例子1

remark只要满足hello或java一个词即可。

GET /moe_article/_search
{
  "query": {
    "match": {
      "remark": "hello java"
    }
  }
}

转化后:

GET /moe_article/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "term": {
            "remark": "hello"
          }
        },
        {
          "term": {
            "remark": {
              "value": "java"
            }
          }
        }
      ]
    }
  }
}

2. 例子2

remark必须满足hello和java条件。

GET /moe_article/_search
{
  "query": {
    "match": {
      "remark": {
        "query": "hello java",
        "operator": "and"
      }
    }
  }
}

转化后:

GET /moe_article/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "remark": "hello"
          }
        },
        {
          "term": {
            "remark": {
              "value": "java"
            }
          }
        }
      ]
    }
  }
}

3. 总结

尽量使用转换后的语法执行搜索,效率更高。

三、boost权重控制

大家在进行百度搜索时,当搜索某个词后,排名前几的不一定就是要的结果,有的时候基本上都是广告,大家懂的,交钱即可进行竞价排名。这种需求怎么实现,可以利用boost来控制权重。

需求:有的帖子包含java hadoop,有的帖子包含java elasticsearch,在搜索标题中包含java的帖子时,要求包含elasticsearch的帖子要比hadoop优先搜索出来。

PUT /moe_article/_doc/6
{   
  "title": "java 怎么学?",
  "content": "a",
  "tags": ["a"],
  "remark":"a"
}

PUT /moe_article/_doc/7
{   
  "title": "java elasticsearch 怎么学?",
  "content": "b",
  "tags": ["b"],
  "remark":"b"
}

PUT /moe_article/_doc/8
{   
  "title": "java hadoop 怎么学?",
  "content": "c",
  "tags": ["c"],
  "remark":"c"
}

PUT /moe_article/_doc/9
{   
  "title": "java elasticsearch xxx?",
  "content": "d",
  "tags": ["d"],
  "remark":"d"
}

PUT /moe_article/_doc/10
{   
  "title": "java hadoop xxx?",
  "content": "e",
  "tags": ["e"],
  "remark":"e"
}


GET /moe_article/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "title": "java"
          }
        }
      ],
      "should": [
        {
          "match": {
            "title": {
              "query": "hello elasticsearch",
              "boost": 3
            }
          }
        },
        {
          "match": {
            "title": {
              "query": "hadoop",
              "boost": 2
            }
          }
        }
      ]
    }
  }
}

搜索结果如下:

image.png

四、总结

使用ElasticSearch进行搜索时,尽量使用转化后的语法执行搜索,效率会高。当需要对搜索结果进行权重控制时,可以使用boost进行处理。

欢迎大家关注微信公众号(MarkZoe)互相学习、互相交流。