ashutosh sharma
ashutosh sharma

Reputation: 302

not able to search in compounding query using analyzer

I have a problem index which has multiple fields e.g tags (comma separated string of tags), author, tester. I am creating a global search where problems can be searched by all these fields at once. I am using boolean query e.g

{
    "query": {
        "bool": {
            "must": [{
                    "match": {
                        "author": "author_username"
                    }
                },
                {
                    "match": {
                        "tester": "tester_username"
                    }
                },
                {
                    "match": {
                        "tags": "<tag1,tag2>"
                    }
                }

            ]
        }
    }
}

Without Analyzer I am able to get the results but it uses space as separator e.g python 3 is getting searched as python or 3.

But I wanted to search Python 3 as single query. So, I have created an analyzer for tags so that every comma-separated tag is considered as one, not by standard whitespace.

{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "my_tokenizer"
        }
      },
      "tokenizer": {
        "my_tokenizer": {
          "type": "pattern",
          "pattern": ","
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "tags": {
        "type": "text",
        "analyzer": "my_analyzer", 
        "search_analyzer": "standard" 
      }
    }
  }
}

But now I am not getting any results. Please let me know what I am missing here. I am not able to find the use of analyzer in compound queries in the documentation: https://www.elastic.co/guide/en/elasticsearch/reference/current/compound-queries.html

Adding an example:

{

   "query": {
        "bool": {
            "must": [{
                    "match": {
                        "author": "test1"
                    }
                },
                {
                    "match": {
                        "tester": "test2"
                    }
                },
                {
                    "match": {
                        "tags": "test3, abc 4"
                    }
                }

            ]
        }
    }
}

Results should match all the fields but for the tags field there should be a union of tags and query should be comma-separated not by space. i.e query should match test and abc 4 but above query searching for test, abc and 4.

Upvotes: 3

Views: 150

Answers (1)

jaspreet chahal
jaspreet chahal

Reputation: 9099

You need to either remove search_analyzer from your mapping or pass my_analyzer in match query

GET tags/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "tags": {
              "query": "python 3",
              "analyzer": "my_analyzer"  --> by default search analyzer is used
            }
          }
        }
      ]
    }
  }
}

By default, queries will use the analyzer defined in the field mapping, but this can be overridden with the search_analyzer setting.

Upvotes: 2

Related Questions