0

I want to retrieve data from elastic search. I indexed data in elastc search and it look like below

{
  "_index": "user",
  "_type": "user_type",
  "_id": "2393294",
  "_version": 1,
  "_score": 2,
  "_source": {
    "name": "shwn decker",
    "worksFor": {
      "legalName": "nykc private fund",
      "address": "Mk street, bruke field"
    },
  },
  "fields": {
    "tweetDate": [
      1457481600000
     ]
   },
   "sample_date":"10/10/2013"
 }

I want to search the data based on the field "name" and "legalName". I tried some queries but when I am trying to filter data based on both field then no document is getting matched. Query which I used is:

 curl -XGET 'localhost:9200/user/user_type/_search?pretty' -H 'Content- Type: application/json' -d'
{
   "query":{
      "bool":{
         "must":{
             "query_string": {
                "query": "shwn decker",
             "fields": ["name"]
          }
         },
         "filter":{
            "term":{
                   "worksFor.legalName":"nykc private fund"
            }
         }
      }
   }
}'

What modification I should do in the query so that I can get the result. I am using customized mapping for this data which is shown below:

{
  "settings":
  {
    "number_of_shards" : 3,
    "number_of_replicas" : 2
  },
    "mappings" : {
      "user_type" : {
        "properties" : {
          "name" : {
            "type" : "text",
            "fields" : {
                  "raw" : {
                   "type":"keyword"
                   }
       }
          },
           "worksFor" : {
            "properties" : {
              "address" : {
                "type" : "text"
              },
              "legalName" : {
                "type" : "text",
                 "fields" : {
                  "raw" : {
                  "type":"keyword"
                   }
      }
              },
            }
          }
        }
      }
    }
  }'
4
  • Which ES version you're using ? Commented Nov 29, 2017 at 18:43
  • 5.5 It support bool query Commented Nov 29, 2017 at 18:45
  • Did you define any mappings before indexing your data ? Commented Nov 29, 2017 at 19:03
  • Yes I defined the mapping for this data. I edited the question to include mapping also. Commented Nov 30, 2017 at 6:06

1 Answer 1

0

I assume that you didn't define any mappings before indexing your data and all your fields are indexed as text which means that they are analyzed. It means that instead of

nykc private fund

your index contains three separate words for this document under worksFor.legalName key.

Then you're trying to do terms filter which is not analyzed so in fact you're comparing entire phrase against single words. That's why you're getting no results. If you still want to do terms filtering you should define this field as keyword.

So what you need to do is to remove your index, define mapping, at least for this one particular field and then index your data again. Here's how you can define mapping for your field

PUT localhost:9200/user

{
  "mappings": {
    "user_type": {
      "properties": {
        "worksFor.legalName": {
          "type": "keyword"
        }
      }
    }
  }
}

Then same query should return your document.

EDIT: all above is still valid but question was edited and mapping definition has been attached. With such mapping there should be:

{
 "query":{
    "bool":{
       "must":{
           "query_string": {
              "query": "shwn decker",
           "fields": ["name"]
        }
       },
       "filter":{
          "term":{
                 "worksFor.legalName.raw":"nykc private fund"
          }
       }
    }
 }

}

It's because raw part of legalName is a keyword.

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

3 Comments

but I am already defining the 'legalName' as a keyword type in my mapping.You can check the mapping above.
Thanks for clarification, the problem is that you have multifield mapping and legalName is still text. My example still works however if you want to use your mapping you should redefine your filter key to worksFor.legalName.raw since raw part is a keyword.
Thanks @mickl, I redefine the filter with worksFor.legalName.raw and it works. :)

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.