简体   繁体   中英

Elastic search is not returning expected results for term query

This is how ,my article data looks in elastic search

id:123,
title:xyz,
keywords:"Test Example"


id:124,
title:xyzz,
keywords:"Test Example|test1"

When a keyword is clicked on the front end,say for example: 'Test Example' then i should get articles having that keyword ( i should get above two articles as my results).But i am getting only first article as my result and below is my mapping:

"keywords":
{
"type":"string",
"index":"not_analysed"
}

How can i get both articles in search results?Thank you

Term Query searches for exact terms. That's why when you search for Test Example you get only one result, as there is only one record that exactly matches Test Example . If you want both the results you need to use something like match or query_string . You can use query_string like:

{
"query": {
    "query_string": {
       "default_field": "keywords",
        "query": "Test Example*"
    }
  }
}

您必须使用query_string进行查询,术语查询仅搜索确切的术语。

You set your keywords field to not_analyzed : if you want the field to be searchable you should remove the index clause like so

"keywords": {
    "type":"string"
}

Searching over this field with a match query, anyway, will return results containing a superset of the provided query: searching for test will return both documents even though the tag is actually Test Example .


If you can change your documents to something like this

id:123,
title:xyz,
keywords:"Test Example"


id:124,
title:xyzz,
keywords: ["Test Example", "test1"]

you can use your original mapping with "index":"not_analysed" and a term query will return only documents containing exactly the tag you were looking for.

{
  "query": {
    "term": {
      "keywords": "test1"
    }
  }
}

Another option to accomplish the same result is to use a pattern tokenizer to split your tag string on the | character to accomplish the same result

  "tokenizer": {
    "split_tags": {
      "type": "pattern",
      "group": "-1",
      "pattern": "\|"
    }
  }

I have got it working with the following tokenizer:

"split_keywords": {
      "type": "pattern",
      "group": "0",
      "pattern": "([^|]+)"
    }

Keywords will split at pipe character(below is the example)

{
  "tokens" : [ {
    "token" : "TestExample",
    "start_offset" : 0,
    "end_offset" : 12,
    "type" : "word",
    "position" : 1
  }, {
    "token" : "test",
    "start_offset" : 13,
    "end_offset" : 17,
    "type" : "word",
    "position" : 2
  }, {
    "token" : "1",
    "start_offset" : 17,
    "end_offset" : 18,
    "type" : "word",
    "position" : 3
  }, {
    "token" : "test1",
    "start_offset" : 13,
    "end_offset" : 18,
    "type" : "word",
    "position" : 3
  } ]
}

Now when i search for 'TestExample',i get above two articles. Thanks a lot for your help :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM