cubanacan
cubanacan

Reputation: 654

Multi-field search for synonym in the query string

It looks like Elasticsearch does not take field analyzers into account for multi-field search using query string, without specifying field. Can this be configured for index or specified in the query?

Here it is a hands on example.

Given files from commit (spring-data-elasticsearch).
There is a test SynonymRepositoryTests, which will pass with QueryBuilders.queryStringQuery("text:british") and QueryBuilders.queryStringQuery("british").analyzer("synonym_analyzer") queries.

Is it possible to make it passing with QueryBuilders.queryStringQuery("british") query, without specifying field and analyzer for query?

Upvotes: 0

Views: 609

Answers (1)

ChintanShah25
ChintanShah25

Reputation: 12672

You could query without specifying fields or analyzers. By default query string query will query on _all field which is combination of all fields and uses standard analyzer. so QueryBuilders.queryStringQuery("british") will work.

You can exclude some fields from all fields while creating index and you can also create custom all field with the help of copy_to functionality.

UPDATE

You would have to use your custom analyzer on _all fields while creating index.

PUT text_index
{
  "settings": {
    "analysis": {
      "filter": {
        "edge_filter": {
          "type": "edge_ngram",
          "min_gram": 1,
          "max_gram": 10
        }
      },
      "analyzer": {
        "prefix_analyzer": {
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "trim",
            "edge_filter"
          ]
        }
      }
    }
  },
  "mappings": {
    "test_type": {
      "_all": {
        "enabled": true,
        "analyzer": "prefix_analyzer"  <---- your synonym analyzer
      },
      "properties": {
        "name": {
          "type": "string"
        },
        "tag": {
          "type": "string",
          "analyzer": "simple"
        }
      }
    }
  }
}

You can replace prefix_analyzer with your synonym_analyzer and then it should work.

Upvotes: 1

Related Questions