官术网_书友最值得收藏!

Searching with multiple conditions

Often, we find ourselves in a position where we need to search with more than one criterion, where each of these criteria could be expressed as an inpidual query. In this section, let's see how we can make such a combination of search.

One simple way to express multiple search criteria is using a bool query. The bool query lets you use a combination of other queries, where each of the conditions can be put using a Boolean clause. There are three Boolean clauses:

  • must
  • should
  • must_not

The must clause suggests that the constituent query must appear in the matching documents. If there is no must clause in the query, a combination of should clauses can be given. Also, you may set the minimum number of should clauses that should be matched. As the name indicates, must_not means that this clause should not match the document.

Let's see how we can use the bool query in Elasticsearch:

{
    "bool" : {
        "must" : [{
            "term" : { "productType" : "Laptop " }
        }],
        "must_not" : {
            "range" : {
                "dateOfManufactoring" : { "from" : 1990/01/01, "to" : 2014/01/01 }
            }
        },
        "should" : [
            {
                "term" : { "color" : "yellow" }
            },
            {
                "term" : { "description" : "lenovo" }
            }
        ],
        "minimum_should_match" : 1,
        "boost" : 1.0
    }
}   

Here, we used a bool query to select all the laptops whose date of manufacturing is not between 1999 to 2014. Then, we boosted the laptops that were of the color yellow or were from lenovo.

Note

If there is at least one must query, then none of the should queries need to match a document to qualify that document as a result. In this case, the should query match will increase the relevance of that document.

主站蜘蛛池模板: 台东县| 盐边县| 乌苏市| 图们市| 广宗县| 德庆县| 集安市| 嵊泗县| 安阳县| 安化县| 班戈县| 忻州市| 荆州市| 清镇市| 米泉市| 镇沅| 孝义市| 霍林郭勒市| 神木县| 西盟| 藁城市| 通州区| 黑河市| 威信县| 太和县| 白山市| 乌什县| 林芝县| 田阳县| 修武县| 高邮市| 镇沅| 中山市| 房山区| 奉节县| 合肥市| 安西县| 临泉县| 石门县| 雷波县| 故城县|