文本字段

时间:2017-04-06 14:09:05

标签: elasticsearch similarity analyzer elasticsearch-analyzers

这是我在弹性搜索上的字段:

"keywordName": {
        "type": "text",
        "analyzer": "custom_stop"
      }

这是我的分析仪:

"custom_stop": {
      "type":      "custom",
      "tokenizer": "standard",
      "filter": [
        "my_stop",
        "my_snow",
        "asciifolding"
      ]
    }

这是我的过滤器:

           "my_stop": {
              "type":       "stop",
              "stopwords":  "_french_"
          },
           "my_snow" : {
                "type" : "snowball",
                "language" : "French"
            }

以下是我的文档索引(在我的唯一字段中:keywordName):

“canne a peche”,“canne”,“canne a peche telescopique”,“iphone 8”,“iphone 8 case”,“iphone 8 cover”,“iphone 8 charger”,“iphone 8 new”

当我搜索“canne”时,它会给我“canne”文档,这就是我想要的:

GET ads/_search
{
   "query": {
    "match": {
      "keywordName": {
        "query": "canne",
        "operator":  "and"
      }
    }
  },
  "size": 1
}

当我搜索“canneàpêche”时,它给了我“canne a peche”,这也没关系。同样适用于“CannesàPêche” - > “canne a peche” - >行。

这是一个棘手的部分:当我搜索“iphone 8”时,它给了我“iphone 8 cover”而不是“iphone 8”。如果我改变大小,我设置5(因为它返回包含“iphone 8”的5个结果)。我看到“iphone 8”是得分方面的第4个结果。第一个是“iphone 8 cover”,然后是“iphone 8 case”,然后是“iphone 8 new”,最后是“iphone 8”......

以下是查询的结果:

{
  "took": 5,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 5,
    "max_score": 1.4009607,
    "hits": [
      {
        "_index": "ads",
        "_type": "keyword",
        "_id": "iphone 8 cover",
        "_score": 1.4009607,
        "_source": {
          "keywordName": "iphone 8 cover"
        }
      },
      {
        "_index": "ads",
        "_type": "keyword",
        "_id": "iphone 8 case",
        "_score": 1.4009607,
        "_source": {
          "keywordName": "iphone 8 case"
        }
      },
      {
        "_index": "ads",
        "_type": "keyword",
        "_id": "iphone 8 new",
        "_score": 0.70293105,
        "_source": {
          "keywordName": "iphone 8 new"
        }
      },
      {
        "_index": "ads",
        "_type": "keyword",
        "_id": "iphone 8",
        "_score": 0.5804671,
        "_source": {
          "keywordName": "iphone 8"
        }
      },
      {
        "_index": "ads",
        "_type": "keyword",
        "_id": "iphone 8 charge",
        "_score": 0.46705723,
        "_source": {
          "keywordName": "iphone 8 charge"
        }
      }
    ]
  }
}

如何保持关键词“canne a peche”(口音,大写字母,复数词)的灵活性,还告诉他如果有完全匹配(“iphone 8”=“iphone 8”),请给出我确切的keywordName?

2 个答案:

答案 0 :(得分:1)

匹配查询使用tf / idf算法。这意味着您将获得按频率排序的模糊搜索结果。如果你想在完全匹配的情况下获得结果,你应该在之前创建一个query_string case,如果没有结果则使用你的匹配查询。

答案 1 :(得分:1)

我建议这样的事情:

    "keywordName": {
      "type": "text",
      "analyzer": "custom_stop",
      "fields": {
        "raw": {
          "type": "keyword"
        }
      }
    }

查询:

{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "keywordName": {
              "query": "iphone 8",
              "operator": "and"
            }
          }
        },
        {
          "term": {
            "keywordName.raw": {
              "value": "iphone 8"
            }
          }
        }
      ]
    }
  },
  "size": 10
}