0

In ElasticSearch I've documents following the structure of an example document given below:

    {
        "ProductType": "TV",
        "Manufacturer": "XYZ",
        "Model": "XYZ-52-TV",
        "ProductDocumentationTopic": "DeviceSpecifications",
        "Content": "Lorem ipsum screen size = 10 Minim eu laborum ex veniam et ut commodo ullamco culpa irure ad nulla veniam et irure deserunt eiusmod nostrud"
    }

I'm trying to search Content value only within an exact specific product. The product will be identified by exactly matching values for ProductType,Manufacturer,Model, and ProductDocumentationTopic.

Hence, following the given example above, how can I search for the Content within "DeviceSpecifications" documentation of a "XYZ-52-TV" model TV manufactured by "XYZ"?

Please guide with an appropriate ElasticSearch query.

Temp O'rary
  • 5,366
  • 13
  • 49
  • 109

1 Answers1

0

You should start with proper mapping definition which will tell Elasticsearch how to store your fields in index. By default each string from your JSON will be analyzed which means that you'll be able to perform full text search on such fields. More info here

It's not exactly what you want to achieve for all fields except from Content. So if you want to do filtering on all the other fields you should define them as keywords in your mapping definition.

PUT http://{yourhost+port}/{indexname} HTTP/1.1

{
   "mappings": {
     "{yourtypename}": {
         "properties": {
            "ProductType": {
               "type": "keyword"
             },
             "Manufacturer": {
                "type": "keyword"
             },
             "Model": {
                "type": "keyword"
             },
             "ProductDocumentationTopic": {
                "type": "keyword"
             },
             "Content": {
                "type": "text"
             }
          }
       }
    }
 }

Then you can use query with filter, where you can perform full text search on Content field and exact matching for all the others (docs)

{
   "query": { 
      "bool": { 
         "must": [
            { "match": { "Content":   "size" }}  
         ],
         "filter": [ 
          { "term":  { "Model": "XYZ-52-TV" }},
          { "term":  { "Manufacturer": "XYZ" }},
          { "term":  { "ProductType": "TV" }}
       ]
      }
    }
  }
mickl
  • 48,568
  • 9
  • 60
  • 89
  • Still no difference. Are you missing something here? Do we need to setup the mapping against the data? Do I need to do anything more than exactly what you have mentioned here? – Temp O'rary Nov 03 '17 at 12:42
  • So you've defined your mapping, then indexed your data, then executed query I've attached and you're getting no results ? I didn't describe indexing itself, you can find more info here https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html – mickl Nov 03 '17 at 17:25
  • Thanks for the link but, I achieved the desired result using the query that I've shared over jsfiddle here https://jsfiddle.net/ak4z95bs/ for your perusal. Kindly review and suggest. Your first answer has guided me to achieve this. Thanks. – Temp O'rary Nov 05 '17 at 08:11