如何在Elasticsearch

时间:2017-03-25 22:33:47

标签: elasticsearch

在Elasticsearch中我想找到与我的搜索字段相关的相关记录,如stackoverflow相关问题建议。这意味着,匹配彼此靠近的搜索结果。

例如,我从以下数据中搜索“Men's Shoe”

  1. “Men's Shoe”
  2. “Girl's Shoe”
  3. “男士鞋黑色”
  4. “女鞋”
  5. “女鞋的粉红色”
  6. “女鞋红色”
  7. “男孩的鞋子”
  8. “男士鞋灰色”
  9. “Men's Shoe white color”
  10. “男鞋绿色”
  11. “男鞋”
  12. 然后我如何通过搜索项目“Men's Shoe”获得更多相关元组?我怎样才能获得与同义词相关的数据,即。男士鞋类。

    在Kibana中批量插入

    POST /atomap/product/_bulk
    {"index":{"_id":"1"}}
    {"name": "Girl's Shoe"}
    {"index":{"_id":"2"}}
    {"name": "Men's Shoe"}
    {"index":{"_id":"3"}}
    {"name": "Women's Shoe"}
    {"index":{"_id":"4"}}       
    {"name": "Women's Shoe pink color"}
    {"index":{"_id":"5"}}
    {"name": "Women's Shoe red color"}
    {"index":{"_id":"6"}}
    {"name": "Boy's Shoe"}
    {"index":{"_id":"7"}}
    {"name": "Men's Shoe red color"}
    {"index":{"_id":"8"}}
    {"name": "Men's Shoe white color"}
    {"index":{"_id":"9"}}
    {"name": "Men's Shoe green color"}
    {"index":{"_id":"10"}}
    {"name": "Men's Shoe gray color"}
    {"index":{"_id":"11"}}
    {"name": "Men's footwear"}
    

    我尝试过使用more like this查询:

    GET /atomap/product/_search
    {
      "query": {
        "more_like_this": {
            "like": "Men's shoe",
            "min_term_freq": 1,
            "min_doc_freq": 1
         }
       }
    }
    

    我的问题是如何搜索相关字词?当我搜索“男士鞋子”时,More Like This Query无法找到“男士鞋类”。

2 个答案:

答案 0 :(得分:1)

创建同义词和字段映射:

PUT /atomap
{
  "settings": {
    "analysis": {
      "filter": {
        "my_synonym_filter": {
          "type": "synonym", 
          "synonyms": [ 
            "shoe,footwear",
            "color,colour"
          ]
        }
      },
      "analyzer": {
        "my_synonyms": {
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "my_synonym_filter" 
          ]
        }
      }
    }
  },
  "mappings":{

  "product" : {
     "properties" : {
        "name" : {
           "type" : "string",
           "analyzer" : "my_synonyms"
        }
     }
  }

  }
}

然后插入所有数据,之后运行以下查询:

POST /myshop/_search
{
    "query": {
        "query_string": {
            "default_field": "name",
            "query": "Men's shoe",
            "analyzer": "my_synonyms"
        }
    }
}

答案 1 :(得分:0)

当我运行您的查询时(针对索引atomap而不是my_test

GET /atomap/product/_search
{
  "query": {
    "more_like_this": {
        "like": "Men's shoe",
        "min_term_freq": 1,
        "min_doc_freq": 1
     }
   }
}

我得到Men's footwear作为第4个结果,得分为0.62191015。在Elasticsearch 5.2上测试。

PS:提供测试数据和查询的奖励积分。否则就没有机会重现这一点。