简体   繁体   English

Elasticsearch查询字符串查询无法与同义词分析器一起使用

[英]Elasticsearch Query String Query not working with synonym analyzer

I am trying to configure elastic search with synonyms. 我正在尝试使用同义词配置弹性搜索。

These are my settings: 这些是我的设置:

                "analysis": {
                    "analyzer": {
                        "category_synonym": {
                            "tokenizer": "whitespace",
                            "filter": [
                                "synonym_filter"
                            ]
                        }
                    },
                    "filter": {
                        "synonym_filter": {
                            "type": "synonym",
                            "synonyms_path": "synonyms.txt"
                        }
                    }
                }

Mappings config: 映射配置:

        "category": {
            "properties": {
                "name": {
                    "type":"string",
                    "search_analyzer" : "category_synonym",
                    "index_analyzer" : "standard",
                    "fields": {
                        "raw": {
                            "type":  "string",
                            "index": "not_analyzed"
                        }
                    }
                }
            }
        }

And the list of my synonyms 还有我的同义词列表

film => video,
ooh => panels , poster,
commercial => advertisement,
print => magazine

I must say that I am using Elasticsearch Java API. 我必须说我正在使用Elasticsearch Java API。 I am using QueryBuilders.queryStringQuery because this is the only way how I set analyzers to my request. 我正在使用QueryBuilders.queryStringQuery因为这是将分析器设置为请求的唯一方法。 So, when I am making: 因此,当我制作时:

QueryBuilders.queryStringQuery("name:film").analyzer(analyzer)

It returns me 它返回我

[
  {
    "id": 71,
    "name": "Pitch video",
    "description": "... ",
    "parent": null
  },
  {
    "id": 25,
    "name": "Video",
    "description": "... ",
    "parent": null
  }
]

That is perfect for me, but when I am calling something like this 这对我来说很完美,但是当我打电话给这样的人时

QueryBuilders.queryStringQuery("name:vid").analyzer(analyzer)

I expect that it should return same objects, but there is nothing: [] 我希望它应该返回相同的对象,但是什么也没有: []

So, I added asterisk to queryStringQuery : 因此,我在queryStringQuery添加了星号:

QueryBuilders.queryStringQuery("name:vid*").analyzer(analyzer)

Works well, but now 效果很好,但是现在

QueryBuilders.queryStringQuery("name:film*").analyzer(analyzer)

returns me [] 还给我[]

So, how can I configure my elastic search that it will return same objects when I am searching video , vid , film and fil ? 因此,如何配置弹性搜索,使其在搜索videovidfilmfil时将返回相同的对象?

Thanks in advance! 提前致谢!

Hm, I don't think Elasticsearch will know to "translate" fil into vid :-). 嗯,我不认为Elasticsearch会知道将fil “翻译”为vid :-)。 So, I think you need edgeNGram s for this, both at indexing and search time. 因此,我认为您需要在索引和搜索时使用edgeNGram

PUT test
{
  "settings": {
    "analysis": {
      "analyzer": {
        "category_synonym": {
          "tokenizer": "whitespace",
          "filter": [
            "synonym_filter",
            "my_edgeNGram_filter"
          ]
        },
        "standard_edgeNGram": {
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "synonym_filter",
            "my_edgeNGram_filter"
          ]
        }
      },
      "filter": {
        "synonym_filter": {
          "type": "synonym",
          "synonyms_path": "synonyms.txt"
        },
        "my_edgeNGram_filter": {
          "type": "edgeNGram",
          "min_gram": 2,
          "max_gram": 8
        }
      }
    }
  },
  "mappings": {
    "test": {
      "properties": {
        "name": {
          "type": "string",
          "analyzer": "category_synonym",
          "index_analyzer": "standard_edgeNGram",
          "fields": {
            "raw": {
              "type": "string",
              "index": "not_analyzed"
            }
          }
        }
      }
    }
  }
}

POST test/test/1
{"name": "Pitch video"}
POST test/test/2
{"name": "Video"}

GET /test/test/_search
{
  "query": {
    "query_string": {
      "query": "name:fil"
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM