Elastic / Kibana:在查询搜索中支持复数

时间:2019-03-08 21:48:11

标签: elasticsearch kibana

我将简化我的问题。假设我有一个索引,其中包含我用Kibana创建的3个文档:

PUT /test/vendors/1
{
  "type": "doctor",
  "name": "Phil",
  "works_in": [
      {
        "place": "Chicago"  
      },
      {
        "place": "New York"
      }
    ]
}

PUT /test/vendors/2
{
  "type": "lawyer",
  "name": "John",
  "works_in": [
      {
        "place": "Chicago"  
      },
      {
        "place": "New Jersey"
      }
    ]
}

PUT /test/vendors/3
{
  "type": "doctor",
  "name": "Jill",
  "works_in": [
      {
        "place": "Chicago"  
      }
    ]
}

现在我正在搜索:

GET /test/_search
{
  "query": {
    "multi_match" : {
      "query":    "doctor in chicago", 
      "fields": [ "type", "place" ] 
    }
  }
}

我得到了很好的答复:

{
  "took": 4,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0.2876821,
    "hits": [
      {
        "_index": "test",
        "_type": "vendors",
        "_id": "1",
        "_score": 0.2876821,
        "_source": {
          "type": "doctor",
          "name": "Phil",
          "works_in": [
            {
              "place": "Chicago"
            },
            {
              "place": "New York"
            }
          ]
        }
      },
      {
        "_index": "test",
        "_type": "vendors",
        "_id": "3",
        "_score": 0.2876821,
        "_source": {
          "type": "doctor",
          "name": "Jill",
          "works_in": [
            {
              "place": "Chicago"
            }
          ]
        }
      }
    ]
  }
}

现在事情开始变得有问题了...

doctor更改为doctors

GET /test/_search
{
  "query": {
    "multi_match" : {
      "query":    "doctors in chicago", 
      "fields": [ "type", "place" ] 
    }
  }
}

找不到0,结果为doctors。弹性不知道复数还是单数。

将查询更改为New York

GET /test/_search
{
  "query": {
    "multi_match" : {
      "query":    "doctor in new york", 
      "fields": [ "type", "place" ] 
    }
  }
}

但是除了doctor中的Chicago之外,响应结果集还给了doctor中的New York。字段与OR匹配...

另一个有趣的问题是,如果某人使用docsphysicianshealth professionals但表示doctor,会发生什么。有没有规定我可以教Elasticsearch将这些信息汇集到“医生”中?

是否仅使用elasticsearch就可以解决这些问题?在我自己的应用程序中,我将不必分析字符串的含义,然后构造一个复杂的精确Elasticsearch查询来匹配它?

我希望任何正确方向的指针

1 个答案:

答案 0 :(得分:1)

我假设typeplace字段的类型为TextStandard Analyzers

要管理单数/复数,您要查找的内容称为Snowball Token Filter,您需要将其添加到映射中。

您提到的另一个要求,例如physicians也应等效为doctor,您需要使用Synonym Token Filter

下面是映射的方式。请注意,我刚刚将分析器添加到type中。您可以对到其他字段的映射进行类似的更改。

映射

PUT <your_index_name>
{  
   "settings":{  
      "analysis":{  
         "analyzer":{  
            "my_analyzer":{  
               "tokenizer":"standard",
               "filter":[  
                  "lowercase",
                  "my_snow",
                  "my_synonym"
               ]
            }
         },
         "filter":{  
            "my_snow":{  
               "type":"snowball",
               "language":"English"
            },
            "my_synonym":{  
               "type":"synonym",
               "synonyms":[  
                  "docs, physicians, health professionals, doctor"
               ]
            }
         }
      }
   },
   "mappings":{  
      "mydocs":{  
         "properties":{  
            "type":{  
               "type":"text",
               "analyzer":"my_analyzer"
            },
            "place":{  
               "type":"text",
               "analyzer":"my_analyzer"
            }
         }
      }
   }
}

请注意,我是如何在映射本身中添加同义词的,而不是建议您在文本文件中添加同义词,如下所示

{  
   "type":"synonym",
   "synonyms_path" : "analysis/synonym.txt"
}

根据我分享的链接,它提到上面配置了一个同义词过滤器,其路径为analysis / synonym.txt(相对于配置位置)。

希望有帮助!