简体   繁体   中英

How to match different instances of the same query in Elasticsearch?

Example 1: My query term is "abcd".

My query structure is like this:

    query: {
       query_string: {
          query: "abc",
          fields: ["field1", "field2", "field3"]
       }
    },
    size: 50,
    "highlight": {
    "fields": {
        "field1": {},
        "field2": {},
        "field3": {}
    }

It matches the following instances:

abc abcs abc_def_ghi

But it does not match def_abc or def_abc_ghi. Basically instances where abc is in the middle of a string.

Example 2: In the same example above, if my query is abc_def

It does not match abc_def_ghi, although abc_def is present.

I have tried prefix_phrase and it solves scenario 2 but misses out on example 1's problems.

Any help would be appreciated.

for these usages you should use wildcard in query or regular expression

if you are using term query you can utilize wildcard term query or regexp query instead.

  • As name suggests phase_prefix is like poor mans autocomplete it searches for fields which starts with given phrase in your case abc , abcs , abc_def_ghi . as your field doesn't start with abc in case of def_abc , def_abc_ghi it won't work with phrase prefix.
  • Try using character filters specifically Pattern Replace Character Filter to replace _ with (space) from your field while analyzing your field. check this answer . so your token would result in [def,abc,ghi] instead of single token like [def_abc_ghi] . then you can search it using cross_field on analyzed field which should satisfy all of your mentioned cases.

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