jsonelasticsearchfilteringkibananon-relational-database

How to filter data by two clauses in Elasticsearch?


I am trying to filter a data set based on two conditions. I want my results to return the record if it satisfies either the condition 1 (where record contains A and B), or condition 2 (where record contains A and C), with wildcards included.

For example, if I have this table here (and Z is just a random filler character):

TABLE 1
AZZZ ZBZ
ZZZZ ZAZ
ZZAZ ZZB
CZZZ ZZA
AZZZ ZCZ
ZZZZ ZZZ
ZZCZ ZZZ

I would want my return result to be this:

TABLE 1 (Returned)
AZZZ ZBZ
ZZAZ ZZB
CZZZ ZZA
AZZZ ZCZ

I've been trying codes along these lines, but can not seem to get the desired results:

{  
   "query":{  
      "query_string":{  
         "analyze_wildcard":true,
         "default filter":"*",
         "query":"A B"
      }
   }
}

Any thoughts?


Solution

    1. Wildcard queries allow you to match parts of a string on not analyzed (keyword) fields, for example:

      POST my_index/_search
      {
          "query": {
              "wildcard" : { "my_field" : "*A*" }
          }
      }
      
    2. Bool queries are your friend for combining things. I think it would be easier to restructure this to the following which should be equivalent:

      POST my_index/_search
      {
        "query": {
          "bool" : {
            "must" : {
              "wildcard" : { "my_field" : "*A*" }
            },
            "should" : [
              { "wildcard" : { "my_field" : "*B*" } },
              { "wildcard" : { "my_field" : "*C*" } }
            ],
            "minimum_should_match" : 1
          }
        }
      }
      

    But this won't be very performant. It's a bit hard to abstract what kind of data you have there and what you want to achieve, but maybe there is a better way to achieve that? The problem feels very much modelled around the features of relational databases whereas a search engine has a lot more options.