ElasticSearch Analyzer on text field

孤街浪徒 提交于 2019-12-24 08:12:43

问题


Here is my field on elasticSearch :

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

Here is my analyzer :

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

And here are my filters :

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

Here are my documents my index (in my only field : keywordName) :

"canne a peche", "canne", "canne a peche telescopique", "iphone 8", "iphone 8 case", "iphone 8 cover", "iphone 8 charger", "iphone 8 new"

When I search for "canne", it gives me the "canne" document, which is what I want :

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

When I search for "canne à pêche", it gives me "canne a peche", which is OK, too. Same for "Cannes à Pêche" -> "canne a peche" -> OK.

Here is the tricky part : when I search for "iphone 8", it gives me "iphone 8 cover" instead of "iphone 8". If I change the size, I set 5 (as it returns the 5 results containing "iphone 8"). I see that "iphone 8" is the 4th result in term of score. The first is "iphone 8 cover" then "iphone 8 case" then "iphone 8 new" and finally "iphone 8" ...

Here is the result of the query :

{
  "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"
        }
      }
    ]
  }
}

How can I keep the flexibility concerning the keyword "canne a peche" (accents, capital letters, plural terms) but also tell him that if there is an exact match ("iphone 8" = "iphone 8"), give me the exact keywordName ?


回答1:


I suggest something like this:

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

And the query:

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



回答2:


The match query uses the tf/idf algorithm. It means that you would get fuzzy search results ordered by frequency. If you want to get a result in a case of an exact match you should create a query_string case before and if there is no result use your match query.



来源:https://stackoverflow.com/questions/43257656/elasticsearch-analyzer-on-text-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!