简体   繁体   中英

Questions about joint query?

Use Elasticsearch to search " productID of a or (productID of b and price of c) " with devp of kibana This is my code: (what is right?)

 GET my_store/products/_search
{
    "query":{
        "bool":{
            "should":[
                {
                    "match":{
                        "productId":a
                    }},
                    {"match":{
                        "productId":b
                    }
                }
                ],
            "must":{
                "match":{
                    "price":c
                }
            }
        }
    }
}

You want productID of a or (productID of b and price of c) . It sounds like

productId=a OR (productId=b AND price=C)

{
    "query": {
        "bool": {
            "should": [
                {
                    "match": {"productID": "a"}
                },
                {
                    "bool": {
                        "must": [
                            {"match": {"productID": "b"}},
                            {"match": {"price": "c"}}
                        ]
                    }
                }
            ]
        }
    }
}

You consider the below

OR = should AND = must

Since you have not mentioned anything about the data that you have taken, I have indexed the following data:

Sample Index Data

{
   "productId":"a",
   "price": 100
}
{
   "productId":"b",
   "price": 200
}
{
   "productId":"c",
   "price": 300
}

Search Query:

{
    "query": {
        "bool": {
            "should": [
                {
                    "match": {"productId": "a"}
                },
                {
                    "bool": {
                        "must": [
                            {"match": {"productId": "b"}},
                            {"match": {"price": 700}}
                        ]
                    }
                }
            ]
        }
    }
}

Search Result:

"hits": [
         {
            "_index": "foo10",
            "_type": "_doc",
            "_id": "1",
            "_score": 0.9808292,
            "_source": {
               "productId": "a",
               "price": 100
            }
         }
      ]

Here, since no data match with "productId": "b" and "price": 700`, in the Search result only the data with "productId": "a" is shown.

You can simply consider Must to be equivalent to logical AND and should as logical OR

Refer this to know about Elasticsearch difference between MUST and SHOULD bool query and to get detailed explanation about Boolean Query refer this official documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM