Like query in nested type without using wildcard

Hi,

Am using nested data type for to create index

POST _my_index1
{
            "mappings": {
              "_doc": {
                "properties": {
                  "user": {
                    "type": "nested"
                  }
                }
              }
            }
 }

Inserted data

curl -XPOST -H "Content-Type: application/json" "http://localhost:9200/my_index1/_doc/1" -d '{
  "group" : "fans",
  "user" : [
    {
      "first" : "John",
      "last" :  "Smith"
    }
  ]
}'

curl -XPOST -H "Content-Type: application/json" "http://localhost:9200/my_index1/_doc/1" -d '{
  "group" : "fans",
  "user" : [
    {
      "first" : "John1",
      "last" :  "Smith1"
    }
  ]
}'

Need to fech date which matches first name like '%john%'. Tried with Elasticsearch

GET
{
  "_source": ["user"],
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "user",
            "query": {
              "bool": {
                "must": [
                  {
                    "query_string": {
                      "default_field": "user.first",
                      "query": "John*"
                    }
                  }
                ]
              }  
            }
          }
        }
      ],
      "filter": {
        "terms": {
          "_id": [ "2"] 
        }
      }
    }
  }
}
OUTPUT

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "my_index1",
        "_type": "_doc",
        "_id": "2",
        "_score": 1,
        "_source": {
          "user": [
            {
              "last": "Smith",
              "first": "John"
            },
            {
              "last": "White",
              "first": "Alice"
            }
          ]
        }
      }
    ]
  }
}

Please help me how to make like query without using wildcard this it may cause super slow.

Thanks,
Veera

Have a look at ngram and edge ngram. That might help

Hi

Tried to create index using ngram with nested type thowing error ,

ngram and edge ngram not supporting type nested. Its working for type text.

POST  my_index3

{
            "settings": {
                "analysis": {
                    "filter": {
                        "trigrams_filter": {
                            "type":     "ngram",
                            "min_gram": 3,
                            "max_gram": 3
                        }
                    },
                    "analyzer": {
                        "trigrams": {
                            "type":      "custom",
                            "tokenizer": "standard",
                            "filter":   [
                                "lowercase",
                                "trigrams_filter"
                            ]
                        }
                    }
                }
            },
            "mappings": {
                "my_type": {
                    "properties": {
                        "user": {
                            "type":     "nested",
                            "analyzer": "trigrams" 
                        }
                    }
                }
            }
        }

ERROR

{"error":{"root_cause":[{"type":"mapper_parsing_exception","reason":"Mapping definition for [text] has unsupported parameters: [analyzer : trigrams]"}],"type":"mapper_parsing_exception","reason":"Failed to parse mapping [my_type]: Mapping definition for [text] has unsupported parameters: [analyzer : trigrams]","caused_by":{"type":"mapper_parsing_exception","reason":"Mapping definition for [text] has unsupported parameters: [analyzer : trigrams]"}},"status":400}

Should it be

PUT _my_index3

?

Sorry typo mistake

PUT my_index3

You can't put an analyzer on a nested object but you can on the field inside the nested object.

So this is incorrect:

          "type": "nested",
          "analyzer": "trigrams"

Put the analyzer on last and first fields.

Hi,

Yes it works for me. But for me first,last...phoneno are dynamic field in nested type and i may get more dynamic fileds also how do i attach "analyzer": "trigrams" dynamically to new fields while inserting data.

Use this: https://www.elastic.co/guide/en/elasticsearch/reference/current/dynamic-templates.html

I have go through the link which you shared but din't found usage for nested type. Please help me with example for my use case

{
            "settings": {
                "analysis": {
                    "filter": {
                        "trigrams_filter": {
                            "type":     "ngram",
                            "min_gram": 3,
                            "max_gram": 3
                        }
                    },
                    "analyzer": {
                        "trigrams": {
                            "type":      "custom",
                            "tokenizer": "standard",
                            "filter":   [
                                "lowercase",
                                "trigrams_filter"
                            ]
                        }
                    }
                }
            },
            "mappings": {
                "company": {
                    "properties": {
                        "user": {
                            "type": "nested",
                            "properties": {
                                "first": {"type": "text","analyzer": "trigrams","index": true },
                                "last": { "type": "text","analyzer": "trigrams","index": true  }
                            }
                         }
                    }
                }
            }
        }

This one i tried i works for all my inputs. I need to add new fields dynamicall when user post data to server like phone no,address i need to insert values under "user" nested block by attaching "analyzer": "trigrams".

What did you try? Again read the page and specifically https://www.elastic.co/guide/en/elasticsearch/reference/current/dynamic-templates.html#path-match-unmatch. That should work.

Yes this works very well.

I tried to add full_address to mapping set got error "A dynamic template must be defined with a name"

{"error":{"root_cause":[{"type":"mapper_parsing_exception","reason":"A dynamic template must be defined with a name"}],"type":"mapper_parsing_exception","reason":"Failed to parse mapping [doc]: A dynamic template must be defined with a name","caused_by":{"type":"mapper_parsing_exception","reason":"A dynamic template must be defined with a name"}},"status":400}

POST my_index
{
            "settings": {
                "analysis": {
                    "filter": {
                        "trigrams_filter": {
                            "type":     "ngram",
                            "min_gram": 3,
                            "max_gram": 3
                        }
                    },
                    "analyzer": {
                        "trigrams": {
                            "type":      "custom",
                            "tokenizer": "standard",
                            "filter":   [
                                "lowercase",
                                "trigrams_filter"
                            ]
                        }
                    }
                }
            },
            "mappings": {
				"doc": {
				  "dynamic_templates": [
					{
					  "full_name": {
						"path_match":   "name.*",
						"mapping": {
						  "type":       "text",
						  "analyzer": "trigrams",
						  "index": true,
						  "copy_to":    "full_name"
						}
					  },
					  "full_address": {
						"path_match":   "address.*",
						"mapping": {
						  "type":       "text",
						  "analyzer": "trigrams",
						  "index": true,
						  "copy_to":    "full_address"
						}
					  }
					}
				  ]
				}
			  }
			
        }

How to attach more fieds to mapping set.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.