简体   繁体   中英

How can I build a compound query with SearchBox in searchkit?

I'm using searchkit to try to build a basic text search. I think the query I want to build is fairly simple. It needs to be structured like this:

{  
  "query":{  
    "bool":{  
      "must":[
        {  
          "multi_match":{
            "query":"test search",
            "type":"phrase_prefix",
            "fields":[
              "field_1^5",
              "field_2^4",
              "field_3"
            ]
          }
        },
        {
          "term":
          {
            "field_id": "3"
          }
        }
      ],
      "must_not":[
        {
          "term":
          {
            "status": "archived"
          }
        }
      ]
    }
  },
  "size":6,
  "highlight":{  
    "fields":{  
      "field_1":{},
      "field_2":{},
      "field_3":{}
    }
  }
}

I've tried using the prefixQueryFields attribute, which gave me something fairly close to what I wanted except it was using a BoolShould rather than a BoolMust , plus it was always including the default SimpleQueryString query. It looked something like this:

const prefixQueryFields = [
  'field_1^5',
  'field_2^4',
  'field_3',
];

...

<SearchBox
  searchOnChange={true}
  prefixQueryFields={prefixQueryFields}
/>

I couldn't figure out the issues there easily and decided to go with the queryBuilder attribute in SearchBox . This is what I came up with:

_queryBuilder(queryString) {
  const prefixQueryFields = [
    'field_1^5',
    'field_2^4',
    'field_3',
  ];
  return new ImmutableQuery()
    .addQuery(BoolMust([
      MultiMatchQuery(queryString, {
        type: 'phase_prefix',
        fields: prefixQueryFields,
      })
    ]))
    .addQuery(BoolMustNot([
      TermQuery('status', 'archived'),
    ]));
}

...

<SearchBox
  searchOnChange={true}
  queryBuilder={this.queryBuilder}
/>

This query came out even more messed up, and I have no idea what to try next after checking the documentation and a cursory look at the source code.

(For the sake of brevity, I will not bother including the incorrect queries these two attempts created unless someone thinks that info will be useful.)

Figured it out. Using the QueryDSL structures wasn't working out very well, but apparently you can create the query with pure JSON, which worked great. Basically updated my query builder to return as so:

return {
  bool: {
    must: [
      {
        multi_match:{
          query: queryString,
          type: 'phrase_prefix',
          fields: prefixQueryFields,
        }
      }
    ],
    must_not: [
      {
        term: {
          status: 'archived',
        }
      }
    ]
  }
};

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