27

I am trying to do a simple query for two specified fields, and the manual and google is proving to be of little help. Example below should make it pretty clear what I want to do.

{
  "query": {
      "and": {
        "term": {
          "name.family_name": "daniel",
          "name.given_name": "tyrone"
        }
      }
   }
}

As a bonus question, why does it find "Daniel Tyrone" with "daniel", but NOT if I search for "Daniel". It behaves like a realy weird anti case sensitive search.

0

4 Answers 4

26

Edit: Updated, sorry. You need a separate Term object for each field, inside of a Bool query:

{
  "query": {
    "bool": {
        "must" : [
          {
           "term": {
             "name.family_name": "daniel"
           }
         },
         {
           "term": {
             "name.given_name": "tyrone"
           }
         }
       ]
     }
   }
}

Term queries are not analyzed by ElasticSearch, which makes them case sensitive. A Term query says to ES "look for this exact token inside your index, including case and punctuation".

If you want case insensitivity, you could add a keyword + lowercase filter to your analyzer. Alternatively, you could use a query that analyzes your text at query time (like a Match query)

Edit2: You could also use And or Bool filters too.

Sign up to request clarification or add additional context in comments.

8 Comments

You misunderstood my case sensitive question, I am saying it is behaving like an INVERSE case sensitive search, it does NOT find "Daniel" when I search with "Daniel"
The query example you gave, gives me an error > pastium.org/view/c0efa58b6b7689bfff5a9f480c01ed01
Gah, sorry about that. I should never answer questions on SO before coffee. I was thinking the And filter, when in reality ES only has the Bool query. You can also use the Bool or And filter depending on your use-case.
Re: case sensitivity...how do you have your analyzer set up? The same points apply - Term is only searching for the exact token stored inside the index. You might have an analyzer that is lowercasing your text before storing?
I have not really set up any analyzers. It is just using default
|
7

I found a solution for at least multiple text comparisons on the same field:

{
  "query": {
    "match": {
      "name.given_name": {
        "query": "daniel tyrone",
        "operator": "and"
      }
    }
  }

And I found this for multiple fields:

{
  "query": {
    "bool": {
      "must": [        
        {
          "match": {
            "name.formatted": {
              "query": "daniel tyrone",
              "operator": "and"
            }
          }
        },
        {
          "match": {
            "display_name": "tyrone"
          }
        }
      ]
    }
  }
}

3 Comments

Yep, your second query will work (assuming you want 'daniel tyrone' to be analyzed by the analyzers set on the doc field).
If you're not sure about the answer, you should not post it as an answer, you can always add it as a comment to your initial question
Finally I've found this! What does operator in your first query do? Am I right, that it will try to find words combination for given_name? In other words given_name should looks like 'daniel tyrone" not just daniel or tyrone?
0

If composing the json with PHP, these 2 examples worked for me.

$activeFilters is just a comma separated string like: 'attractions, limpopo'

$articles = Article::searchByQuery(array(
        'match' => array(
            'cf_categories' => array(
                'query' => $activeFilters,
                'operator' =>'and'
            )
        )
    ));

    // The below code is also working 100%
    // Using Query String https://www.elastic.co/guide/en/elasticsearch/reference/1.4/query-dsl-query-filter.html
    // https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html
    /* $articles = Article::searchByQuery(array(
        'query_string' => array(
            'query' => 'cf_categories:attractions AND cf_categories:limpopo'
        )
    )); */

Comments

0

This worked for me: minimum_should_match is set to 2 since the number of parameters for the AND query are 2.

{
"query": {
    "bool": {
        "should": [
                {"term": { "name.family_name": "daniel"}},
                {"term": { "name.given_name": "tyrone" }}
            ],
    "minimum_should_match" : 2

        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.