Search code examples
javaelasticsearchelastic-stackelasticsearch-dslelasticsearch-query

Index on Elastic search contains and starts with search


We are using elastic search for faster searching on our organization data . The data model has organization id, address, organization name, business start date and organization contacts array .
We have asked to perform string contains search and string starts with search on organization id and/or organization name field For example, organization.name:”abc*” or organization.id:”abc

organization.name:”abc*” and organization.id:”*abc*”
organization.name:”*abc*” and organization.id:”abc*”
Since we need to use both on same field using Ngram analyzer is not working Please advise

Solution

  • As far as I can understand, you need to find those documents, where organization.name begins with abc AND organization.id contains abc (not in the beginning).

    For this, you can use multi-field, which is useful to index the same field in different ways for different purposes along with n-gram tokenizer

    Adding a working example with index data, mapping, search query, and search result

    Index Mapping:

        {
      "settings": {
        "analysis": {
          "analyzer": {
            "my_analyzer": {
              "tokenizer": "my_tokenizer"
            }
          },
          "tokenizer": {
            "my_tokenizer": {
              "type": "ngram",
              "min_gram": 3,
              "max_gram": 20,
              "token_chars": [
                "letter",
                "digit"
              ]
            }
          }
        },
        "max_ngram_diff": 20
      },
      "mappings": {
        "properties": {
          "organization": {
            "properties": {
              "name": {
                "type": "keyword",
                "fields": {
                  "raw": {
                    "type": "text",
                    "analyzer": "my_analyzer"
                  }
                }
              },
              "id": {
                "type": "keyword",
                "fields": {
                  "raw": {
                    "type": "text",
                    "analyzer": "my_analyzer"
                  }
                }
              }
            }
          }
        }
      }
    }
    

    Index Data:

    {
      "organization": {
        "id": "abc def",
        "name": "Aspect abc Technology"
      }
    }
    {
      "organization": {
        "id": "defabc",
        "name": "abc Aspect Technology"
      }
    }
    {
      "organization": {
        "id": "abcef",
        "name": "abc Aspect Technology"
      }
    }
    {
      "organization": {
        "id": "abc",
        "name": "Aspect Technology"
      }
    }
    

    Search Query:

    {
      "query": {
        "bool": {
          "should": [
            {
              "bool": {
                "must": [
                  {
                    "match": {
                      "organization.id.raw": "abc"
                    }
                  },
                  {
                    "prefix": {
                      "organization.name": "abc"
                    }
                  }
                ],
                "must_not": {
                  "prefix": {
                    "organization.id": "abc"
                  }
                }
              }
            },
            {
              "bool": {
                "must": [
                  {
                    "prefix": {
                      "organization.id": "abc"
                    }
                  },
                  {
                    "match": {
                      "organization.name.raw": "abc"
                    }
                  }
                ],
                "must_not": {
                  "prefix": {
                    "organization.name": "abc"
                  }
                }
              }
            }
          ],
          "minimum_should_match": 1
        }
      }
    }
    

    Search Result:

    "hits": [
          {
            "_index": "65054994",
            "_type": "_doc",
            "_id": "1",
            "_score": 1.3590312,
            "_source": {
              "organization": {
                "id": "abc def",
                "name": "Aspect abc Technology"
              }
            }
          },
          {
            "_index": "65054994",
            "_type": "_doc",
            "_id": "2",
            "_score": 1.0725547,
            "_source": {
              "organization": {
                "id": "defabc",
                "name": "abc Aspect Technology"
              }
            }
          }
        ]