2

I want to search in elasticsearch but getting hit even though the condition does not match. For eg:-

     {
         tweet: [
              {
                  firstname: Lav
                  lastname: byebye
             }
             {
                   firstname: pointto
                   lastname: ihadcre
             }
             {
                   firstname: letssearch
                   lastname: sarabhai
             }
          ]
      }
    }

Now there are following condition:-

1) must:- firstname: Lav must:- lastname: byebye required:there should be hit

getting: Hit

2) must:- firstname: Lav must:- lastname: ihadcre required:there should not be hit

getting: Hit

I should not be getting hit in 2nd condition which is problem

thanks for help

1
  • can u describe the exact query that you have tried out? Commented Jul 3, 2013 at 19:26

1 Answer 1

4

To achieve the behavior that you are describing, tweets should be indexed as nested objects and searched using nested query or filter. For example:

curl -XDELETE localhost:9200/test-idx
curl -XPUT localhost:9200/test-idx -d '{
    "settings": {
        "index.number_of_shards": 1,
        "index.number_of_replicas": 0
    },
    "mappings": {
        "doc": {
            "properties": {
                "tweet": {"type": "nested"}
            }
        }
    }
}'
curl -XPUT "localhost:9200/test-idx/doc/1" -d '{
    "tweet": [{
        "firstname": "Lav",
        "lastname": "byebye"
    }, {
        "firstname": "pointto",
        "lastname": "ihadcre"
    }, {
        "firstname": "letssearch",
        "lastname": "sarabhai"
    }]
}
'
echo
curl -XPOST "localhost:9200/test-idx/_refresh"
echo
curl "localhost:9200/test-idx/doc/_search?pretty=true" -d '{
    "query": {
        "nested" : {
            "path" : "tweet",
            "score_mode" : "avg",
            "query" : {
                "bool" : {
                    "must" : [
                        {
                            "match" : {"tweet.firstname" : "Lav"}
                        },
                        {
                            "match" : {"tweet.lastname" : "byebye"}
                        }
                    ]
                }
            }
        }
    }
}'
echo
curl "localhost:9200/test-idx/doc/_search?pretty=true" -d '{
    "query": {
        "nested" : {
            "path" : "tweet",
            "score_mode" : "avg",
            "query" : {
                "bool" : {
                    "must" : [
                        {
                            "match" : {"tweet.firstname" : "Lav"}
                        },
                        {
                            "match" : {"tweet.lastname" : "ihadcre"}
                        }
                    ]
                }
            }
        }
    }
}'
Sign up to request clarification or add additional context in comments.

2 Comments

It's not just at using nested queries at query-time. Make sure to change your mapping to support nesting at index-time too. Read the es docs on nested mapping
I hope the example will help you to figure it out.

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.