M.Ramzan
M.Ramzan

Reputation: 317

Partially matches the requirement in elastic-search query

I am trying to retrieve data from elasticsearch based on 2 conditions, It should match the jarFileName and dependentClassName. The query runs fine with jarFileName but it matches dependendentClassName partially.

This is the query I used.

{
"query": {
"bool": {
  "must": [
    {
      "match": {
        "dependencies.dependedntClass": "java/lang/String"
      }
    },
    {
      "match": {
        "JarFileName": {
          "query": "Client.jar"
         }
       }
     }
     ]
   }
  }
}

Query fully matches the jarFileName but for the dependentClassName it even matched and returned any part of the value mentioned. For an example if I used java/lang/String, it returns any type that has java or lang or String in their dependentClassName. I think its because of the "/". How can I correct this one?

EDIT

I used this query for mapping,

{
"classdata": {
"properties": {
  "dependencies": {
    "type": "object",
    "properties": {
      "dependedntClass": {
        "type": "string",
        "index": "not_analyzed"
      }
     }
    }
   }
  }
}

Upvotes: 1

Views: 62

Answers (1)

sunkuet02
sunkuet02

Reputation: 2442

You can set the index of dependencies.dependedntClass to not_analyzed so that your given string will not be analyzed with standard analyzer. If you are using ES 2.x then the below mapping should work fine.

PUT /your_index
{
    "mappings": {
        "your_type":{
            "properties": {
                "dependencies":{
                    "type": "string",
                    "fields": {
                        "dependedntClass":{
                            "type": "string",
                            "index": "not_analyzed"
                        }
                    }
                }
            }
        }
    }
}

Then, your query should also work fine.

EDIT (if dependencies field is of nested type)

If your dependencies field is of nested or array type, then change the mapping as like :

POST /your_index
{
    "mappings": {
        "your_type":{
            "properties": {
                "dependencies":{
                    "type": "nested",
                    "properties": {
                        "dependedntClass":{
                            "type": "string",
                            "index": "not_analyzed"
                        }
                    }
                }
            }
        }
    }
}

And the query should be changed as like below:

GET /your_index/_search
{
    "query": {
        "bool": {
          "must": [
            {
              "nested": {
                   "path": "dependencies",
                   "query": {
                       "match": {
                          "dependencies.dependedntClass": "java/lang/String"
                       }
                   }
                }
            },
            {
              "match": {
                "JarFileName": {
                  "query": "Client.jar"
                }
              }
            }
          ]
        }
    }
}

Upvotes: 1

Related Questions