最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

elk - "Contains" search in ElasticSearch - Stack Overflow

programmeradmin5浏览0评论

I am new to ElasicSearch and was thrown into a project using it and I have a question about searching. We'd like to implement a "Contains()" type of search on a particular field like C# offers. I found something promising in this post however it is not working and just returns all results. Basically, we want to search the "meta_keywords" and return any results where it contains a string, for example "product". I have tried this based on the link I looked at:

{  
   "query": {  
      "query_string": {  
         "default_field": "meta_keywords",
         "query": "*product*"
      }
   }
}

However, this query just returns all the results and doesn't seem to filter out anything.

I haven't found any documentation on their site about this so not sure I am doing it correctly. Also, I'd like it to be case insensitive if possible. Also a link to their documentation would be helpful as well.

Thanks

I am new to ElasicSearch and was thrown into a project using it and I have a question about searching. We'd like to implement a "Contains()" type of search on a particular field like C# offers. I found something promising in this post however it is not working and just returns all results. Basically, we want to search the "meta_keywords" and return any results where it contains a string, for example "product". I have tried this based on the link I looked at:

{  
   "query": {  
      "query_string": {  
         "default_field": "meta_keywords",
         "query": "*product*"
      }
   }
}

However, this query just returns all the results and doesn't seem to filter out anything.

I haven't found any documentation on their site about this so not sure I am doing it correctly. Also, I'd like it to be case insensitive if possible. Also a link to their documentation would be helpful as well.

Thanks

Share Improve this question edited Mar 20 at 15:07 JimboJones asked Mar 19 at 20:24 JimboJonesJimboJones 1318 silver badges18 bronze badges 1
  • Please, provide an example with values of the meta_keywords field – G0l0s Commented Mar 22 at 8:04
Add a comment  | 

1 Answer 1

Reset to default 0

Tldr;

This should do the trick

Demo

I create an index with default mapping and analyzer

POST 79521320/_bulk
{"index":{}}
{"meta_keywords":"I sell good products"}
{"index":{}}
{"meta_keywords":"You know for search"}
{"index":{}}
{"meta_keywords":"Wh have been hanging out for days"}
{"index":{}}
{"meta_keywords":"I hate to smoke"}

GET 79521320/_search
{  
   "query": {  
      "query_string": {  
         "default_field": "meta_keywords",
         "query": "*product*"
      }
   }
}

The search query gives me only one results.

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 1,
      "relation": "eq"
    },
    "max_score": 1,
    "hits": [
      {
        "_index": "79521320",
        "_id": "PYrQ15UBtnCwjdJLMoii",
        "_score": 1,
        "_source": {
          "meta_keywords": "I sell good products"
        }
      }
    ]
  }
}
发布评论

评论列表(0)

  1. 暂无评论