Query_string with "Or" Query? (v1.6)


(Aaron Troy) #1

Let's say I have an index with 2 fields:

lastName:
	type: 'string'

emailAddresses:
	type: 'object'
	properties:
	  address:
	    type: 'string'

I want to return any record that either the lastName or any emailAddresses.address contain a given query_string.

For instance, for a query_string "est"..

This user would match:

{
    "lastName": "Best",
    "emailAddresses": [
    	{
    		"address": "none@none.com"
    	}
    ]
}

This user would match:

{
    "lastName": "Mark",
    "emailAddresses": [
    	{
    		"address": "none@test.com"
    	}
    ]
}

And this user would NOT match:

{
    "lastName": "Mark",
    "emailAddresses": [
    	{
    		"address": "none@none.com"
    	}
    ]
}

I have this working with a filtered query and or/regexp filters:

{
  "query": {
    "filtered": {
      "query": {
        "match_all": {}
      },
      "filter": {
        "or": [
          {
            "regexp": {
              "lastName": ".*est.*"
            }
          },
          {
            "regexp": {
              "emailAddresses.address": ".*est.*"
            }
          }
        ]
      }
    }
  }
}

I'd really prefer to NOT have to use regexp, but I haven't figured out a way to do it otherwise. Is there a way in version 1.6?


(Ivan Brusic) #2

ngrams would be more performant, but it comes at the price of increase
index size.

You can perhaps simplify your query by using a multi_match:
https://www.elastic.co/guide/en/elasticsearch/reference/1.6/query-dsl-multi-match-query.html

Ivan


(system) #3