0

My data looks like below:

  {
    "name": "name1",
    "name_gr": ["gr1","gr2"]
  },
  {
    "name": "name2",
    "name_gr": ["gr1","gr2"]
  },
  {
    "name": "name3",
    "name_gr": ["gr2","gr3"]
  },
  {
    "name": "name4",
    "name_gr": ["gr1","gr2","gr3","gr4"]
  },
  {
    "name": "name4",
    "name_gr": ["gr4","gr5"]
  }

Now, i am trying to write elastic search query using elasticsearch_dsl python module by importing Q from it.

My input query will be some regex on "name_gr". For eg: "name_gr": "gr[12]". So, in this case I want all the names which has both "gr1" and "gr2" in their list of "name_gr".

So, the result for ->

Example 1 : "name_gr": "gr[12]" will be :

  {
    "name": "name1",
    "name_gr": ["gr1","gr2"]
  },
  {
    "name": "name2",
    "name_gr": ["gr1","gr2"]
  },
  {
    "name": "name4",
    "name_gr": ["gr1","gr2","gr3","gr4"]
  }

because, gr1 and gr2 both are present in name1, name2 and name4.

Example 2: "name_gr": "gr.*" will be :

  {
    "name": "name4",
    "name_gr": ["gr1","gr2","gr3","gr4"]
  }

because, all the gr's (gr1,gr2,gr3,gr4) are present in name4 only.

I tried writing my query like below:

must = [{'match':{'regexp': {'name_gr': 'gr[12]'}}}]
result = Q('bool', must = must)

But it gives name3 also in result, which is invalid as both gr1 and gr2 are not present in it. Please help. I am stuck in this from few days now.

Thanks.

zubug55
  • 729
  • 7
  • 27

1 Answers1

1

You are using a match query in your example where you want to be using regexp instead. To do this just use:

s = Search().query('regexp', name_gr='gr[12]')
s.execute()

Hope this helps!

Honza Král
  • 2,982
  • 14
  • 11
  • Can you tell, how do i write the same thing if I am importing Q module this way: from elasticsearch_dsl import Q . Is this correct - Q('regexp', **{'name_gr': 'gr[12]'}) ? – zubug55 Jul 19 '18 at 17:43
  • Q is not a module, it is a function that will just construct the query, but it cannot actually be excuted, only `Search` can be executed. Using `Q` the syntax would be `Q('regexp', name_gr='gr[12]')` (the arguments to the `query` method are always passed to `Q` internally) – Honza Král Jul 19 '18 at 17:56
  • yes,that's correct. I have used exactly this Q('regexp', name_gr='gr[12]') . The problem is in the final result name3 is also coming. It should not come as name_gr list of name3 contains only gr2 not gr1. – zubug55 Jul 19 '18 at 19:08