-1

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
                }
            }
        }
    }
}
yx yao
  • 1
  • 2
  • Please help me quickly! – yx yao Jun 23 '20 at 07:16
  • 3
    Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – Adrian Mole Jun 23 '20 at 07:36
  • 1
    @yx yao can you add some sample data against which you are running your search query ? – ESCoder Jun 23 '20 at 07:46
  • @yx yao did you get a chance to go through my answer ? – ESCoder Jun 23 '20 at 14:15

2 Answers2

1

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

Gibbs
  • 21,904
  • 13
  • 74
  • 138
0

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

ESCoder
  • 15,431
  • 2
  • 19
  • 42
  • 1
    How is it really different from the above answer? apart from sample data. – Gibbs Jun 23 '20 at 08:20
  • 2
    @Gibbs I also got the same search query as you got, but in my answer, I also added some sample data and some links that will hopefully make the issue more clear – ESCoder Jun 23 '20 at 08:26